如果当前阶段有任何作业,如何在 Azure Pipelines 中动态引用以前的作业

How to dynamically reference previous jobs in Azure Pipelines if there are any in the current stage

我正在尝试设置一个 azure yaml 管道,该管道使用两个部署模板分别用于两个作业(测试和部署)。每个阶段的作业应按顺序 运行,因为测试作业会创建部署作业使用的工件。这很好用。

但是,对于一个环境,我将部署分为两个阶段,一个阶段仅 运行 测试,另一个阶段 运行 实际部署。我 运行 遇到的问题是我的部署作业有一个引用测试作业的“dependson”。因此,当我为我的环境添加这两个阶段时,我的管道无效。

我的问题是是否存在某种动态的“依赖”参考,例如“依赖于此阶段之前的任何工作,如果有的话”?

以下 yaml 文件中的代码片段供参考:

Main.yaml

#This first stage would work as it uses both templates in succession

stages:
  - stage: BothTemplates
    displayName: 'Run both templates'
    jobs:
    - template: TemplateTest.yml
    - template: TemplateDeploy.yml #depends on the job in TemplateTest.yml

# The pipeline is invalid because of OnlyDeploy, as the TemplateDeploy.yml depends in the job "Test", which does not exist in the OnlyDeploy-context

  - stage: OnlyTest
    dependsOn: BothTemplates
    displayName: 'Run only test template'
    jobs:
    - template: TemplateTest.yml

  - stage: OnlyDeploy
    dependsOn: OnlyTest
    displayName: 'Run only deploy template'
    jobs:
    - template: TemplateDeploy.yml

TemplateTest.yaml

jobs:
- deployment: Test
  dependsOn: 
  displayName: Test
  continueOnError: false
  strategy:
    runOnce:
      deploy:
        steps:
          #Here the steps for the tests are

当前TemplateDeploy.yaml

jobs:
- deployment: Deploy
  dependsOn: Test
  displayName: Deploy
  continueOnError: false
  strategy:
    runOnce:
      deploy:
        steps:
          #Here the steps for the deployment are

我的想法是将 TemplateDeploy.yaml 改成这样:

jobs:
- deployment: Deploy
  dependsOn: previousJob() #Wait until previous job in stage has finished, if there are any
  displayName: Deploy
  continueOnError: false
  strategy:
    runOnce:
      deploy:
        steps:
          #Here the steps for the deployment are

因为阶段之间已经存在依赖关系

  - stage: OnlyTest
    dependsOn: BothTemplates
    displayName: 'Run only test template'
    jobs:
    - template: TemplateTest.yml

  - stage: OnlyDeploy
    dependsOn: OnlyTest
    displayName: 'Run only deploy template'
    jobs:
    - template: TemplateDeploy.yml

我认为你实际上不需要依赖于 TemplateDeploy.yml 上的工作,但如果你想让它依赖于以前的工作,你可以使用参数

来实现

parameters:
- name: makeExplicitDependency
  displayName: 'Make excplicit job dependency'
  type: boolean
  default: true

jobs:
- deployment: Deploy
  ${{ if eq(parameters.makeExplicitDependency, true) }}:
     dependsOn: Test
  displayName: Deploy
  continueOnError: false
  strategy:
    runOnce:
      deploy:
        steps:
          #Here the steps for the deployment are

然后:


stages:
  - stage: BothTemplates
    displayName: 'Run both templates'
    jobs:
    - template: TemplateTest.yaml
    - template: TemplateDeploy.yaml #depends on the job in TemplateTest.yml

  - stage: OnlyTest
    dependsOn: BothTemplates
    displayName: 'Run only test template'
    jobs:
    - template: TemplateTest.yaml

  - stage: OnlyDeploy
    dependsOn: OnlyTest
    displayName: 'Run only deploy template'
    jobs:
    - template: TemplateDeploy.yaml
      parameters:
        makeExplicitDependency: false

所以删除 dependsOn 就像您预期的那样 dependsOn: previousJob() #Wait until previous job in stage has finished, if there are any