How to use predefined resource variables in Azure DevOps Pipeline Yaml?

104 views Asked by At

I have a pipeline yaml that is triggered based on the completion of another pipeline. I want to access the properties of the trigger resource to get the runName/buildNumber and use it as a variable in the second pipeline. How can I do this?

The pipeline yaml has the following format:

resources:    
  pipelines:
  - pipeline: OfficialReleaseBuild
    source: Release-Official
    trigger: 
      branches:
       include:
         - release/*
      tags:
        - weeklyRelease

This is working fine, and it is triggering this pipeline whenever a build from the Release-Official pipeline with the 'weeklyRelease' tagcompletes

However, since this pipeline can be triggered manually or automatically, there is variable that I want to set based on the properties that are available from the ResourceTrigger:

https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/resources-pipelines-pipeline?view=azure-pipelines

variables:
    # package version to publish
  - name: version
    ${{ if eq( variables['resources.triggeringAlias'], 'OfficialReleaseBuild' ) }}: 
      value: variables['resources.pipeline.OfficialReleaseBuild.runName']
    ${{ else }}: 
      value: "${{ parameters.packageVersion }}"

However, it is not working. Is there anything I am missing? It's always defaulting to parameters.packageVersion. How can I access the properties from the resource trigger?

1

There are 1 answers

0
Shamrai Aleksander On

This is because ${{ if eq( variables['resources.triggeringAlias'], 'OfficialReleaseBuild' ) }}: expression is processed at compile moment but resources.triggeringAlias is a run-time variable. You may use the logging command: SetVariable: Initialize or modify the value of a variable

variables:
    # package version to publish
  - name: version
      value: "${{ parameters.packageVersion }}"

JobsAndStepsSection:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      if ('OfficialReleaseBuild' -eq $(resources.triggeringAlias))
      {
            Write-Host "##vso[task.setvariable variable=version;]$(resources.pipeline.OfficialReleaseBuild.runName)"
      }