我在 Azure DevOps 中有一个管道,可以在合并之前检查我的 Ansible 代码以捕获错误。
当我们在 GitHub 中使用所需的状态检查时,我希望将任何 lint 错误作为评论添加回来。
我遇到两个问题。
#1 - 无法设置评论
- 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
评论.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 }}
如果我只传递静态注释(例如“test”),这个 comment.yaml 文件就可以工作。
#2 - 管道退出,状态代码为 2
如果我将注释更改为静态注释,为了解决上述问题,管道现在会以状态代码 2 退出,而不是状态代码 1,就像我在此处设置的那样:
if [ $? != 0 ]; then exit 1; fi
如果现有代码为 2,GitHub 不会在出现 lint 错误时将管道标记为失败。
temp
任务中的局部变量AzureCLI@2
在以后的任务中不可用。
您需要使用task.setvariable日志记录命令添加变量,以便后续任务可以使用宏语法
$(myVar)
使用该变量。默认情况下,该变量仅适用于同一作业中的任务。
示例:
steps:
- script: |
GIT_COMMENT="foobar"
echo "##vso[task.setvariable variable=Comment]$GIT_COMMENT"
displayName: 'Set pipeline variable: Comment'
- script: |
echo "Git comment: $(Comment)"
displayName: 'Print pipeline variable: Comment'
请参阅 在脚本中设置变量