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:
build.artifacts[].docker.buildArgs
(see builders)build.artifacts[].ko.{env,flags,labels,ldflags}
(seeko
builder)build.tagPolicy.envTemplate.template
(see envTemplate tagger)deploy.helm.releases[].chartPath
(see Deploying with helm)deploy.helm.releases[].name
(see Deploying with helm)deploy.helm.releases[].namespace
(see Deploying with helm)deploy.helm.releases[].repo
(see Deploying with helm)deploy.helm.releases[].setValueTemplates
(see Deploying with helm)deploy.helm.releases[].version
(see Deploying with helm)deploy.helm.releases.valuesFiles
(see Deploying with helm)deploy.kubectl.defaultNamespace
deploy.kustomize.defaultNamespace
manifests.kustomize.paths.[]
manifests.helm.releases[].setValueTemplates
portForward.namespace
portForward.resourceName
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 inbuild.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 digestIMAGE_NAME<N>
,IMAGE_REPO<N>
,IMAGE_TAG<N>
,IMAGE_DIGEST<N>
- 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