I’m seeking assistance from the community to resolve this issue. I’m attempting to modify the value of an externally defined variable, which will be initialized to false by default. However, if the ‘if’ condition is validated, the value should be changed to true, which will then control the subsequent stages.
name: terraform_tests_pipeline_environment
trigger:
paths:
include:
- Environments/Dev/*
variables:
dev_changed: 'false'
stages:
- stage: DetectChanges
displayName: 'Detect Changes'
jobs:
- job: CheckDevChanges
displayName: 'Check Dev Folder Changes'
steps:
- checkout: self
persistCredentials: true
- task: Bash@3
inputs:
targetType: 'inline'
script: |
archivos_modificados='Environments/Dev/readme.md'
if echo "$archivos_modificados" | grep -q "Environments/Dev/"; then
echo "##vso[task.setvariable variable=dev_changed;isoutput=true]true"
else
echo "##vso[task.setvariable variable=dev_changed;isoutput=true]false"
fi
echo $archivos_modificados
echo "$(dev_changed)"
displayName: 'Detect Changes in dev Folder'
- stage: Dev
displayName: 'Deploy Dev Environment'
condition: and(succeeded(), eq(variables['dev_changed'], 'true'))
jobs:
- job: TerraformDeployDev
displayName: 'Terraform Plan and Apply Dev'
pool:
vmImage: 'ubuntu-latest'
steps:
- script: |
echo "Checking for changes in the 'dev' folder"
displayName: 'Terraform Plan and Apply Dev'
I expect that when passing through the if statement, if it is true, the variable will change to true, so that the condition in the other stage can continue. However, when I print the variable’s value, it is not changing
2
Answers
From your description, you need to update the variable value in the DetectChanges stage and then pass the updated value to next stages.
The variable in the DetectChanges stage can not be directly passed to the next stages.
To meet your requirement, you need to use the cross stages variable in Pipeline.
Here is an example:
Or you can use the variables field to define the
dev_changed
variable in Dev stage to get the value passed from the previous stage.Here is an example:
Result:
The value of the variable: dev_changed is true.
The value of the variable: dev_changed is false.
For more detailed info, you can refer to this doc: Use outputs in a different stage
issue 1
: the variable will be available to downstream steps within the same job, not the current step.issue 2
: wrong expression in Dev stage.here is a sample code which been modified from your provided.