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

2024-01-11

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

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

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

以下 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

模板测试.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

我的想法是将 Template Deploy.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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果当前阶段有任何作业,如何动态引用 Azure Pipelines 中的先前作业 的相关文章

随机推荐