Skip to content

Commit b6d5ff6

Browse files
committed
Use ko instead of buildpacks for the custom builder
Since buildpacks are natively supported, it’s better to use another example for the custom build script. Signed-off-by: David Gageot <[email protected]>
1 parent 2c10d41 commit b6d5ff6

File tree

15 files changed

+85
-103
lines changed

15 files changed

+85
-103
lines changed

deploy/skaffold/Dockerfile

-8
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,6 @@ ENV BAZEL_URL https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERS
6868
RUN wget -O bazel "${BAZEL_URL}"
6969
RUN chmod +x bazel
7070

71-
# Download pack
72-
FROM alpine:3.10 as download-pack
73-
ENV PACK_VERSION 0.6.0
74-
ENV PACK_URL https://github.com/buildpack/pack/releases/download/v${PACK_VERSION}/pack-v${PACK_VERSION}-linux.tgz
75-
RUN wget -O pack.tgz "${PACK_URL}"
76-
RUN tar -zxf pack.tgz
77-
7871
FROM gcr.io/gcp-runtimes/ubuntu_16_0_4 as runtime_deps
7972

8073
RUN apt-get update && \
@@ -90,7 +83,6 @@ COPY --from=download-kompose kompose /usr/local/bin/
9083
COPY --from=download-container-structure-test container-structure-test /usr/local/bin/
9184
COPY --from=download-bazel bazel /usr/local/bin/
9285
COPY --from=download-gcloud google-cloud-sdk/ /google-cloud-sdk/
93-
COPY --from=download-pack pack /usr/local/bin/
9486
COPY --from=download-kind kind /usr/local/bin/
9587

9688
# Finish installation of bazel

docs/content/en/docs/tutorials/custom-builder.md

+6-27
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,29 @@ linkTitle: "Custom Build Script"
44
weight: 100
55
---
66

