如何在触发管道中使用以前的Azure DevOps yaml管道名称?

Mar*_*otH 5 azure-devops azure-pipelines

我有两个 Azure DevOps yaml 管道,一个用于构建,一个用于部署(是的,我知道我可以在一个管道中完成这两项工作,但对于我的用例,我需要将其拆分)。

第一个管道构建工件,然后我将第二个管道配置为在第一个管道完成部署这些工件时触发。

我试图让第二个管道与第一个管道具有相同的名称,以便轻松将两者关联起来。

因此,如果第一个管道运行并自动命名为“1.0.0”,我希望第二个管道在触发时也被命名为“1.0.0”。

我的第一个管道(如下)按如下方式启动,并具有自动生成的语义名称。

# Generate build name - see variables section below
name: '$(Version.MajorMinor).$(Version.Revision)$(Version.Suffix)'

# These are Continuous Integration Triggers for automatically starting a new pipeline run when there is a check in for any of the matching branches 
trigger:
- trunk
- feature/*
- task/*
- bug/*

# Use this section to set variables that are specific to the Microservice / Solution being processed
variables:
  Version.MajorMinor: 1.0                                         # Major = non-backward compatible version increment, Minor = backward compatible version increment 
  Version.Revision: $[counter(variables['Version.MajorMinor'],0)] # Increments automatically every build, resets if Version.MajorMinor is changed

  # Set the suffix of the version number depending on whether this is trunk, pr or other branch
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/trunk') }}: 
    Version.Suffix: ''      # trunk
  ${{ if eq(variables['Build.Reason'], 'PullRequest') }}: 
    Version.Suffix: '-pr'   # pull request
  ${{ if and(ne(variables['Build.SourceBranch'], 'refs/heads/trunk'), ne(variables['Build.Reason'], 'PullRequest')) }}: 
    Version.Suffix: '-pre'  # pre-release
Run Code Online (Sandbox Code Playgroud)

我的第二条管道开始:

# Use the name of the first pipeline
name: '$(originalVersion)-test'
    
resources:
  pipelines:
  - pipeline: original             # reference to build pipeline
    source: 'DevOps - CI'
    branch: trunk 
    
variables:
  # variable contains the name of the build pipeline
  originalVersion: $[variables['resources.pipeline.original.runName']]

stages:
- stage: start   
  jobs:

  - job: LogJob
    displayName: 'Logging'
    steps:
    - checkout: none   
    - pwsh: |
        Write-Host "originalVersion: $(originalVersion)"
        Write-Host "pipelineID: $(resources.pipeline.original.pipelineID)"
        Write-Host "runName: $(resources.pipeline.original.runName)"
        Write-Host "runID: $(resources.pipeline.original.runID)"
        Write-Host "runURI: $(resources.pipeline.original.runURI)"
        Write-Host "sourceBranch: $(resources.pipeline.original.sourceBranch)"
        Write-Host "sourceCommit: $(resources.pipeline.original.sourceCommit)"
        Write-Host "sourceProvider: $(resources.pipeline.original.sourceProvider)"
        Write-Host "requestedFor: $(resources.pipeline.original.requestedFor)"
        Write-Host "requestedForID: $(resources.pipeline.original.requestedForID)"

      displayName: "PoSh: Dependant Pipeline Details"
Run Code Online (Sandbox Code Playgroud)

我已将“-test”添加到第二个管道的名称中以便进行调试,因为当第一个管道完成时触发管道时,它分配的名称只是“-test”。

但是,在日志记录作业中,它正确打印出先前构建的名称,特别是这两行都打印出“1.0.0”

    Write-Host "originalVersion: $(originalVersion)"
    Write-Host "pipelineID: $(resources.pipeline.original.pipelineID)"
Run Code Online (Sandbox Code Playgroud)

但在“name:”属性中使用该变量会导致空值。

在第一个管道中,我可以在从表达式(递增版本号)生成的构建名称中使用变量,但在第二个管道中,变量替换似乎确实有效。

任何人都可以发现我是否在做一些愚蠢的事情,或者这是 Azure DevOps YAML 中的错误/限制吗?

Sha*_*zyk 2

因为name值是在variables创建之前评估的。作为解决方法,您可以添加一个简单的脚本来更新名称:

- script: echo "##vso[build.updatebuildnumber]$(originalVersion)"
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述