【问题标题】:How to specify triggers using variables in Azure DevOps Services pipeline yaml如何使用 Azure DevOps Services 管道 yaml 中的变量指定触发器
【发布时间】:2020-11-19 05:46:34
【问题描述】:

以下 azure 管道代码给出错误

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

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-pipelines azure-pipelines-tasks


    【解决方案1】:

    不支持触发器块中的变量。有关详细信息,请参阅文档 here

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

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

    创建一个额外的管道来检查源分支是否匹配 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任务触发主管道。

    【讨论】:

    • 这很好。但是,文档肯定不清楚。此外,这可能是在 AzDo 的未来版本中实现的一个不错的功能
    【解决方案2】:

    我也没有找到证据,但我认为这是设计上的正确行为。触发器是启动管道运行的东西。在启动此运行之前,无法计算变量。当他们可以的时候,已经太晚了——整个事情都被触发了。

    这就是为什么,我认为,您必须调整并尝试仅使用通配符来实现您的目标。如果应该为名称中包含版本的发布分支触发管道,那么这种通配符模式可能会起作用:release/R?.?.?

    【讨论】:

    • 是的,我想这就是原因。但是,我认为文档不是很清楚。感谢您的帮助。
    【解决方案3】:

    我遇到了同样的问题,我的方法是这样的:

    trigger:
      batch: true
      branches:
        include:
        - dev
        - master
        - releases/*
    
    jobs:
      - job: Job    
        condition: eq(variables['Build.SourceBranch'], variables['TARGET_BRANCH'])
        steps: ...
    

    在我做一个条件作业的同一管道上,它在每个分支上被触发,但它只执行我的目标分支中的步骤。

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 2020-04-13
      • 2021-11-23
      • 1970-01-01
      • 2022-01-17
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      相关资源
      最近更新 更多