Skip to main content

Substitution

Vela imports a substitution library to provide the ability to expand, or substitute, repository and build metadata to facilitate dynamic pipeline configurations.

String Operations

SyntaxExample with OutputDescription
${VAR^}VAR=example; echo ${VAR^}ExampleConverts the first character of the variable to uppercase
${VAR^^}VAR=example; echo ${VAR^^}EXAMPLEConverts all characters of the variable to uppercase
${VAR,}VAR=EXAMPLE; echo ${VAR,}eXAMPLEConverts the first character of the variable to lowercase
${VAR,,}VAR=EXAMPLE; echo ${VAR,,}exampleConverts all characters of the variable to lowercase
${VAR:position}VAR=example; echo ${VAR:1}xampleExtracts a substring from the variable starting at the position
${VAR:position:length}VAR=example; echo ${VAR:2:3}ampExtracts a substring from the variable starting at the position with the specified length
${VAR#substring}VAR=exampleexample; echo ${VAR#exa}mpleexampleRemoves the shortest match of substring from the start of the variable
${VAR##substring}VAR=exampleexample; echo ${VAR##*exa}mpleRemoves the longest match of substring from the start of the variable
${VAR%substring}VAR=exampleexample; echo ${VAR%mple}exampleexaRemoves the shortest match of substring from the end of the variable
${VAR%%substring}VAR=exampleexample; echo ${VAR%%mple*}exaRemoves the longest match of substring from the end of the variable
${VAR/substring/replacement}VAR=exampleexample; echo ${VAR/exam/ap}appleexampleReplaces the first match of substring with replacement in the variable
${VAR//substring/replacement}VAR=exampleexample; echo ${VAR//exam/ap}appleappleReplaces all matches of substring with replacement in the variable
${VAR/#substring/replacement}VAR=example; echo ${VAR/#exam/ap}appleIf the variable starts with substring, replace it with replacement
${VAR/%substring/replacement}VAR=example; echo ${VAR/%mple/ctly}exactlyIf the variable ends with substring, replace it with replacement
${#VAR}VAR=example; echo ${#VAR}7Returns the length of the variable
${VAR=default}unset VAR; echo ${VAR=default}defaultIf the variable is unset or null, it will be set to default
${VAR:=default}unset VAR; echo ${VAR:=default}defaultIf the variable is unset or null, it will be set to default and the assignment is exported
${VAR:-default}unset VAR; echo ${VAR:-default}defaultIf the variable is unset or null, it will be replaced with default without changing the value of VAR
tip

If you want to play with the examples above in a terminal, make sure you are in a bash shell.

Escaping

tip

var expressions are evaluated before the yaml is parsed. If you do not want the system to evaluate an expression it must be escaped.

This can come in handy particularly when dealing with runtime build environment variables.

version: "1"
steps:
- name: echo
commands:
# This expression does not escape the evaluation
+ - echo ${VELA_BUILD_COMMIT}
# This expression does escape the evaluation by adding the double '$$'
+ - echo $${VELA_BUILD_COMMIT}