Azure DevOps流水线:GitHub动态注释失败与退出码异常问题
Azure DevOps流水线Ansible Lint检查问题及优化尝试
我在Azure DevOps中配置了一条流水线,用于代码合并前对Ansible代码执行Lint检查以捕获错误。由于GitHub启用了必填状态检查,希望将Lint错误以注释形式添加到GitHub拉取请求中,但遇到两个问题:
问题1:无法设置动态注释
当前用AzureCLI@2任务捕获Lint错误并尝试传递给comment.yaml模板,但无法成功添加动态注释,仅静态注释(如'test')可正常生效。相关代码如下:
- task: AzureCLI@2 displayName: ${{ parameters.dispName }} continueOnError: true inputs: addSpnToEnvironment: true azureSubscription: ${{ parameters.serviceConnectionName }} scriptType: bash scriptLocation: inlineScript inlineScript: | set -euo pipefail export ARM_CLIENT_ID=$servicePrincipalId export ARM_CLIENT_SECRET=$servicePrincipalKey export ARM_SUBSCRIPTION_ID=$(az account show --query id | xargs) export ARM_TENANT_ID=$tenantId export TF_CLI_ARGS="-no-color" echo $PATH ansible --version ansible-lint --version ansible-lint ${{ parameters.command }} 2>&1 | tee ${{ parameters.logFile }}.log temp=(grep -A 2 Error ${{ parameters.logFile }}.log) if [ $? != 0 ]; then exit 1; fi - template: comment.yaml parameters: comment: $temp
comment.yml
--- parameters: - name: comment type: string steps: - task: GitHubComment@0 displayName: 'Add github comment' inputs: pull-requests: write gitHubConnection: 'github-comment' repositoryName: '$(Build.Repository.Name)' comment: ${{ parameters.comment }}
问题2:流水线退出码为2而非预期的1
若改用静态注释规避上述问题,流水线会以状态码2退出,而非设置的exit 1,导致GitHub在出现Lint错误时无法标记流水线为失败。
更新后的代码
主流水线yaml:
--- parameters: - name: serviceConnectionName type: string - name: command type: string - name: dispName type: string - name: logFile type: string steps: - template: init-github.yaml - template: install-ansible-lint.yaml - task: AzureCLI@2 displayName: ${{ parameters.dispName }} inputs: addSpnToEnvironment: true azureSubscription: ${{ parameters.serviceConnectionName }} scriptType: bash scriptLocation: inlineScript inlineScript: | set -euo pipefail export ARM_CLIENT_ID=$servicePrincipalId export ARM_CLIENT_SECRET=$servicePrincipalKey export ARM_SUBSCRIPTION_ID=$(az account show --query id | xargs) export ARM_TENANT_ID=$tenantId export TF_CLI_ARGS="-no-color" echo $PATH ansible --version ansible-lint --version ansible-lint ${{ parameters.command }} 2>&1 | tee ${{ parameters.logFile }}.log error=$(grep -A 2 Error ${{ parameters.logFile }}.log) echo "##vso[task.setvariable variable=Comment]$error" - template: comment.yaml parameters: comment: $(Comment)
comment.yaml文件:
--- parameters: - name: comment type: string steps: - task: GitHubComment@0 displayName: 'Add github comment' inputs: pull-requests: write gitHubConnection: 'github-comment' repositoryName: '$(Build.Repository.Name)' comment: ${{ parameters.comment }} - task: Bash@3 displayName: Fail Pipeline If Error inputs: targetType: 'inline' script: | if [ -n "${{ parameters.comment }}" ]; then echo "##vso[task.complete result=Failed;]" fi

内容的提问来源于stack exchange,提问作者Dan Carlton
相关产品推荐
相关产品推荐

