Apsara DevOps Flow pipeline leverages template syntax to dynamically render pipeline YAML. This approach facilitates the batch configuration of jobs with similar logic and enables on-demand dynamic generation, thereby minimizing repetitive code in pipeline YAML and offering flexible task orchestration.
Prerequisites
To specify template mode, use the first line comment `template=true`
. It supports the use of `{{ }}`
to define templates, adhering to the native `go template`
syntax. Variables defined by `variables`
can be used as parameters during the template rendering process. Typical scenarios include the following:
Scenario 1: Multi-operating system and multi-SDK version compatibility testing scenario
# 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, generate 2×3 Jobs
{{ range $os := .osList}}
{{ range $jdk := $.jdkVersionList}}
{{ $os }}_JDK{{ $jdk }}_job:
name: Test-{{ $os }}-JDK{{ $jdk }}
my_step:
name: Execute command
step: Command
with:
run: |
echo 'test on {{ $os }}-JDK{{ $jdk }}"
{{ end }}
{{ end }}
The example provided traverses two operating systems, ["linux", "windows"]
, and three JDK versions, ["10", "11", "17"]
, utilizing the template's range loop to create six Jobs with identical logic.
Scenario 2: Multi-application 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: 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 task-{{ $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 task-{{ $app }}
needs: build_stage.build_job_{{ $app }}
steps:
build_{{ $app }}:
step: Command
name: Deploy-{{ $app }}
with:
run: "echo start deploy {{ $app }}\n"
{{ end }}
The example above sets up multiple sources for application code, various application build tasks, and numerous deployment tasks tailored to the application. You can dynamically determine the number of applications to deploy using the runtime input environment variable appnames
. The resulting pipeline execution is as follows:
In preview mode, you can set the visual custom variable values in the preview mode tab.