How to Use Template Syntax to Simplify Cloud‑Native Pipeline YAML
This guide explains why static YAML pipelines become unwieldy, introduces Cloud Effect Flow's template syntax based on Go templates, and shows step‑by‑step how to generate multiple jobs dynamically for compatibility testing and per‑application builds, including code examples and runtime configuration.
What is template syntax
Template syntax is a text‑templating language that can combine variables, conditionals, loops, and other constructs so that a YAML file can be rendered dynamically at runtime, producing varied pipeline configurations based on context or external data.
Core usage scenarios
The template engine is activated by adding the comment # template=true at the top of the pipeline YAML and using {{ }} expressions that follow the native Go template syntax. Variables defined under variables serve as parameters for rendering.
Scenario 1: Multi‑OS / Multi‑SDK compatibility testing
When testing code across n operating systems and m SDK versions, a static pipeline would require n × m separate jobs, leading to massive duplication. By defining osList and jdkVersionList variables and iterating with {{ range }}, the same job logic is generated automatically.
# template=true
variables:
- key: osList
type: Object
value: ["linux", "windows"]
- key: jdkVersionList
type: Object
value: ["10", "11", "17"]
stages:
build_stage:
name: 兼容性测试
jobs:
{{ range $os := .osList}}
{{ range $jdk := $.jdkVersionList}}
{{ $os }}_JDK{{ $jdk }}_job:
name: 测试-{{ $os }}-JDK{{ $jdk }}
my_step:
name: 执行命令
step: Command
with:
run: |
echo 'test on {{ $os }}-JDK{{ $jdk }}'
{{ end }}
{{ end }}The pipeline renders six jobs (2 OS × 3 JDK) with a single source of truth.
Scenario 2: Dynamic per‑application build & deploy
In a system with multiple applications, only a subset may need to be built and deployed for a given change. By defining an appnames variable and looping over it, the pipeline creates source definitions, build jobs, and deploy jobs only for the selected apps.
# template=true
variables:
- key: appnames
type: Object
value: ["app1", "app2", "app3"]
sources:
{{ range $app := .appnames }}
repo_{{ $app }}:
type: codeup
name: 代码源名称-{{ $app }}
endpoint: https://yunxiao-test.devops.aliyun.com/codeup/07880db8-fd8d-4769-81e5-04093aaf7b2b/c{{ $app }}.git
branch: master
certificate:
type: serviceConnection
serviceConnection: wwnbrqpihykbiko4
{{ end }}
defaultWorkspace: repo_app1
stages:
build_stage:
name: 构建阶段
jobs:
{{ range $app := .appnames }}
build_job_{{ $app }}:
name: 构建任务-{{ $app }}
sourceOption: ['repo_{{ $app }}']
steps:
build_{{ $app }}:
step: Command
name: 构建-{{ $app }}
with:
run: "echo start build {{ $app }}
"
{{ end }}
deploy_stage:
name: 部署阶段
jobs:
{{ range $app := .appnames }}
deploy_job_{{ $app }}:
name: 部署任务-{{ $app }}
needs: build_stage.build_job_{{ $app }}
steps:
deploy_{{ $app }}:
step: Command
name: 部署-{{ $app }}
with:
run: "echo start deploy {{ $app }}
"
{{ end }}How to enable template mode in Cloud Effect Flow
Open the pipeline YAML editor and add the comment # template=true as the first line.
After switching to template mode, you can write Go‑template expressions inside {{ }} blocks.
Define variables under the variables section; supported types include String, Number, Boolean, and Object. Runtime variables with the same name override the defaults.
Use the “Preview” button to render the pipeline with chosen variable values and verify the generated jobs.
Save and trigger the pipeline. Changing runtime variables (e.g., updating the JDK list) will automatically adjust the generated jobs.
Supported template language features
The engine follows native Go template syntax (see https://pkg.go.dev/text/template ). Common constructs include comments, variable references, conditionals, loops, break, and continue:
{{/* a comment */}}
{{pipeline}} // reference
{{if pipeline}} T1 {{end}} // conditional
{{range pipeline}} T1 {{end}} // loop
{{break}}
{{continue}}Extended functions
Additional helper functions are provided for arithmetic and string manipulation:
# add: integer addition
{{ add 1 2 }} // 3
{{ add 1 2 2 }} // 5
# addf: floating‑point addition
{{ add 1.2 2.3 }} // 3.5
{{ add 1.2 2.3 5 }} // 8.5
# replace: string replace
{{ "Hallo World" | replace "a" "e" }} // Hello World
{{ "I Am Henry VIII" | replace " " "-" }} // I-Am-Henry-VIIIRunning and previewing the pipeline
Configure runtime environment variables in the pipeline settings, preview the rendered YAML to ensure correctness, then save and execute. Adjusting variables (e.g., changing the JDK list to ["17","21"]) will dynamically generate the appropriate number of jobs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
