FBryant87
FBryant87

Reputation: 4614

Azure DevOps pipelines - get build number of previous stage

I have a yml pipeline with 2 stages:

- stage:
- stage:

My second stage needs to refer to the $(Build.BuildNumber) of the previous stage. How can this be achieved? My understanding is output variables are scoped to the same stage and can't be used across-stages.

Trying to pull from stageDependencies:

stages:
  - stage: BuildPublish
    displayName: "Build & Publish"
    jobs:
      - job: BuildPublishJob
        displayName: "Build & Publish"
        steps:
          - script: |
              echo "Recording MSI version"
              echo "##vso[task.setvariable variable=msiVersion;isOutput=true]$(Build.BuildNumber)"
            name: MsiVersion
          - script: echo $(MsiVersion.msiVersion)
            name: echovar

  - stage: DeployInstallerTest
    displayName: "Deploy Installer Test"
    jobs:
    - job:
      displayName: "Deploy Installer Test"
      steps:
      - task: AzurePowerShell@5
        inputs:
          azureSubscription: 'Spektrix Non-Prod'
          ScriptType: 'InlineScript'
          Inline: |
            $msiVersion = stageDependencies.BuildPublish.BuildPublishJob.outputs['MsiVersion.msiVersion']
          azurePowerShellVersion: 'LatestVersion'

Fails with:

##[error]The term 'stageDependencies.BuildPublish.BuildPublishJob.outputs[MsiVersion.msiVersion]' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Upvotes: 2

Views: 1771

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40909

It changed recently:

stages:
- stage: A
  jobs:
  - job: JA
    steps:
    - script: |
        echo "This is job Foo."
        echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes" #The variable doThing is set to true
      name: DetermineResult
    - script: echo $(DetermineResult.doThing)
      name: echovar
  - job: JA_2
    dependsOn: JA
    condition: eq(dependencies.JA.outputs['DetermineResult.doThing'], 'Yes')
    steps:
    - script: |
        echo "This is job Bar."

#stage B runs if DetermineResult task set doThing variable n stage A
- stage: B
  dependsOn: A
  jobs:
  - job: JB
    condition: eq(stageDependencies.A.JA.outputs['DetermineResult.doThing'], 'Yes') #map doThing and check if true
    variables:
      varFromStageA: $[ stageDependencies.A.JA.outputs['DetermineResult.doThing'] ]
    steps:
    - bash: echo "Hello world stage B first job"
    - script: echo $(varFromStageA)

However, please be aware that stageDependencies is not available in condition at stage level. Of course you can use stageDependencies not only in condition.

Upvotes: 4

Related Questions