您可以在多阶段 devops 管道的条件语句中使用构建标签吗

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

是否可以在后续阶段的条件部分中使用在多阶段管道构建上设置的构建标签?

    ##### 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:

谢谢

据我所知,这在 YAML 中是不可能的,因为没有简单的方法在 YAML 中获取可用的标签。你可以尝试的是 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."

您也可以尝试使用 workaraund,在这种情况下:

  1. 在 powershell 脚本中使用 REST API 获取标签 PUT https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/tags/{tag}?api-version=5.1
  2. 然后使用日志记录命令将您的标签分配给输出变量
  3. 最后在条件中使用输出变量

编辑

所以目前看来这是不可能的,但应该很快就会可用。请检查此 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.

我试过了:

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"

但是我得到了这个错误:

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

不过,我们很快就会推出此功能!