skip to Main Content

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


  1. 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:

    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
              name: test
              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'
      dependsOn: DetectChanges
      condition: and(succeeded(), eq(dependencies.DetectChanges.outputs['CheckDevChanges.test.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'
    

    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:

    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
              name: test
              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'
      variables:
        dev_changed: $[dependencies.DetectChanges.outputs['CheckDevChanges.test.dev_changed']]
      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'
    

    Result:

    The value of the variable: dev_changed is true.

    enter image description here

    The value of the variable: dev_changed is false.

    enter image description here

    For more detailed info, you can refer to this doc: Use outputs in a different stage

    Login or Signup to reply.
  2. 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.

    pool:
      vmImage: ubuntu-latest
    
    variables:
      dev_changed: 'false'
    
    stages:
    - stage: DetectChanges
      displayName: 'Detect Changes'
      jobs:
        - job: CheckDevChanges
          displayName: 'Check Changes'
          steps:
            - checkout: self
              persistCredentials: true
    
            - task: Bash@3
              name: 'outputVar'
              inputs:
                targetType: 'inline'
                script: |
                  if [ "aaa" == "aaa" ]; then
                      echo "##vso[task.setvariable variable=dev_changed;isoutput=true]true"
                  else
                      echo "##vso[task.setvariable variable=dev_changed;isoutput=true]false"
                  fi
              displayName: 'Detect Changes in with number'
    
            - bash: |
                echo $(outputVar.dev_changed)
    - stage: Dev
      dependsOn: 
        - DetectChanges
      variables:
        myStageAVar: $[dependencies.DetectChanges.outputs['CheckDevChanges.outputVar.dev_changed']]
      condition: and(succeeded(), eq(variables.myStageAVar, 'true'))
      displayName: 'Deploy Dev Environment'
      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'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search