首页 > 解决方案 > Optional job templates in YAML Pipelines

问题描述

Is it possible to optionally include templates based on some kind of template expression? Specifically, I want my top-level definition in azure-pipelines.yml to call out which build job templates to use in an included stage template:

azure-pipelines.yml :

stages:
- template: generic-build-stage.yml  # Template reference
  parameters:
    # Example of optional build templates to use
    buildTypes: [SpecificBuildJobs1, SpecificBuildJobs3, SpecificBuildJobs4]

generic-build-stage.yml :

parameters:
  buildTypes: ???

stages:
- stage: generic_build
  jobs:
  ${{ }} # ???? What goes here to include the appropriate templates
  - template: ???

The template expression above would ideally expand to this:

  jobs:
  - template: specific-build-jobs1.yml
  - template: specific-build-jobs3.yml
  - template: specific-build-jobs4.yml

Edit: The "Iterative insertion" example in the docs seems to suggest that some form of dynamic, parse-time insertion is possible.

标签: azure-devops

解决方案


It seems to be impossible, for the template reference is resolved at parse time.

You may have to set multiple templates for main pipeline, and set the value for buildTypes as the specific template name for job templates, and in generic-build-stage.yml use - template:${{parameters.buildTypes}}.yml to call corresponding job template;

Azure-pipelines.yml:

stages:
- template: generic-build-stage.yml 
  parameters:
      buildTypes:specific-build-jobs1  

- template: generic-build-stage.yml 
  parameters:
      buildTypes:specific-build-jobs3  

generic-build-stage.yml

parameters:
  buildTypes: ""

stages:
- stage: generic_build
  jobs:
  - template: ${{parameters.buildTypes}}.yml

推荐阅读