如何使用 Azure DevOps Services 管道 yaml 中的变量指定触发器

Chu*_*dad 3 azure-pipelines azure-pipelines-tasks

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

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

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}}/*
Run Code Online (Sandbox Code Playgroud)

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

  1. 由于找不到解决我的场景的文档,我错过了什么?
  2. 如果我想做的事情不受支持,是否有建议的方法可以实现相同的目标?

谢谢

Lev*_*SFT 5

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

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

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

创建一个额外的管道来检查源分支是否匹配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')
Run Code Online (Sandbox Code Playgroud)

在上面的管道中。首先会触发它来检查源分支是否与 farmat 匹配。如果匹配,则将执行任务 TriggerBuild 以触发主管道。