How to pass a variable value between Azure Pipeline jobs (within the same stage)?
Dheeraj Gambhir,
Since the jobs operate in different namespaces, passing variable values between them is unlike passing a variable value from one function to another.
In the example below, $app_Token is the variable name that needs to be sent from one job to another job but within the same stage.
This issue can be resolved by dynamically storing the variable in the variable library and retrieving its value from there. The task.setvariable command, which is illustrated below, is required to store variables using PowerShell in the Azure Pipeline library.
Full example:
# Maven
# Build your Java project and run tests with Apache Maven.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/java
trigger:
- master
stages:
- stage: VariableUpdateAutomation
jobs:
- job: Set_AppTokenValue
pool:
vmImage: ubuntu-latest
steps:
- task: PowerShell@2
name: Set_AppTokenValue
inputs:
targetType: 'inline'
script: |
$app_Token = '$(APPTOKEN)' #Reading APPTOKEN variable value
Write-Output "The app token value till now is: $app_Token"
$app_Token = "ABCDE12345"
echo "##vso[task.setvariable variable=AppToken;isOutput=true]$app_Token"
#Updating app_Token value from ABCDE12345 with the value that we have in variables
- job: Get_AppTokenValue
dependsOn: Set_AppTokenValue
variables:
- name: BSAppToken
value: $[ dependencies.Set_AppTokenValue.outputs['Set_AppTokenValue.AppToken'] ]
pool:
vmImage: ubuntu-latest
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
echo "Value of AppToken in this Job is = $(BSAppToken)"
Note: This task variable only pertains to that stage. The task variable value cannot be used directly in the new stage.
If you find this post helpful, please share them with your colleagues and friends.