如何根据分支触发器在yml文件中运行不同的步骤

时间:2019-04-12 18:11:48

标签: azure azure-devops azure-pipelines

如何使YAML文件触发并签出分支并运行不同的步骤?

我正在处理YAML文件以获取天蓝色,我想在我的master分支中运行某些步骤,而在QA分支中运行其他步骤。

trigger:
- master
pool:
  vmImage: 'Ubuntu-16.04'
steps:
- script: ls $(Build.Repository.LocalPath)
  displayName: 'printing the ls

我想检出master并运行一个步骤,但是如果要触发的QA分支发生某些变化,请检出QA分支并运行其他步骤。 YAML应该是什么样?

1 个答案:

答案 0 :(得分:3)

在每个步骤上,您都可以在每个任务/脚本中放置一个condition:

 condition: and(succeeded(), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), ne(variables['Build.Reason'], 'PullRequest')))

这将触发master分支构建的任务,除非触发构建请求进行拉取请求验证时除外。一个完整的例子:

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文件创建一个管道。为了使它们完全分开。

另请参阅: