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

问题描述 投票:0回答:1

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

我正在为azure工作一个YAML文件,我希望在我的主分支和QA分支中的其他步骤中运行某些步骤。

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

我想查看主服务器并运行一个步骤,但如果QA分支上的某些内容发生变化我想要触发,请检查QA分支并运行其他步骤。 YAML应该是什么样的?

azure azure-devops azure-pipelines
1个回答
3
投票

在每个步骤中,您可以将condition:放入每个任务/脚本:

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

也可以看看:

© www.soinside.com 2019 - 2024. All rights reserved.