首页 > 解决方案 > 如何在 Azure DevOps Services 管道 yaml 中使用变量指定触发器

问题描述

以下天蓝色管道代码给出错误

'在此上下文中不允许使用模板表达式'

variables:
  major: 2020
  minor: 3
  patch: 1
  major_minor_patch: $(major).$(minor).$(patch)

trigger:
- master
- Dev
- release/R${{variables.major_minor_patch}}
- release/${{variables.major_minor_patch}}/*

我的意图是使用主要、次要和补丁变量来指定将形成 CI 触发器的分支,而不是在管道 YAML 中对其进行硬编码。

  1. 因为找不到解决我的场景的文档,我错过了什么?
  2. 如果我试图做的事情不受支持,是否有建议的方法来实现同样的目标?

谢谢

标签: azure-pipelinesazure-pipelines-tasks

解决方案


不支持触发块中的变量。有关更多信息,请参阅此处的文档。

触发器块不能包含变量或模板表达式。

如果您不希望管道被其他分支触发,您可以尝试以下解决方法。

创建一个额外的管道来检查源分支是否匹配 release/major_minor_patch。并在这个附加管道中触发主管道。

variables:
  major: 2020
  minor: 3
  patch: 1
  triggerMain: false

trigger:
 branches:
   include:
    - releases/*
       
steps:
- powershell: |
     $branch = "$(Build.SourceBranchName)"
     if ($branch -match "$(major).$(minor).$(patch)") {
       echo "##vso[task.setvariable variable=triggerMain]True"  #set variable triggerMain to true if matches.
     }

- task: TriggerBuild@3
  inputs:
    definitionIsInCurrentTeamProject: true
    buildDefinition: '56'  #{id of your main pipeline}
    queueBuildForUserThatTriggeredBuild: true
    ignoreSslCertificateErrors: false
    useSameSourceVersion: true
    useSameBranch: true
    waitForQueuedBuildsToFinish: false
    storeInEnvironmentVariable: false
    authenticationMethod: 'Personal Access Token'
    password: '$(system.accesstoken)'
    enableBuildInQueueCondition: false
    dependentOnSuccessfulBuildCondition: false
    dependentOnFailedBuildCondition: false
    checkbuildsoncurrentbranch: false
    failTaskIfConditionsAreNotFulfilled: false
  condition: eq(variables['triggerMain'], 'True')

在上述管道中。它将首先被触发以检查源分支是否与农场匹配。如果匹配,则将执行任务 TriggerBuild 以触发主管道。


推荐阅读