3
votes

I have two Azure DevOps yaml pipelines, one for building and then one deploying (yes I know I can do both in a single pipeline, but for my use case I need to split it).

The first pipeline builds artifacts and I have then configured the second pipeline to be triggered when the first complete to deploy those artifacts.

I am trying to get my second pipeline to have the same name as the first pipeline, to make it easy to correlate the two.

So, if the first pipeline runs and is automatically named '1.0.0', I want the second pipeline to also be named '1.0.0' when it is triggered.

My first pipeline (below) starts as follows and has an automatically generated semantic name.

# 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

My second pipeline starts:

# 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"

I've added '-test' to the name of the second pipeline in order to debug, as when the pipeline is triggered by the first completing the name it assigned is just '-test'.

However, in the logging job, it correctly prints out the name of the previous build, specifically these two lines both print out '1.0.0'

    Write-Host "originalVersion: $(originalVersion)"
    Write-Host "pipelineID: $(resources.pipeline.original.pipelineID)"

but using that variable in the 'name:' property results in an empty value.

In the first pipeline, I am able to use variables in the build name generated from expressions (the incrementing version number) but in the second pipeline, the variable substitution does appear to work.

Can anyone spot if I'm doing something daft, or is this a bug/limitation in Azure DevOps YAML?

1

1 Answers

0
votes

Because the name value is evaluated before the variables are created. as a workaround you can add a simple script that updates the name:

- script: echo "##vso[build.updatebuildnumber]$(originalVersion)"

Result:

enter image description here