繁体   English   中英

如何将调度程序和触发器添加到 Azure DevOps 管道中的嵌套模板?

[英]How to add Scheduler and Trigger to a nested template in Azure devops pipeline?

我是 Azure DevOps 的新手,希望得到您的建议。 我有一个主要的 azure-pipeline.yml,它引用了一个嵌套模板 (azure-pipeline-e2e.yml) 文件。 基本上,当主 azure-pipeline.yml 运行时,它会触发嵌套模板,其中如果分支不是主分支,嵌套模板作业将被跳过。

询问:

我希望嵌套模板作为夜间计划的一部分运行。 我还希望这个嵌套管道没有触发器(触发器:无)......请注意当前设置工作正常。 仅当我在嵌套模板中添加调度程序作业和触发器作业时,主要的 azure-pipeline.yml 抱怨调度程序和触发器命令未知? 请告知如何在嵌套管道中同时使用触发器和调度程序,以便主管道不会抱怨

当前设置:主要的 azure-pipeline.yml 文件

 trigger: - master pool: name: EDEA stages: - stage: dev jobs: - job: build_test_deploy_UI_to_DEV steps: - task: NodeTool@0 displayName: 'Install Node.js' inputs: versionSpec: '12.x' etc..... - stage: e2e condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master')) displayName: 'Run e2e Tests' jobs: - template: 'azure-pipelines-e2e.yml' - stage: dtc jobs: - job: build_and_deploy_UI_to_DTC steps: - task: NodeTool@0 displayName: 'Install Node.js' inputs: versionSpec: '12.x' etc.....

嵌套模板(azure-pipeline-e2e.yml)

 jobs: - job: run_e2e_tests condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master')) steps: - task: SecretsManagerGetSecret@1 displayName: Get AWS secrets inputs: awsCredentials: 'project.dev.Aws' regionName: 'eu-central-1' secretIdOrName: 'secretname' variableName: 'tempvariable'

基本上,在上面的嵌套模板中,当我添加调度程序和触发器作业时,嵌套模板不会抱怨。 但是主要的 azure 管道抱怨 Scheduler 和 Trigger 命令未知

建议:azure-pipeline-e2e.yml

 schedules: - cron: "0 0 * * *" displayName: Daily midnight build branches: include: - master always: true trigger: none jobs: - job: run_e2e_tests condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master')) steps: - task: SecretsManagerGetSecret@1 displayName: Get AWS secrets inputs: awsCredentials: 'project.dev.Aws' regionName: 'eu-central-1' secretIdOrName: 'secretname' variableName: 'tempvariable'

主管道抱怨triggerschedule参数,因为它只需要在嵌套模板中定义的作业。 在您提供的当前示例中,通过将模板中的这 2 个参数定义到主管道中,如下所示:

[...]
- stage: e2e
  condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master')) 
  displayName: 'Run e2e Tests'
  jobs:
  schedules:
   - cron: "0 0 * * *"
     displayName: Daily midnight build
     branches:
       include:
       - master
     always: true
  trigger: none
  - job:
[...]

...这显然不是正确的语法。

最好的解决方法可能是在主管道中包含schedule ,并为需要在构建被触发时执行的阶段添加运行条件eq(variables['Build.Reason'], 'Schedule')日程。 结果将类似于:(main) azure-pipelines.yml

 schedules:
 - cron: "0 0 * * *"
   displayName: Daily midnight build
   branches:
     include:
     - master
   always: true
 trigger:
 - master
 pool:
   name: EDEA

 stages:
   [...]
 - stage: e2e
   condition: or(and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master')), eq(variables['Build.Reason'], 'Schedule')) 
   displayName: 'Run e2e Tests'
   jobs:
   - template: 'azure-pipelines-e2e.yml'

您还需要在嵌套模板中使用相同的运行条件。

触发器应在主 yaml azure 管道中定义,而不是在嵌套模板 yaml 中定义。

模板 yaml 将与主 yaml 管道一起触发。 您所能做的就是通过跳过阶段来防止执行阶段。 您可以通过将条件eq(variables['Build.Reason'], 'Schedule')到需要按 LJ 提到的计划执行的阶段来实现这一点。有关更多信息,请参阅条件

如果在计划构建中只需要运行 e2e 阶段,您还可以尝试创建另一个 yaml 管道来运行模板 yaml 中的作业。 然后就可以在新的yaml管道中定义调度的触发器,这个管道只在调度上触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM