Skip to main content

Expression Language Reference

The steps of a user-defined promotion process may take advantage of expressions in their configuration.

info

The documentation on this page assumes a general familiarity with the concept of Promotion Templates and some knowledge of how a promotion process is defined as a sequence of discrete steps.

For more information on Promotion Templates, refer to the Promotion Templates Reference.

For detailed coverage of individual promotion steps, refer to the Promotion Steps Reference.

Syntax

All steps in a user-defined promotion processes (i.e. those described by a Promotion Template and PromotionTasks) support the use of expr-lang as a means of dynamically resolving values in their configuration at promotion time.

All expressions must be enclosed within the ${{ and }} delimiters. This is not universally true for all applications of expr-lang. Kargo selected these specific delimiters to mimic GitHub Actions expression syntax, which many users will already be familiar with.

Basic example:

config:
message: ${{ "Hello, world!" }}

The above example will be evaluated as the following:

config:
message: Hello, world!

The expr-lang language definition docs provide a comprehensive overview of the language's syntax and capabilities, so this reference will continue to focus only on Kargo-specific extensions and usage.

Behavior

Kargo will evaluate expressions just-in-time as each step of a promotion process is executed. It will only evaluate expressions within values of a configuration block and will not evaluate expressions within keys. Expressions in values are evaluated recursively, so expressions may be nested any number of levels deep within a configuration block.

Validation

Kargo parses configuration blocks before evaluating expressions, so any configuration containing expressions must be well-formed YAML even prior to evaluation. Further validation (e.g. for adherence to a step-specific schema) is performed only after expressions are evaluated.

Types

Due to the requirement that configuration blocks be well-formed YAML, all fields containing expressions must be strings. Internally, all expressions will also evaluate to strings, however, Kargo will attempt to coerce the results to other valid JSON types (YAML is a superset of JSON) including object, array, number, boolean, and null before concluding that the evaluated expression should continue to be treated as a string.

This behavior should be unsurprising and perhaps even familiar to experienced YAML users, as YAML parsers behave in the same way. 42, for example, is interpreted as a JSON number unless it is explicitly quoted (i.e. "42") to specify that it should be interpreted as a string.

In practice, this means care should be taken to use Kargo's built-in quote() function in cases where an evaluated expression may appear to be a number or boolean, for instance, but should be treated as a string.

For example:

config:
numField: ${{ 40 + 2 }} # Will be treated as a number
strField: ${{ quote(40 + 2) }} # Will be treated as a string

The above example will be evaluated to the following:

config:
numField: 42
strField: "42"

Pre-Defined Variables

Kargo provides a number of pre-defined variables that are accessible within expressions. This section enumerates these variables, their structure, and use.

NameTypeDescription
ctxobjectstring fields project, stage, and promotion provide convenient access to details of a Promotion.
outputsobjectA map of output from previous promotion steps indexed by step aliases.
secretsobjectA map of maps indexed by the names of all Kubernetes Secrets in the Promotion's Project and the keys within the Data block of each.
varsobjectA user-defined map of variable names to static values of any type. The map is derived from a Promotion's spec.promotionTemplate.spec.vars field. Variable names must observe standard Go variable-naming rules. Variables values may, themselves, be defined using an expression. vars (contains previously defined variables) and ctx are available to expressions defining the values of variables, however, outputs and secrets are not.
taskobjectA map containing output from previous steps within the same PromotionTask under the outputs field, indexed by step aliases. Only available within (Cluster)PromotionTask steps.
info

Expect other useful variables to be added in the future.

The following example promotion process clones a repository and checks out two branches to different directories, uses Kustomize with source from one branch to render some Kubernetes manifests that it commits to the other branch, and pushes back to the repository. These steps make extensive use of the pre-defined variables ctx, outputs, and vars.

promotionTemplate:
spec:
vars:
- name: gitRepo
value: https://github.com/example/repo.git
- name: srcPath
value: ./src
- name: outPath
value: ./out
- name: targetBranch
value: stage/${{ ctx.stage }}
steps:
- uses: git-clone
config:
repoURL: ${{ vars.gitRepo }}
checkout:
- fromFreight: true
path: ${{ vars.srcPath }}
- branch: ${{ vars.targetBranch }}
create: true
path: ${{ vars.outPath }}
- uses: git-clear
config:
path: ${{ vars.outPath }}
- uses: kustomize-set-image
as: update-image
config:
path: ${{ vars.srcPath }}/base
images:
- image: public.ecr.aws/nginx/nginx
- uses: kustomize-build
config:
path: ${{ vars.srcPath }}/stages/${{ ctx.stage }}
outPath: ${{ vars.outPath }}
- uses: git-commit
as: commit
config:
path: ${{ vars.outPath }}
messageFromSteps:
- update-image
- uses: git-push
config:
path: ${{ vars.outPath }}
targetBranch: ${{ vars.targetBranch }}
- uses: argocd-update
config:
apps:
- name: example-${{ ctx.stage }}
sources:
- repoURL: ${{ vars.gitRepo }}
desiredRevision: ${{ outputs.commit.commit }}
info

