Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of how to pass variables between Azure DevOps stages
pool:
vmImage: windows-latest
variables:
- name: system.debug
value: true
stages:
- stage: Stage1
jobs:
- job: Job1
steps:
- checkout: none
- pwsh: |
# declare the variable as standard job and stage variable
$MyPowerShellVar = "1.2.3.4"
write-host "Local var is $MyPowerShellVar"
write-host "##vso[task.setvariable variable=standardvar]$MyPowerShellVar"
write-host "##vso[task.setvariable variable=stagevar;isOutput=true]$MyPowerShellVar"
name: myscript
- pwsh: |
# accessing the two variable in the same job
Write-Host "Standard Var '$(standardvar)'"
Write-Host "Stage Var inside the same job '$(myscript.stagevar)'"
# to access the stage variable in a different job we need to create a local variable'alias'
# and this job must be set to depend on the first job
- job: Job2
variables:
job2alias: $[dependencies.Job1.outputs['myscript.stagevar']]
dependsOn: Job1
steps:
- checkout: none
- pwsh: |
Write-Host "Stage Var inside the same stage but a different job '$(job2alias)'"
# to access the stage variable in a different stage we need to create a local variable'alias'
# not the form is different to that used in job2 above
# and this job must be set to depend on the first stage
- stage: Stage2
dependsOn: Stage1
jobs:
- job: Job3
variables: # add the alias
job3alias: $[stagedependencies.Stage1.Job1.outputs['myscript.stagevar']]
steps:
- checkout: none
- pwsh: |
Write-Host "Stage Var inside a different stage '$(job3alias)'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment