Building Artifacts with a Custom Build Script

This page describes building Skaffold artifacts using a custom build script, which builds images using ko. ko builds containers from Go source code, without the need for a Dockerfile or even installing Docker.

Before you begin

First, you will need to have Skaffold and a Kubernetes cluster set up. To learn more about how to set up Skaffold and a Kubernetes cluster, see the quickstart docs.

Tutorial - Hello World in Go

This tutorial will be based on the custom example in our repository.

Adding a Custom Builder to Your Skaffold Project

We’ll need to configure your Skaffold config to build artifacts with ko. To do this, we will take advantage of the custom builder in Skaffold.

First, add a build.sh file which Skaffold will call to build artifacts:

#!/usr/bin/env bash
set -e

if ! [ -x "$(command -v ko)" ]; then
    GO111MODULE=on go get -mod=readonly github.com/google/ko/cmd/ko@v0.4.0
fi

output=$(ko publish --local --preserve-import-paths --tags= . | tee)
ref=$(echo $output | tail -n1)

docker tag $ref $IMAGE
if $PUSH_IMAGE; then
    docker push $IMAGE
fi

Then, configure artifacts in your skaffold.yaml to build with build.sh:

apiVersion: skaffold/v2alpha1
kind: Config
build:
  artifacts:
  - image: gcr.io/k8s-skaffold/skaffold-custom
    custom:
      buildCommand: ./build.sh
      dependencies:
        paths:
        - .

List the file dependencies for each artifact; in the example above, Skaffold watches all files in the build context. For more information about listing dependencies for custom artifacts, see the documentation here.

You can check custom builder is properly configured by running skaffold build. This command should build the artifacts and exit successfully.