Search code examples
yamlazure-pipelines-yaml

Azure Devops Yaml Pipeline - stage condition not working


I have a problem in a yaml pipeline and hope to get some help.

I want to skip a whole stage depending on a value of a variable which is either created in a prior stage or created before the stages, but changed in a prior stage.

stages:
- stage: Variables
  displayName: Determine deploy target

  jobs:
  - job: setPrVariable
    variables:
      allLabels: 'none'
    steps:
    - script: echo $(allLabels)
    - bash: |
       echo "##vso[task.setvariable variable=labelOutputVar;isoutput=true]$(allLabels)"
      name: passOverPRLabels

  - job: determineDeployTarget
    dependsOn: setPrVariable
    variables:
      allPrLabels: $[ dependencies.setPrVariable.outputs['passOverPRLabels.labelOutputVar'] ]

    steps:
    - bash: |
       echo "##vso[task.setvariable variable=deployTarget]unset"
    - task: PowerShell@2
      inputs: 
       targetType: 'inline'
       script: |
        Write-Host "##vso[task.setvariable variable=deployTarget]none"
      condition: eq(1, 1)                                                                 )

    - bash: |
       echo "##vso[task.setvariable variable=determinedDeployTarget;isoutput=true]$(deployTarget)"
      name: passOverDeployTarget2

- stage: Build_For_Dev_Systems
  dependsOn: Variables
  displayName: Build for deploy of a Dev System
  variables: 
  - name: stage2DeployTarget
    value: $[stageDependencies.Variables.determineDeployTarget.outputs['passOverDeployTarget2.determinedDeployTarget']]
  condition: (variables.stage2DeployTarget, 'none')
  jobs:
  - job: PassOverTest
    condition: ne(variables.stage2DeployTarget, 'none')
    steps:
    - script: echo $(stage2DeployTarget)

The condition in the "job: PassOverTest" works and the job gets skipped, but the condition doesn't work for the stage. I assume that even though I put the condition after the variables part, yaml will still use the condition first.

I then did some research and found that the stage condition should look different, but this doesn't work as well:

condition: ne(dependencies.Variables.outputs[passOverDeployTarget2.passOverDeployTarget2.determinedDeployTarget], 'none')

or this:

condition: 
ne(dependencies.Variables.outputs['determineDeployTarget.determineDeployTarget.passOverDeployTarget2.determinedDeployTarget'], 'none')

Any help is highly appreciated


Solution

  • to be in stage condition use the following syntax

    - stage: Variablesstage
      displayName: Determine deploy target
    
      jobs:
      - job: setPrVariable
        variables:
          allLabels: 'none'
        steps:
        - script: echo $(allLabels)
        - bash: |
           echo "##vso[task.setvariable variable=labelOutputVar;isoutput=true]$(allLabels)"
          name: passOverPRLabels
    
      - job: determineDeployTarget
        dependsOn: setPrVariable
        variables:
          allPrLabels: $[ dependencies.setPrVariable.outputs['passOverPRLabels.labelOutputVar'] ]
    
        steps:
        - bash: |
           echo "##vso[task.setvariable variable=deployTarget]unset"
        - task: PowerShell@2
          inputs: 
           targetType: 'inline'
           script: |
            Write-Host "##vso[task.setvariable variable=deployTarget]none"
          condition: eq(1, 1)                                                                 )
    
        - bash: |
           echo "##vso[task.setvariable variable=determinedDeployTarget;isoutput=true]$(deployTarget)"
          name: passOverDeployTarget2
    
    - stage: Build_For_Dev_Systems
      dependsOn: Variables
      displayName: Build for deploy of a Dev System
      condition: eq(dependencies.Variablesstage.outputs['determineDeployTarget.passOverDeployTarget2.determinedDeployTarget'],'none')
      jobs:
      - job: PassOverTest
        steps:
        - script: echo $(stage2DeployTarget)
    

    reference:https://travisgosselin.com/azure-pipelines-passing-variables-between-stages/