0
votes

I am new to azure pipelines and am currently experimenting with the passing variables to the later jobs. Here is the current snippet whereby I am trying to extract the project version from the pom file using maven help: evaluate plugin. The pomVersion variable is populated but is not available in the same step or later steps in the second job via the projectVersionDynamic variable.

stages:
  - stage: FirstStage
    jobs:
      - job: FirstJob
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: Bash@3
            inputs:
              targetType: 'inline'
              script: |
                pomVersion=`mvn help:evaluate -Dexpression=project.version -q -DforceStdout`
                echo $pomVersion ##Prints semantic version 2.27.0-SNAPSHOT
                echo '##vso[task.setvariable variable=projectVersionDynamic;isOutput=true]$pomVersion'
                echo '##vso[task.setvariable variable=projectVersionStatic;isOutput=true]2.27.0'
                echo $(Task1.projectVersionDynamic) ##Error message
                echo $projectVersionDynamic ##Is empty
            name: Task1
            displayName: Task1 in JobOne of FirstStage
      - job: SecondJob
        dependsOn: FirstJob
        variables:
          DynVar: $[ dependencies.FirstJob.outputs['Task1.projectVersionDynamic'] ]
          StaVar: $[ dependencies.FirstJob.outputs['Task1.projectVersionStatic'] ]
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: Bash@3
            inputs:
              targetType: 'inline'
              script: |
                echo 'SecondJob'
                echo $(DynVar) ##Is empty
                echo $DynVar  ##Is empty
                echo $(StaVar) ##Prints 2.27.0
                echo $StaVar  ##Is empty
            displayName: Task in JobTwo of FirstStage

Observation: projectVersionDynamic value does not get populated and is not available in the same task or subsequent tasks within or in later jobs / stages. However, the static variable gets populated in projectVersionStatic without any issues.

Is it possible to set dynamic values for user-defined variables in azure pipelines or is it that I am doing something wrong? I see an example here under the Stages section where it seems to be working.

1

1 Answers

0
votes

Variables in Azure Pipelines can be really tricky sometimes. The documentation isn't always crystal-clear on how it's supposed to work. Looking at your example, a cpuple of observations:

  1. echo '##vso[task.setvariable variable=projectVersionDynamic;isOutput=true]$pomVersion' - your single quotes need to be double quotes to expand the value of $pomVersion into the echo statement
  2. (and this is where things get fuzzy): The purpose of task.setvariable is to communicate values to other tasks or jobs. From the documentation: "This doesn't update the environment variables, but it does make the new variable available to downstream steps within the same job."

$variable syntax won't work because the task.setVariable doesn't inject into the running environment - rather, it's a signal to the pipeline to capture the output and store it away for later use. $(variable) syntax won't work because it's expanded just before the job starts, so it's too late to capture here.

If you use my suggestion in point 1) about double-quoting the task.setVariable, you should see the value available in the second job.