如何使 YAML 文件触发并 checkout 分支并运行不同的步骤?
我正在处理 azure 的 YAML 文件,我想在主分支中运行某些步骤,并在 QA 分支中运行其他步骤。
trigger:
- master
pool:
vmImage: 'Ubuntu-16.04'
steps:
- script: ls $(Build.Repository.LocalPath)
displayName: 'printing the ls
我想检查主分支并运行一个步骤,但如果 QA 分支发生变化,我想触发,检查 QA 分支并运行其他步骤。 YAML 应该是什么样子?
最佳答案
在每个步骤中,您可以为每个任务/脚本添加一个条件:
:
condition: and(succeeded(), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), ne(variables['Build.Reason'], 'PullRequest')))
这将触发主分支构建的任务,除非为拉取请求验证而触发构建。一个完整的例子:
task: SnykTask@1
condition: and(succeeded(), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), ne(variables['Build.Reason'], 'PullRequest')))
displayName: 'Dependency scan'
inputs:
file: xxxxx
test: true
monitor: false
authType: endpoint
endpoint: xxx
severityThreshold: high
failBuild: false
您还可以在 yaml 文件中定义一个阶段。阶段可以包含一组步骤,也可以设置条件:
stages:
- stage: string # name of the stage, A-Z, a-z, 0-9, and underscore
displayName: string # friendly name to display in the UI
dependsOn: string | [ string ]
condition: string variables: { string: string } | [ variable | variableReference ]
jobs: [ job | templateReference]
在最极端的情况下,您可以创建多个 yaml 文件并将它们提交到源代码管理。然后进入 Azure Pipelines UI 并为每个 yaml 文件创建一个管道。让它们完全分开。
另请参阅:
关于azure - 如何根据分支触发器在 yml 文件中运行不同的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55657444/