7-
This page describes building Skaffold artifacts using a custom build script, which builds images using [buildpacks](https://buildpacks.io/).
8-
Buildpacks enable building language-based containers from source code, without the need for a Dockerfile.
7+
This page describes building Skaffold artifacts using a custom build script, which builds images using [ko](https://github.com/google/ko).
8+
ko builds containers from Go source code, without the need for a Dockerfile or
9+
even installing Docker.
910

1011
## Before you begin
12+
1113
First, you will need to have Skaffold and a Kubernetes cluster set up.
1214
To learn more about how to set up Skaffold and a Kubernetes cluster, see the [quickstart docs]({{< relref "/docs/quickstart" >}}).
1315

14-
For this tutorial, to use buildpacks as a custom builder with Skaffold, please install the following additional tools:
15-
16-
* [pack](https://buildpacks.io/docs/install-pack/)
17-
* [docker](https://docs.docker.com/install/)
18-
19-
20-
To use buildpacks with your own project, you must choose a buildpack image to build your artifacts.
21-
To see a list of available buildpacks, run:
22-
23-
```shell
24-
$ pack suggest-builders
25-
```
26-
27-
Choose a buildpack from the list, making sure your chosen image can detect the runtime your project is written in.
28-
Set your default buildpack:
29-
30-
```shell
31-
$ pack set-default-builder <insert buildpack image here>
32-
```
33-
3416
## Tutorial - Hello World in Go
3517

36-
This tutorial will be based on the [buildpacks example](https://github.com/GoogleContainerTools/skaffold/tree/master/examples/buildpacks) in our repository.
18+
This tutorial will be based on the [custom example](https://github.com/GoogleContainerTools/skaffold/tree/master/examples/custom) in our repository.
3719

3820

3921
## Adding a Custom Builder to Your Skaffold Project
4022

41-
We'll need to configure your Skaffold config to build artifacts with this custom builder.
23+
We'll need to configure your Skaffold config to build artifacts with [ko](https://github.com/google/ko).
4224
To do this, we will take advantage of the [custom builder]({{<relref "/docs/pipeline-stages/builders/custom" >}}) in Skaffold.
4325

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

4628
{{% readfile file="samples/builders/custom-buildpacks/build.sh" %}}
4729

48-
4930
Then, configure artifacts in your `skaffold.yaml` to build with `build.sh`:
5031

5132
{{% readfile file="samples/builders/custom-buildpacks/skaffold.yaml" %}}
@@ -55,5 +36,3 @@ For more information about listing dependencies for custom artifacts, see the do
5536

5637
You can check custom builder is properly configured by running `skaffold build`.
5738
This command should build the artifacts and exit successfully.
58-
59-
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -e
3-
images=$(echo $IMAGES | tr " " "\n")
43

5-
for image in $images
6-
do
7-
pack build $image
8-
if $PUSH_IMAGE
9-
then
10-
docker push $image
11-
fi
12-
done
4+
if ! [ -x "$(command -v ko)" ]; then
5+
GO111MODULE=on go get github.com/google/ko/cmd/ko
6+
fi
7+
8+
output=$(ko publish --local --preserve-import-paths --tags= . | tee)
9+
ref=$(echo $output | tail -n1)
10+
11+
docker tag $ref $IMAGE
12+
if $PUSH_IMAGE; then
13+
docker push $IMAGE
14+
fi
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
apiVersion: skaffold/v1
1+
apiVersion: skaffold/v2alpha1
22
kind: Config
33
build:
44
artifacts:
5-
- image: gcr.io/k8s-skaffold/image
6-
custom:
7-
buildCommand: build.sh
8-
dependencies:
9-
paths:
10-
- .
11-
deploy:
12-
kubectl:
13-
manifests:
14-
- ./k8s/**
5+
- image: gcr.io/k8s-skaffold/skaffold-custom
6+
custom:
7+
buildCommand: ./build.sh
8+
dependencies:
9+
paths:
10+
- .

examples/custom/README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
### Example: use the custom builder with Cloud Native Buildpacks
1+
### Example: use the custom builder with ko
22

33
This example shows how the custom builder can be used to
4-
build artifacts with Cloud Native Buildpacks.
4+
build artifacts with [ko](https://github.com/google/ko).
55

6-
* **building** a single Go file app with buildpacks
6+
* **building** a single Go file app with ko
77
* **tagging** using the default tagPolicy (`gitCommit`)
88
* **deploying** a single container pod using `kubectl`
99

@@ -12,31 +12,32 @@ build artifacts with Cloud Native Buildpacks.
1212
For this tutorial to work, you will need to have Skaffold and a Kubernetes cluster set up.
1313
To learn more about how to set up Skaffold and a Kubernetes cluster, see the [getting started docs](https://skaffold.dev/docs/getting-started).
1414

15-
To use buildpacks with Skaffold, please install the following additional tools:
16-
17-
* [pack](https://buildpacks.io/docs/install-pack/)
18-
* [docker](https://docs.docker.com/install/)
19-
2015
#### Tutorial
2116

22-
This tutorial will demonstrate how Skaffold can build a simple Hello World Go application with buildpacks and deploy it to a Kubernetes cluster.
17+
This tutorial will demonstrate how Skaffold can build a simple Hello World Go application with ko and deploy it to a Kubernetes cluster.
2318

24-
First, clone the Skaffold [repo](https://github.com/GoogleContainerTools/skaffold) and navigate to the [buildpacks example](https://github.com/GoogleContainerTools/skaffold/tree/master/examples/buildpacks) for sample code:
19+
First, clone the Skaffold [repo](https://github.com/GoogleContainerTools/skaffold) and navigate to the [custom example](https://github.com/GoogleContainerTools/skaffold/tree/master/examples/custom) for sample code:
2520

2621
```shell
2722
$ git clone https://github.com/GoogleContainerTools/skaffold
28-
$ cd skaffold/examples/buildpacks
23+
$ cd skaffold/examples/custom
2924
```
3025

31-
Take a look at the `build.sh` file, which uses `pack` to containerize source code with buildpacks:
26+
Take a look at the `build.sh` file, which uses `ko` to containerize source code:
3227

3328
```shell
3429
$ cat build.sh
3530
#!/usr/bin/env bash
3631
set -e
3732

38-
pack build --builder=heroku/buildpacks $IMAGE
33+
if ! [ -x "$(command -v ko)" ]; then
34+
GO111MODULE=on go get github.com/google/ko/cmd/ko
35+
fi
36+
37+
output=$(ko publish --local --preserve-import-paths --tags= . | tee)
38+
ref=$(echo $output | tail -n1)
3939

40+
docker tag $ref $IMAGE
4041
if $PUSH_IMAGE; then
4142
docker push $IMAGE
4243
fi
@@ -50,7 +51,7 @@ apiVersion: skaffold/v2alpha1
5051
kind: Config
5152
build:
5253
artifacts:
53-
- image: gcr.io/k8s-skaffold/skaffold-example
54+
- image: gcr.io/k8s-skaffold/skaffold-custom
5455
custom:
5556
buildCommand: ./build.sh
5657
```
@@ -63,7 +64,7 @@ Now, use Skaffold to deploy this application to your Kubernetes cluster:
6364
$ skaffold run --tail --default-repo <your repo>
6465
```
6566

66-
With this command, Skaffold will build the `skaffold-example` artifact with buildpacks and deploy the application to Kubernetes.
67+
With this command, Skaffold will build the `skaffold-example` artifact with ko and deploy the application to Kubernetes.
6768
You should be able to see *Hello, World!* printed every second in the Skaffold logs.
6869

6970
#### Cleanup

examples/custom/build.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
pack build --builder=heroku/buildpacks $IMAGE
4+
if ! [ -x "$(command -v ko)" ]; then
5+
GO111MODULE=on go get github.com/google/ko/cmd/ko
6+
fi
7+
8+
output=$(ko publish --local --preserve-import-paths --tags= . | tee)
9+
ref=$(echo $output | tail -n1)
510

11+
docker tag $ref $IMAGE
612
if $PUSH_IMAGE; then
713
docker push $IMAGE
814
fi

examples/custom/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/GoogleContainerTools/skaffold/examples/buildpacks
1+
module github.com/GoogleContainerTools/skaffold/examples/custom
22

3-
go 1.12
3+
go 1.13

examples/custom/k8s/pod.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ metadata:
55
spec:
66
containers:
77
- name: getting-started
8-
image: gcr.io/k8s-skaffold/skaffold-example
8+
image: gcr.io/k8s-skaffold/skaffold-custom

examples/custom/skaffold.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ apiVersion: skaffold/v2alpha1
22
kind: Config
33
build:
44
artifacts:
5-
- image: gcr.io/k8s-skaffold/skaffold-example
5+
- image: gcr.io/k8s-skaffold/skaffold-custom
66
custom:
77
buildCommand: ./build.sh
8+
dependencies:
9+
paths:
10+
- .

integration/build_test.go

-7
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ func TestBuild(t *testing.T) {
8080
{
8181
description: "custom",
8282
dir: "examples/custom",
83-
setup: func(t *testing.T, _ string) func() {
84-
cmd := exec.Command("pack", "set-default-builder", "heroku/buildpacks")
85-
if err := cmd.Run(); err != nil {
86-
t.Fatalf("error setting default buildpacks builder: %v", err)
87-
}
88-
return func() {}
89-
},
9083
},
9184
{
9285
description: "--tag arg",

integration/examples/custom/README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
### Example: use the custom builder with Cloud Native Buildpacks
1+
### Example: use the custom builder with ko
22

33
This example shows how the custom builder can be used to
4-
build artifacts with Cloud Native Buildpacks.
4+
build artifacts with [ko](https://github.com/google/ko).
55

6-
* **building** a single Go file app with buildpacks
6+
* **building** a single Go file app with ko
77
* **tagging** using the default tagPolicy (`gitCommit`)
88
* **deploying** a single container pod using `kubectl`
99

@@ -12,31 +12,32 @@ build artifacts with Cloud Native Buildpacks.
1212
For this tutorial to work, you will need to have Skaffold and a Kubernetes cluster set up.
1313
To learn more about how to set up Skaffold and a Kubernetes cluster, see the [getting started docs](https://skaffold.dev/docs/getting-started).
1414

15-
To use buildpacks with Skaffold, please install the following additional tools:
16-
17-
* [pack](https://buildpacks.io/docs/install-pack/)
18-
* [docker](https://docs.docker.com/install/)
19-
2015
#### Tutorial
2116

22-
This tutorial will demonstrate how Skaffold can build a simple Hello World Go application with buildpacks and deploy it to a Kubernetes cluster.
17+
This tutorial will demonstrate how Skaffold can build a simple Hello World Go application with ko and deploy it to a Kubernetes cluster.
2318

24-
First, clone the Skaffold [repo](https://github.com/GoogleContainerTools/skaffold) and navigate to the [buildpacks example](https://github.com/GoogleContainerTools/skaffold/tree/master/examples/buildpacks) for sample code:
19+
First, clone the Skaffold [repo](https://github.com/GoogleContainerTools/skaffold) and navigate to the [custom example](https://github.com/GoogleContainerTools/skaffold/tree/master/examples/custom) for sample code:
2520

2621
```shell
2722
$ git clone https://github.com/GoogleContainerTools/skaffold
28-
$ cd skaffold/examples/buildpacks
23+
$ cd skaffold/examples/custom
2924
```
3025

31-
Take a look at the `build.sh` file, which uses `pack` to containerize source code with buildpacks:
26+
Take a look at the `build.sh` file, which uses `ko` to containerize source code:
3227

3328
```shell
3429
$ cat build.sh
3530
#!/usr/bin/env bash
3631
set -e
3732

38-
pack build --builder=heroku/buildpacks $IMAGE
33+
if ! [ -x "$(command -v ko)" ]; then
34+
GO111MODULE=on go get github.com/google/ko/cmd/ko
35+
fi
36+
37+
output=$(ko publish --local --preserve-import-paths --tags= . | tee)
38+
ref=$(echo $output | tail -n1)
3939

40+
docker tag $ref $IMAGE
4041
if $PUSH_IMAGE; then
4142
docker push $IMAGE
4243
fi
@@ -50,7 +51,7 @@ apiVersion: skaffold/v2alpha1
5051
kind: Config
5152
build:
5253
artifacts:
53-
- image: gcr.io/k8s-skaffold/skaffold-example
54+
- image: gcr.io/k8s-skaffold/skaffold-custom
5455
custom:
5556
buildCommand: ./build.sh
5657
```
@@ -63,7 +64,7 @@ Now, use Skaffold to deploy this application to your Kubernetes cluster:
6364
$ skaffold run --tail --default-repo <your repo>
6465
```
6566

66-
With this command, Skaffold will build the `skaffold-example` artifact with buildpacks and deploy the application to Kubernetes.
67+
With this command, Skaffold will build the `skaffold-example` artifact with ko and deploy the application to Kubernetes.
6768
You should be able to see *Hello, World!* printed every second in the Skaffold logs.
6869

6970
#### Cleanup

integration/examples/custom/build.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
pack build --builder=heroku/buildpacks $IMAGE
4+
if ! [ -x "$(command -v ko)" ]; then
5+
GO111MODULE=on go get github.com/google/ko/cmd/ko
6+
fi
7+
8+
output=$(ko publish --local --preserve-import-paths --tags= . | tee)
9+
ref=$(echo $output | tail -n1)
510

11+
docker tag $ref $IMAGE
612
if $PUSH_IMAGE; then
713
docker push $IMAGE
814
fi

integration/examples/custom/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/GoogleContainerTools/skaffold/examples/buildpacks
1+
module github.com/GoogleContainerTools/skaffold/examples/custom
22

3-
go 1.12
3+
go 1.13

integration/examples/custom/k8s/pod.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ metadata:
55
spec:
66
containers:
77
- name: getting-started
8-
image: gcr.io/k8s-skaffold/skaffold-example
8+
image: gcr.io/k8s-skaffold/skaffold-custom

integration/examples/custom/skaffold.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ apiVersion: skaffold/v2alpha1
22
kind: Config
33
build:
44
artifacts:
5-
- image: gcr.io/k8s-skaffold/skaffold-example
5+
- image: gcr.io/k8s-skaffold/skaffold-custom
66
custom:
77
buildCommand: ./build.sh
8+
dependencies:
9+
paths:
10+
- .

0 commit comments

Comments
 (0)