Skip to content

Files

Latest commit

b0c0629 · Jun 15, 2020

History

History
This branch is 2384 commits behind GoogleContainerTools/skaffold:main.

templated-fields

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Jun 11, 2020
Jun 11, 2020
Jun 15, 2020
Jun 11, 2020
Jun 11, 2020
Jun 11, 2020

Example: use image values in templated fields for build and deploy

This example shows how IMAGE_REPO and IMAGE_TAG keywords are available in templated fields for custom build and helm deploy

  • building a single Go file app with ko
  • tagging using the default tagPolicy (gitCommit)
  • deploying two replica container pods using helm

Before you begin

For this tutorial to work, you will need to have Skaffold, Helm and a Kubernetes cluster set up. To learn more about how to set up, see the getting started docs.

Tutorial

This tutorial will demonstrate how Skaffold can inject image repo and image tag values into the build and deploy stanzas.

First, clone the Skaffold repo and navigate to the templated-fields example for sample code:

$ git clone https://github.com/GoogleContainerTools/skaffold
$ cd skaffold/examples/templated-fields

IMAGE_REPO and IMAGE_TAG are available as templated fields in build.sh file

#from build.sh, line 13
img="${IMAGE_REPO}:${IMAGE_TAG}"

and also in the helm deploy section of the skaffold config, which configures artifact skaffold-templated to build with build.sh:

// from skaffold.yaml, line 22-24
      setValueTemplates:
          imageRepo: "{{.IMAGE_REPO}}"
          imageTag: "{{.IMAGE_TAG}}"

These values are then being set as container environment variables FOO_IMAGE_REPO and FOO_IMAGE_TAG in the helm template deployment.yaml file, just as an example to show how they can be added to your helm templates.

// from charts/templates/deployment.yaml, line 16-24
      containers:
      - name: {{ .Chart.Name }}
        image: {{ .Values.image }}
        env:
          - name: FOO_IMAGE_REPO
            value: {{ .Values.imageRepo }}
          - name: FOO_IMAGE_TAG
            value: {{ .Values.imageTag }}

For more information about how this works, see the Skaffold custom builder and helm documentation.

Now, use Skaffold to deploy this application to your Kubernetes cluster:

$ skaffold run --tail --default-repo <your repo>

With this command, Skaffold will build the skaffold-templated artifact with ko and deploy the application to Kubernetes using helm. You should be able to see something like:

Running image skaffold-templated:a866d5efd634062ea74662b20e172cd6e2d645f9f33f929bfaf8e856ec66bd94

printed every second in the Skaffold logs, since the code being executed is main.go.

// from main.go, line 12
fmt.Printf("Running image %v:%v\n", os.Getenv("FOO_IMAGE_REPO"), os.Getenv("FOO_IMAGE_TAG"))

Cleanup

To clean up your Kubernetes cluster, run:

$ skaffold delete