【问题标题】:Azure DevOps Pipeline variables in templates模板中的 Azure DevOps Pipeline 变量
【发布时间】:2021-03-17 08:28:29
【问题描述】:

我正在使用 Azure 管道模板。我希望启动管道的开发人员设置特定分支的变量或保留为 $(Build.SourceBranch)

原因是从不同的存储库/分支中提取工件以进行组合。

所以在我的 yml 上我添加了参数(为简单起见只显示 1)

parameters:
- name : source_branch
  displayName: Which Branch (e.g. refs/head/foo)
  type: string
  default: $(Build.SourceBranch)

然后我调用一个模板

- template: download_artifact.yml
  parameters:
    artifacts:
    - project: 'XXX'
      pipeline: 291
      artifact: 'artifcat'
      branch: ${{ parameters.source_branch }}
      

我使用模板,因为有大约 30 种不同的工件可以组合。

它在模板中下载提取和操作,但我将简化为仅下载。

parameters:  
  artifacts: []
steps:
  - ${{ each step in parameters.artifacts }}:
    - task: DownloadPipelineArtifact@2
      displayName: '${{ step.artifact }}'
      inputs:
        source: 'specific'
        project: ${{step.project}}
        pipeline: ${{step.pipeline}}
        runVersion: 'latestFromBranch'       
        runBranch: ${{step.branch}}
        artifact: ${{step.artifact}}           
        path: '$(Pipeline.Workspace)\${{ step.artifact }}'

所以最终结果是变量没有在模板中得到解析。我认为这是由于模板在排队时被扩展。有没有人有这种情况的解决方法?

【问题讨论】:

    标签: azure-devops azure-devops-pipelines


    【解决方案1】:

    您在从管道调用模板时对source_branch 参数的引用需要是模板表达式:

     - template: download_artifact.yml
      parameters:
        artifacts:
        - project: 'XXX'
          pipeline: 291
          artifact: 'artifcat'
          branch: ${{ parameters.source_branch }}
    

    另外,如果$(Build.SourceBranch) 在您的参数声明中不起作用,您可以尝试:

    parameters:
    - name : source_branch
      displayName: Which Branch (e.g. refs/head/foo)
      type: string
      default: ${{ variables['Build.SourceBranch'] }}
    

    【讨论】:

    • "A template expression is not allowed in this context" 替换默认值时。还更新了 OP,因为我有你的第一个建议
    • 嗨@gumby,模板中的参数应该在管道编译时展开。根据我的测试和尝试,我们似乎没有任何适用于您的方案的解决方法。
    【解决方案2】:

    模板中的参数应在管道编译时展开。根据我的测试和尝试,我们似乎没有任何适用于您的方案的解决方法。

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      • 2021-07-29
      相关资源
      最近更新 更多