Search code examples
azure-devopsyamlazure-pipelinesazure-pipelines-yaml

Using job output variables in Azure Pipeline condition


I have a multiple pipelines which trigger a deployment pipeline with multiple different deployments. I would like to be able to conditionally run deployments based on what pipeline has triggered the run. To test the logic I'm trying to add a condition to a job, which should prevent running the job when triggering pipeline was build-arm.

I've tried to trigger the following code by running a pipeline called build-arm:

  jobs:
  - job: set_triggering_alias
    displayName: "Set triggering alias"
    steps:
      - task: PowerShell@2
        displayName: 'Set triggering alias'
        name: setTriggeringAlias
        inputs:
          targetType: 'inline'
          script: |
            $triggeringAlias = "$(resources.triggeringAlias)"
            echo "Triggering alias: $triggeringAlias"
            Write-Host "##vso[task.setvariable variable=triggeringAlias;isOutput=true]$triggeringAlias"

  - job: testCondition
    dependsOn: set_triggering_alias
    variables:
      deps: $[convertToJson(dependencies)]
    condition: and(succeeded(), ne(dependencies.set_triggering_alias.outputs['setTriggeringAlias.triggeringAlias'], 'build-arm'))
    steps:
      - task: PowerShell@2
        displayName: 'Test condition'
        name: cond
        inputs:
          targetType: 'inline'
          script: |
            echo "dependencies: $(deps)"

Looking at the logs in both jobs, I can see that the value is build-arm.

Logs from testCondition job

But the condition is never evaluated to false. I've tried to change the setvariable line to Write-Host "##vso[task.setvariable variable=triggeringAlias;isOutput=true]build-arm" In which case the condition does evaluate to false and testCondition job is skipped. I'm guessing something is wrong either with the way I'm accessing resources.triggeringAlias or how I'm setting the variable.


Solution

  • According to your reply to my comment, it seems you have a typo in your condition.

    Replace build-arm:

    condition: and(succeeded(), ne(dependencies.set_triggering_alias.outputs['setTriggeringAlias.triggeringAlias'], 'build-arm'))
    

    With build_arm:

    condition: and(succeeded(), ne(dependencies.set_triggering_alias.outputs['setTriggeringAlias.triggeringAlias'], 'build_arm'))