Search code examples
azure-devopsazure-pipelinesmultistage-pipeline

Can you use build tags in conditions statements in multi-stage devops pipelines


Is it possible to use the build tags set on a multi-stage pipeline build, in the condition section of a later stage?

    ##### task in build stage #####
    - task: YodLabs.VariableTasks.AddTag.AddTag@0
      displayName: Adding environment tag to build
      inputs:
        tags: |
          deploy
          $(DEPLOY_ENV)
  #### some later stage ####
  - stage: deploy
    displayName: deploy
    condition: |
      and(
        succeeded(),
        #Is there something I can put here to condition on tags
      )
    jobs:

Thanks


Solution

  • From what I know this is not possible with YAML yet, because there is no easy way to get tags availabe in YAML. What you can try is output variable

    jobs:
    - job: Foo
      steps:
      - script: |
          echo "This is job Foo."
          echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes" #The variable doThing is set to true
        name: DetermineResult
    - job: Bar
      dependsOn: Foo
      condition: eq(dependencies.Foo.outputs['DetermineResult.doThing'], 'Yes') #map doThing and check if true
      steps:
      - script: echo "Job Foo ran and doThing is true."
    

    You can also try with workaraund which is in this case:

    1. Fetch tags using REST API in powershell script PUT https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/tags/{tag}?api-version=5.1
    2. Then assign your tags to output variables using logging commands
    3. And finally use output variable in condition

    EDIT

    So it looks that this is not possible at the moment but should be available soon. Please check this GitHub issue.

    Output variables may now be used across stages in a YAML-based pipeline. This helps you pass useful information, such as a go/no-go decision or the ID of a generated output, from one stage to the next. The result (status) of a previous stage and its jobs is also available.

    Output variables are still produced by steps inside of jobs. Instead of referring to dependencies.jobName.outputs['stepName.variableName'], stages refer to stageDependencies.stageName.jobName.outputs['stepName.variableName']. Note: by default, each stage in a pipeline depends on the one just before it in the YAML file. Therefore, each stage can use output variables from the prior stage. You can alter the dependency graph, which will also alter which output variables are available. For instance, if stage 3 needs a variable from stage 1, it will need to declare an explicit dependency on stage 1.

    I tried it now:

    stages:
    - stage: A
      jobs:
      - job: JA
        steps:
        - script: |
            echo "This is job Foo."
            echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes" #The variable doThing is set to true
          name: DetermineResult
    
    # stage B runs if A fails
    - stage: B
      condition: eq(stageDependencies.A.JA.outputs['DetermineResult.doThing'], 'Yes') #map doThing and check if true
      jobs:
      - job: JB
        steps:
        - bash: echo "Hello world stage B first job"
    

    but I got this error:

    An error occurred while loading the YAML build pipeline. Unrecognized value: 'stageDependencies'. Located at position 4 within expression: eq(stageDependencies.A.JA.outputs['DetermineResult.doThing'], 'Yes'). For more help, refer to https://go.microsoft.com/fwlink/?linkid=842996

    However, this feature can be soon with us!