By default, if you overwrite a variable the value is available only to his job, not to the sequence jobs.
Passing variables between jobs in the same stage is a bit more complex, as it requires working with output variables.
Similarly to the example above, to pass the FOO variable:
- Make sure you give a name to the job, for example
job: firstjob
- Likewise, make sure you give a name to the step as well, for example:
name: mystep
- Set the variable with the same command as before, but adding
;isOutput=true, like: echo "##vso[task.setvariable variable=FOO;isOutput=true]some value"
- In the second job, define a variable at the job level, giving it the value
$[ dependencies.firstjob.outputs['mystep.FOO'] ] (remember to use single quotes for expressions)
A full example:
jobs:
- job: firstjob
pool:
vmImage: 'Ubuntu-16.04'
steps:
# Sets FOO to "some value", then mark it as output variable
- bash: |
FOO="some value"
echo "##vso[task.setvariable variable=FOO;isOutput=true]$FOO"
name: mystep
# Show output variable in the same job
- bash: |
echo "$(mystep.FOO)"
- job: secondjob
# Need to explicitly mark the dependency
dependsOn: firstjob
variables:
# Define the variable FOO from the previous job
# Note the use of single quotes!
FOO: $[ dependencies.firstjob.outputs['mystep.FOO'] ]
pool:
vmImage: 'Ubuntu-16.04'
steps:
# The variable is now available for expansion within the job
- bash: |
echo "$(FOO)"
# To send the variable to the script as environmental variable, it needs to be set in the env dictionary
- bash: |
echo "$FOO"
env:
FOO: $(FOO)
More info you can find here.