All Products
Search
Document Center

Alibaba Cloud DevOps:Use the template syntax to dynamically configure a pipeline

Last Updated:Mar 31, 2025

Alibaba Cloud DevOps Flow allows you to use the template syntax to dynamically render the pipeline YAML, which enables jobs with similar or identical logic to be configured in batches and generated dynamically on demand. This approach reduces repetitive code in pipeline YAML files and allows for flexible job orchestration.

Prerequisites

The template mode is specified by the first-line comment template=true, templates defined with {{ }} are supported, following the native go template syntax, and variables defined with variables are supported as parameters in the template rendering process. The following examples show some typical sample scenarios:

Scenario 1: Test compatibility across multiple operating systems and SDK versions

# template=true
variables:
  - key: osList
    type: Object
    value: ["linux", "windows"]
  - key: jdkVersionList
    type: Object
    value: ["10", "11", "17"]

stages:
  build_stage:
    name: compatibility testing
    jobs: # Double loop, 2*3 jobs are generated.
      {{ range $os := .osList}}
        {{ range $jdk := $.jdkVersionList}}
        {{ $os }}_JDK{{ $jdk }}_job:
                   name: Test-{{ $os }}-JDK{{ $jdk }}
                   my_step:
                     name: Run a command
                     step: Command
                     with:
                       run: |
                         echo 'test on {{ $os }}-JDK{{ $jdk }}"
        {{ end }}
        {{ end }} 

The preceding example iterates over two operating systems, ['linux', 'windows'], and three JDK versions, ['10', '11', '17'], and uses template range loops to generate six jobs with identical logic.

image

Scenario 2: Multiple applications for dynamic on-demand build and deployment

# template=true
variables:
  - key: appnames
    type: Object
    value: ["app1", "app2", "app3"]

sources:
  {{ range $app := .appnames }}
  repo_{{ $app }}:
    type: codeup
    name: the name of the code source-{{ $app }}
    endpoint: https://codeup.aliyun.com/07880db8-fd8d-4769-81e5-04093aaf7b2b/{{ $app }}.git
    branch: master
    certificate:
      type: serviceConnection
      serviceConnection: wwnbrqpihykbiko4
  {{ end }}

defaultWorkspace: repo_app1

stages:
  build_stage:
    name: build stage
    jobs:
      {{ range $app := .appnames }}
      build_job_{{ $app }}:
        name: build_job -{{ $app }}
        sourceOption: ['repo_{{ $app }}']
        steps:
          build_{{ $app }}:
            step: Command
            name: build -{{ $app }}
            with:
              run: "echo start build {{ $app }}\n"
      {{ end }}
  deploy_stage:
    name: deployment stage
    jobs:
      {{ range $app := .appnames }}
      deploy_job_{{ $app }}:
        name: deploy_job-{{ $app }}
        needs: build_stage.build_job_{{ $app }}
        steps:
          build_{{ $app }}:
            step: Command
            name: deploy-{{ $app }}
            with:
              run: "echo start deploy {{ $app }}\n"
      {{ end }}

In the preceding example, multiple application code sources, build jobs, and deployment jobs are configured. You can dynamically determine the number of applications to deploy based on the runtime environment variable appnames. The following figure shows the result:

Note

You can specify visual custom variables in the preview mode tab.

image