Since the usage of expressions and pre-defined variables effectively parameterizes the promotion process, the same promotion process can be reused in other Projects or Stages with few, if any, modifications (other than the definition of the static variables).

Functions

Several functions are built-in to Kargo's expression language. This section describes each of them.

quote(value)

The quote() function converts any value to its string representation. It has one required argument:

  • value (Required): A value of any type to be converted to a string.

This is useful for scenarios where an expression evaluates to a non-string JSON type, but you wish to treat it as a string regardless.

Example:

config:
numField: ${{ 40 + 2 }} # Will be treated as a number
strField: ${{ quote(40 + 2) }} # Will be treated as a string

warehouse(name)

The warehouse() function returns a FreightOrigin object representing a Warehouse. It has one required argument:

  • name (Required): A string representing the name of a Warehouse resource in the same Project as the Promotion being executed.

The returned FreightOrigin object has the following fields:

FieldDescription
KindThe kind of the FreightOrigin. Always equals Warehouse for this function.
NameThe name of the Warehouse resource.

The FreightOrigin object can be used as an optional argument to the commitFrom(), imageFrom(), or chartFrom() functions to disambiguate the desired source of an artifact when necessary.

commitFrom(repoURL, [freightOrigin])

The commitFrom() function returns a corresponding GitCommit object from the Promotion's FreightCollection. It has one required and one optional argument:

  • repoURL (Required): The URL of a Git repository.
  • freightOrigin (Optional): A FreightOrigin object (obtained from warehouse()) to specify which Warehouse should provide the commit information.

The returned GitCommit object has the following fields:

FieldDescription
RepoURLThe URL of the Git repository the commit originates from.
IDThe ID of the Git commit.
BranchThe branch of the repository where this commit was found. Only present if the Warehouse's Git subscription is configured to track branches.
TagThe tag of the repository where this commit was found. Only present if the Warehouse's Git subscription is configured to track tags.
MessageThe first line of the commit message (up to 80 characters).
AuthorThe name and email address of the commit author.
CommitterThe name and email address of the committer.

The optional freightOrigin argument should be used when a Stage requests Freight from multiple origins (Warehouses) and more than one can provide a GitCommit object from the specified repository.

Examples:

config:
commitID: ${{ commitFrom("https://github.com/example/repo.git").ID }}
config:
commitID: ${{ commitFrom("https://github.com/example/repo.git", warehouse("my-warehouse")).ID }}

imageFrom(repoURL, [freightOrigin])

The imageFrom() function returns a corresponding Image object from the Promotion's FreightCollection. It has one required and one optional argument:

  • repoURL (Required): The URL of a container image repository.
  • freightOrigin (Optional): A FreightOrigin object (obtained from warehouse()) to specify which Warehouse should provide the image information.

The returned Image object has the following fields:

FieldDescription
RepoURLThe URL of the container image repository the image originates from.
GitRepoURLThe URL of the Git repository which contains the source code for the image. Only present if Kargo was able to infer it from the URL.
TagThe tag of the image.
DigestThe digest of the image.

The optional freightOrigin argument should be used when a Stage requests Freight from multiple origins (Warehouses) and more than one can provide a Image object from the specified repository.

Examples:

config:
imageTag: ${{ imageFrom("public.ecr.aws/nginx/nginx").Tag }}
config:
imageTag: ${{ imageFrom("public.ecr.aws/nginx/nginx", warehouse("my-warehouse")).Tag }}

chartFrom(repoURL, [chartName], [freightOrigin])

The chartFrom() function returns a corresponding Chart object from the Promotion's FreightCollection. It has one required and two optional arguments:

  • repoURL (Required): The URL of a Helm chart repository.
  • chartName (Optional): The name of the chart (required for HTTP/S repositories, not needed for OCI registries).
  • freightOrigin (Optional): A FreightOrigin object (obtained from warehouse()) to specify which Warehouse should provide the chart information.

The returned Chart object has the following fields:

FieldDescription
RepoURLThe URL of the Helm chart repository the chart originates from. For HTTP/S repositories, this is the URL of the repository. For OCI repositories, this is the URL of the container image repository including the chart's name.
NameThe name of the Helm chart. Only present for HTTP/S repositories.
VersionThe version of the Helm chart.

For Helm charts stored in OCI registries, the URL should be the full path to the repository within that registry.

For Helm charts stored in classic (HTTP/S) repositories, which can store multiple different charts within a single repository, the chartName argument must be provided to specify the name of the chart within the repository.

The optional freightOrigin argument should be used when a Stage requests Freight from multiple origins (Warehouses) and more than one can provide a Chart object from the specified repository.

Examples:

# OCI registry
config:
chartVersion: ${{ chartFrom("oci://example.com/my-chart").Version }}
# OCI registry with specific warehouse
config:
chartVersion: ${{ chartFrom("oci://example.com/my-chart", warehouse("my-warehouse")).Version }}
# HTTP/S repository
config:
chartVersion: ${{ chartFrom("https://example.com/charts", "my-chart").Version }}
# HTTP/S repository with specific warehouse
config:
chartVersion: ${{ chartFrom("https://example.com/charts", "my-chart", warehouse("my-warehouse")).Version }}