Home > Blockchain >  azure pipeline condition always returns false
azure pipeline condition always returns false

Time:09-17

I am trying to check if my stage-specific variable sourceCodeChanged is equal to true. If it is true then the stage should be executed, else it should be skipped. I can do this by using a condition and comparing sourceCodeChanged with true. this however is not working for me. what am I doing wrong?

This is a snippet out of my azure-pipelines.yml:

 - stage: build
   dependsOn: determineChanges
   variables:
     sourceCodeChanged: stageDependencies.determineChanges.checkChanges.outputs['check_changes.SOURCE_CODE_CHANGED']
   condition: and(succeeded(), eq(variables.sourceCodeChanged, 'true'))
   jobs:
      - job: buildBinaries
        displayName: Build Binaries
        steps:
          - bash: echo $(sourceCodechanged)
            displayName: TestOutPutDeleteMe #debugging step

I implemented a debugging step called TestOutPutDeleteMe. when the step TestOutPutDeleteMe gets executed it prints out true, so that means the value gets correctly assigned to sourceCodeChanged. If I try to use the variable sourceCodeChanged in the eq() function it always assigns false to the condition and skips all the steps in the stage.

CodePudding user response:

azure pipeline condition always returns false

That because you are using the condition on the stage level. Please try to using following condition:

 - stage: build
   dependsOn: determineChanges
   condition: eq(Dependencies.determineChanges.outputs['checkChanges.check_changes.SOURCE_CODE_CHANGED'], 'true')

Please note that we need update the stages refer from stageDependencies.stageName.jobName.outputs['stepName.variableName'] To Dependencies.stageName.outputs['jobName.stepName.variableName']

And not use the variable for the value Dependencies.determineChanges.outputs['checkChanges.check_changes.SOURCE_CODE_CHANGED']

My test result:

stages:
  - stage: stageA
    jobs:
    - job: A
      pool:
        name: Default
      steps:
      - task: PowerShell@2
        displayName: "create a variable"
        inputs:
          targetType: 'inline'
          script: |
              Write-Host "##vso[task.setvariable variable=CustomVar;isOutput=true]true"                
        name: CustomVariable

  - stage: stageB
    dependsOn: stageA
    condition: eq(Dependencies.stageA.outputs['A.CustomVariable.CustomVar'], 'true')
    jobs:
    - job: A
      pool:
        name: Default
      steps:
      - task: PowerShell@2
        displayName: "output the variable"
        inputs:
          targetType: 'inline'
          script: |
                  Write-Host "hello world"  

enter image description here

  • Related