在Azure管道中使用Powershell / CMD验证YAML文件

时间:2020-11-02 15:15:12

标签: powershell azure-devops azure-pipelines

我需要通过PowerShell或CMD在azure管道中验证YAML文件,如果YAML文件无效,则它将使任务失败。

能否请您提供一些有关实现方法的建议。

2 个答案:

答案 0 :(得分:1)

您可以使用powershell-yaml模块来验证YAML文件,如下所示:

    steps:
    - pwsh: Install-Module powershell-yaml -Force
    - pwsh: |
        $content = Get-Content -Path stackoverflow\80-yaml\test.yaml -Raw
        $correct = true
        try {
          $obj = ConvertFrom-Yaml $content
          Write-Host $obj
        }
        catch {
            Write-Host "An error occurred:"
            
            $correct = false
        }
        

        
        Write-Host $correct

但是请记住设置$ErrorActionPreference

答案 1 :(得分:0)

就像 Shayki Abramczyk 一样,不确定是否要class Comment { Comment({ this.id, this.author, this.isAuthor, this.isLiked, this.content, this.stats, this.createdAt, this.updatedAt, this.post, this.tagged, this.replies, }); int id; String author; bool isAuthor; bool isLiked; String content; String stats; DateTime createdAt; DateTime updatedAt; String post; List<dynamic> tagged; List<Comment> replies; factory Comment.fromJson(Map<String, dynamic> json) => Comment( id: json["id"], author: json["author"], isAuthor: json["isAuthor"], isLiked: json["isLiked"], content: json["content"], stats: json["stats"], createdAt: DateTime.parse(json["createdAt"]), updatedAt: DateTime.parse(json["updatedAt"]), post: json["post"], tagged: List<dynamic>.from(json["tagged"].map((x) => x)), replies: List<Comment>.from(json["replies"].map((x) => Comment.fromJson(x))), ); } 文件或另一个azure-pipelines.yml文件

对于.yml

有一个服务器端API:

使用这样的JSON正文发布到azure-pipelines.yml

dev.azure.com/<org>/<project>/_apis/pipelines/<pipelineId>/runs?api-version=5.1-preview

响应将包含呈现的YAML。您可以在以下链接中找到更多详细信息:Expose YAML validation features as standalone tool

对于其他{ "PreviewRun": true, "YamlOverride": " # your new YAML here, optionally " } 文件

您可以在powershell任务中使用脚本或某些第三方扩展Files Validator来处理此问题。