Skip to content

Instantly share code, notes, and snippets.

@rfennell
Created May 5, 2022 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfennell/abb41fa17f2c4403102c72c9360f59d4 to your computer and use it in GitHub Desktop.
Save rfennell/abb41fa17f2c4403102c72c9360f59d4 to your computer and use it in GitHub Desktop.
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