Conditionals
Example Go template with conditionals.
We recommend reviewing Go Templates documentation before attempting to create a template.
If you’re new to YAML we also recommend reviewing the YAML 1.2 spec for validation on syntax.
Overview
From Go template Conditional:
{{if pipeline}}
T1
{{end}}
If the value of the pipeline is empty, no output is generated; otherwise, T1 is executed. The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero. Dot is unaffected.
Tip:
For information on if/else statements see conditional docsSample
Let’s take a look at using a conditional with a variable in a template:
metadata:
template: true
{{$br := vela "BUILD_BRANCH"}}
steps:
- name: test
commands:
- go test ./...
image: {{ .image }}
{{ .pull_policy }}
ruleset:
event: [ push, pull_request ]
# if branch equals main add this step to the final pipeline
{{ if (eq $br "main") }}
- name: build
commands:
- go build
image: {{ .image }}
{{ .pull_policy }}
ruleset:
event: [ push, pull_request ]
{{ end }}
The caller of this template could look like:
version: "1"
templates:
- name: sample
source: github.com/<org>/<repo>/path/to/file/<template>.yml
type: github
steps:
- name: golang
template:
name: sample
vars:
image: golang:latest
pull_policy: "pull: always"
Which means the compiled pipeline for execution on a worker is:
version: "1"
steps:
- name: sample_test
commands:
- go test ./...
image: golang:latest
pull: always
ruleset:
event: [ push, pull_request ]
- name: sample_build
commands:
- go build
image: golang:latest
pull: always
ruleset:
event: [ push, pull_request ]
Last modified September 16, 2022: chore: use main as default (#324) (400aa851)