Alibaba Cloud DevOps Flow supports template syntax to dynamically render pipeline YAML, enabling batch configuration and on-demand generation of jobs with similar logic. This reduces repetitive YAML code and enables flexible job orchestration.
Prerequisites
Enable template mode by adding the first-line comment template=true. Templates are defined with {{ }} using the native go template syntax, and variables serve as parameters during template rendering. The following examples demonstrate typical 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: # A nested loop that generates 2x3 jobs.
{{ 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 }}
This example iterates over two operating systems, ["linux", "windows"], and three JDK versions, ["10", "11", "17"], using the range loop to generate six jobs with identical logic.
After the pipeline runs, all six jobs execute successfully, with each job taking approximately 1 second to complete.
Scenario 2: Dynamically build and deploy multiple applications on demand
# template=true
variables:
- key: appnames
type: Object
value: ["app1", "app2", "app3"]
sources:
{{ range $app := .appnames }}
repo_{{ $app }}:
type: codeup
name: Code source name-{{ $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: Deployment job - {{ $app }}
needs: build_stage.build_job_{{ $app }}
steps:
build_{{ $app }}:
step: Command
name: Deploy - {{ $app }}
with:
run: "echo start deploy {{ $app }}\n"
{{ end }}
This example configures multiple application code sources, build jobs, and deployment jobs. You can dynamically determine the number of applications to deploy based on the runtime variable appnames.
You can specify visual custom variables in the preview mode tab.
The pipeline run completes successfully in 14 seconds. The three applications (app1, app2, and app3) each complete their build jobs in the build stage and their deployment jobs in the deployment stage.