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

标签 azure azure-devops azure-pipelines

如何使 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/

相关文章:

如果已登录到另一个 Microsoft 帐户,使用 WAAD 作为 IdP 的 Azure ACS 会出现 WS-Federation 协议(protocol)错误

带有自托管代理的azure devops : can't deploy to aks cluster

c# - 如何在没有位置路径的情况下在 azure 存储上上传zip文件?

Python Azure 存储表 : query_entities by datetime

php - Azure AD 组配置 : not getting PATCH request from AD when updating the group

azure - 找不到测试文件时如何使 AzureDevops 管道失败

Azure Pipelines "Require Template"检查不起作用

azure - 为什么在我的 Azure 应用服务中安装了 python 3.6 版本后,python 仍然是 2.7 版本?

azure - 未从 Azure Devops 管道中的变量组中的变量获取任何值

azure-devops - 如何使用 SSH 获取 VSTS 中的远程 Git 存储库?