Templated Fields

Skaffold allows for certain fields in the config to be templated via the Go text/template package. Environment variables and certain special values computed by Skaffold (see below) are available in the templating context (defined as “dot” or .).

Go templates are quite powerful, including control flow, arguments, pipelining and variables. Predefined functions in the standard library are complemented in Skaffold by the Sprig template function library.

build:
  tagPolicy:
    envTemplate:
      template: "{{.FOO}}"
  artifacts:
  - image: gcr.io/k8s-skaffold/example

Suppose the value of the FOO environment variable is v1, the image built will be gcr.io/k8s-skaffold/example:v1.

List of fields that support templating:

Please note, this list is not exhaustive.

List of variables that are available for templating:

  • all environment variables passed to the Skaffold process at startup
  • For the envTemplate tagger:
    • IMAGE_NAME - the artifact’s image name - the image name rewriting acts after the template is calculated
  • For Helm deployments:
    • IMAGE_NAME, IMAGE_TAG, IMAGE_DIGEST, IMAGE_DOMAIN, IMAGE_REPO_NO_DOMAIN - the first (by order of declaration in build.artifacts) artifact’s image name, repo, tag, sha256 digest, registry/domain and repository w/o the registry/domain prefixed . Note: the image name rewriting acts after the template is calculated.
    • IMAGE_NAME_<artifact-name>, IMAGE_REPO_<artifact-name>, IMAGE_TAG_<artifact-name>, IMAGE_DIGEST_<artifact-name> - the named artifact’s image name, repo, tag, and sha256 digest. NOTE: When used in for templating all / and - chars must be changed to _ characters as go templates do not accept / and -.
    • IMAGE_NAME2, IMAGE_REPO2, IMAGE_TAG2, IMAGE_DIGEST2 - the 2nd artifact’s image name, tag, and sha256 digest
    • IMAGE_NAME2, IMAGE_REPON, IMAGE_TAGN, IMAGE_DIGESTN - the Nth artifact’s image name, tag, and sha256 digest

Local template functions

In addition to the functions listed above, Skaffold locally provides the following:

  • cmd: This allows users to use the result from external commands in template, for example {{cmd "bash" "-c" "xxx xxx xxx"}} can be used to execute bash script and get the result into the template.

Usage Examples

The templating pipelines provided by Go templates can be quite comprehensive when combined with Sprig. For example:

  • The environment variable SOURCE_DATE_EPOCH commonly specifies a UNIX timestamp to be used in replacement of the current date and time in compiler __DATE__ and __TIME__ macros, so that the embedded timestamps become reproducible. A numeric UNIX timestamp is less readable than a proper date, and the environment variable may not exist at all, in which case we would want to use the current date. This could be written as:

    default now .SOURCE_DATE_EPOCH | date "2006-01-02T15:04:05-0700"

  • The idiomatic seven-character abbreviated Git hash is easily accessible:

    cmd "bash" "-c" "git rev-parse HEAD" | substr 0 7

Last modified April 2, 2024: release: v2.11.0 (#9376) (5431c6b)