I have a question regarding variables and their scopes. I want to set up a pipeline with multiple jobs and each job contains the same tasks and I want to use a template for them. I also have job variables in them which I want to manipulate during the job runtime. Since the Job is defined in a template all variable names are the same. Usually I use the following command to set the new value for a variable.
Write-Output ("##vso[task.setvariable variable=job_variable1;]$newValue")
I don't really know if this causes all variables from all jobs being set with the content of $newVariable or if this logging command just sets the variable from the current job.
All jobs run in parallel and if this would also set the variable from other jobs it could cause side effects in other jobs.
I tested this problem with the following yaml pipeline and as far as I can see it looks good and the new value which is set in BuildJob1 isn't echoed in BuildJob2 but I don't really know if it's a valid test.
trigger: none
jobs:
- job: BuildJob1
variables:
job_variable1: VariableFromJob1 # this is only available in BuildJob
steps:
- task: PowerShell@2
displayName: "Echo and set job variable"
inputs:
targetType: inline
script: '
echo "initial job variable";
echo "$(job_variable1)";
$newValue = "new Variable for VariableFromJob1";
Write-Output ("##vso[task.setvariable variable=job_variable1;]$newValue");
'
- task: PowerShell@2
displayName: "Echo new job variable"
inputs:
targetType: inline
script: '
echo "new job variable";
echo "$(job_variable1)";
'
- job: BuildJob2
variables:
job_variable1: VariableFromJob2 # this is only available in BuildJob
steps:
- task: PowerShell@2
displayName: "Echo and set job variable"
inputs:
targetType: inline
script: '
echo "initial job variable";
Start-Sleep -s 45;
echo "$(job_variable1)";
$newValue = "newJobValue from job 2";
Write-Output ("##vso[task.setvariable variable=job_variable1;]$newValue");
'
- task: PowerShell@2
displayName: "Echo new job variable"
inputs:
targetType: inline
script: '
echo "new job variable";
echo "$(job_variable1)";
'
In Job2 I waited 45 seconds to be sure that the job variable is set in Job1 before printing its content.