Conditionals
Example Starlark template with conditionals.
We recommend reviewing Starlark Spec before attempting to create a template.
Overview
From Starlark Conditional Expressions:
"yes" if enabled else "no"
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:
def main(ctx):
steps = [
{
"name": "test",
"image": ctx["vars"]["image"],
"commands": [
"go test ./..."
],
"pull": ctx["vars"]["pull_policy"],
"ruleset": {
"event": [
"push",
"pull_request"
]
}
}
]
# if branch equals main add this step to the final pipeline
if ctx["vela"]["build"]["branch"] == "main":
steps.append(
{
"name": "build",
"image": ctx["vars"]["image"],
"commands": [
"go build"
],
"pull": ctx["vars"]["pull_policy"],
"ruleset": {
"event": [
"push",
"pull_request"
]
}
}
)
return {
'version': '1',
'steps': steps,
}
The caller of this template could look like:
version: "1"
templates:
- name: sample
source: github.com/<org>/<repo>/path/to/file/<template>.star
type: github
format: starlark
steps:
- name: sample
template:
name: golang
vars:
image: golang:latest
pull_policy: "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)