Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 0721b95

Browse files
authored
drone: build images from cross-compiled binary (#58)
Change how we build our `FROM scratch` images to leverage the already-cross-compiled binary. See `make image-dist`. Signed-off-by: Jacob Blain Christen <[email protected]>
1 parent 4d4bd30 commit 0721b95

File tree

5 files changed

+56
-143
lines changed

5 files changed

+56
-143
lines changed

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
/bin/
2-
/dist/
32
.idea/

.drone.yml

Lines changed: 18 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
kind: pipeline
33
type: docker
4-
name: amd64
4+
name: cross
55

66
platform:
77
os: linux
@@ -20,7 +20,7 @@ steps:
2020
- name: docker
2121
path: /var/run/docker.sock
2222

23-
- name: build
23+
- name: bin-dist
2424
image: drone.cattle.io/rancher/kim:possible
2525
pull: never
2626
environment:
@@ -39,19 +39,23 @@ steps:
3939
- name: docker
4040
path: /var/run/docker.sock
4141

42-
- name: image
42+
- name: img-dist
4343
image: drone.cattle.io/rancher/kim:possible
4444
pull: never
4545
environment:
4646
DOCKER_BUILDKIT: "1"
4747
GOLANG: drone.cattle.io/rancher/kim:possible
4848
commands:
49-
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} image
49+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=amd64 image-dist
50+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=arm64 image-dist
51+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=arm image-dist
52+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=ppc64le image-dist
53+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=s390x image-dist
5054
volumes:
5155
- name: docker
5256
path: /var/run/docker.sock
5357

54-
- name: upload
58+
- name: bin-upload
5559
image: plugins/github-release
5660
settings:
5761
api_key:
@@ -72,7 +76,7 @@ steps:
7276
- refs/head/master
7377
- refs/tags/*
7478

75-
- name: image-push
79+
- name: img-push
7680
image: drone.cattle.io/rancher/kim:possible
7781
pull: never
7882
environment:
@@ -84,135 +88,11 @@ steps:
8488
GOLANG: drone.cattle.io/rancher/kim:possible
8589
commands:
8690
- docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
87-
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} image-push
88-
volumes:
89-
- name: docker
90-
path: /var/run/docker.sock
91-
when:
92-
event:
93-
- tag
94-
instance:
95-
- drone-publish.rancher.io
96-
ref:
97-
- refs/head/master
98-
- refs/tags/*
99-
100-
volumes:
101-
- name: docker
102-
host:
103-
path: /var/run/docker.sock
104-
105-
---
106-
kind: pipeline
107-
type: docker
108-
name: arm64
109-
110-
platform:
111-
os: linux
112-
arch: arm64
113-
114-
steps:
115-
- name: prepare
116-
image: library/alpine:3.12
117-
environment:
118-
GOLANG: golang:1.16-alpine3.12
119-
IMAGE: drone.cattle.io/rancher/kim
120-
commands:
121-
- "apk --no-cache add docker-cli git"
122-
- "docker build --target docker --build-arg GOLANG --tag $${IMAGE}:possible --tag $${IMAGE}:$(git rev-parse --short HEAD) ."
123-
volumes:
124-
- name: docker
125-
path: /var/run/docker.sock
126-
127-
- name: image
128-
image: drone.cattle.io/rancher/kim:possible
129-
pull: never
130-
environment:
131-
DOCKER_BUILDKIT: "1"
132-
GOLANG: drone.cattle.io/rancher/kim:possible
133-
commands:
134-
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} image
135-
volumes:
136-
- name: docker
137-
path: /var/run/docker.sock
138-
139-
- name: image-push
140-
image: drone.cattle.io/rancher/kim:possible
141-
pull: never
142-
environment:
143-
DOCKER_USERNAME:
144-
from_secret: docker_username
145-
DOCKER_PASSWORD:
146-
from_secret: docker_password
147-
DOCKER_BUILDKIT: "1"
148-
GOLANG: drone.cattle.io/rancher/kim:possible
149-
commands:
150-
- docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
151-
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} image-push
152-
volumes:
153-
- name: docker
154-
path: /var/run/docker.sock
155-
when:
156-
event:
157-
- tag
158-
instance:
159-
- drone-publish.rancher.io
160-
ref:
161-
- refs/head/master
162-
- refs/tags/*
163-
164-
volumes:
165-
- name: docker
166-
host:
167-
path: /var/run/docker.sock
168-
169-
---
170-
kind: pipeline
171-
type: docker
172-
name: armhf
173-
174-
platform:
175-
os: linux
176-
arch: arm
177-
178-
steps:
179-
- name: prepare
180-
image: library/alpine:3.12
181-
environment:
182-
GOLANG: golang:1.16-alpine3.12
183-
IMAGE: drone.cattle.io/rancher/kim
184-
commands:
185-
- "apk --no-cache add docker-cli git"
186-
- "docker build --target docker --build-arg GOLANG --tag $${IMAGE}:possible --tag $${IMAGE}:$(git rev-parse --short HEAD) ."
187-
volumes:
188-
- name: docker
189-
path: /var/run/docker.sock
190-
191-
- name: image
192-
image: drone.cattle.io/rancher/kim:possible
193-
pull: never
194-
environment:
195-
DOCKER_BUILDKIT: "1"
196-
GOLANG: drone.cattle.io/rancher/kim:possible
197-
commands:
198-
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} image
199-
volumes:
200-
- name: docker
201-
path: /var/run/docker.sock
202-
203-
- name: image-push
204-
image: drone.cattle.io/rancher/kim:possible
205-
pull: never
206-
environment:
207-
DOCKER_USERNAME:
208-
from_secret: docker_username
209-
DOCKER_PASSWORD:
210-
from_secret: docker_password
211-
DOCKER_BUILDKIT: "1"
212-
GOLANG: drone.cattle.io/rancher/kim:possible
213-
commands:
214-
- docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
215-
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} image-push
91+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=amd64 image-push
92+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=arm64 image-push
93+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=arm image-push
94+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=ppc64le image-push
95+
- make TAG=${DRONE_TAG:-$(git describe --tags --always)} GOARCH=s390x image-push
21696
volumes:
21797
- name: docker
21898
path: /var/run/docker.sock
@@ -237,10 +117,10 @@ name: manifest
237117

238118
platform:
239119
os: linux
240-
arch: arm
120+
arch: amd64
241121

242122
steps:
243-
- name: manifest
123+
- name: all
244124
image: drone.cattle.io/rancher/kim:possible
245125
pull: never
246126
environment:
@@ -266,8 +146,6 @@ steps:
266146
- refs/tags/*
267147

268148
depends_on:
269-
- amd64
270-
- arm64
271-
- armhf
149+
- cross
272150

273151
...

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@ RUN set -x \
1111
FROM base AS docker
1212
RUN set -x \
1313
&& apk --no-cache add \
14+
curl \
1415
docker-cli \
1516
&& docker --version
17+
ARG BUILDX_RELEASE=v0.5.1
18+
RUN set -x \
19+
&& export BUILDX_ARCH=$(go env GOARCH) \
20+
&& if [ "${BUILDX_ARCH}" = "arm" ]; then export BUILDX_ARCH="arm-v7"; fi \
21+
&& mkdir -p /usr/libexec/docker/cli-plugins/ \
22+
&& curl -fsSL --output /usr/libexec/docker/cli-plugins/docker-buildx \
23+
"https://github.com/docker/buildx/releases/download/${BUILDX_RELEASE}/buildx-${BUILDX_RELEASE}.linux-${BUILDX_ARCH}" \
24+
&& file /usr/libexec/docker/cli-plugins/docker-buildx \
25+
&& chmod -v +x /usr/libexec/docker/cli-plugins/docker-buildx \
26+
&& docker buildx version
1627

1728
FROM base AS gobuild
1829
RUN apk --no-cache add \

Dockerfile.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM scratch AS release
2+
ARG GOARCH=amd64
3+
ARG GOOS=linux
4+
COPY dist/artifacts/kim-${GOOS}-${GOARCH} /bin/kim
5+
ENTRYPOINT ["kim"]
6+
CMD ["--help"]

Makefile

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ GOOS := $(shell go env GOOS)
88
endif
99

1010
DOCKER_BUILDKIT ?= 1
11+
DOCKER_BUILDX ?= docker buildx
1112
DOCKER_IMAGE ?= docker image
1213
DOCKER_MANIFEST ?= docker manifest
1314

@@ -68,6 +69,16 @@ image:
6869
--tag $(IMG)-$(GOARCH) \
6970
.
7071

72+
.PHONY: image-dist
73+
image-dist:
74+
$(DOCKER_BUILDX) build \
75+
--file Dockerfile.dist \
76+
--platform $(GOOS)/$(GOARCH) \
77+
--build-arg GOARCH=$(GOARCH) \
78+
--build-arg GOOS=$(GOOS) \
79+
--tag $(IMG)-$(GOARCH) \
80+
.
81+
7182
.PHONY: image-push
7283
image-push:
7384
$(DOCKER_IMAGE) push $(IMG)-$(GOARCH)
@@ -108,5 +119,13 @@ dogfood: build
108119

109120
.PHONY: symlinks
110121
symlinks: build
111-
ln -nsf $(notdir $(BIN)) $(dir $(BIN))/kubectl-builder
112-
ln -nsf $(notdir $(BIN)) $(dir $(BIN))/kubectl-image
122+
ln -nsf $(notdir $(BIN)) $(dir $(BIN))./kubectl-builder
123+
ln -nsf $(notdir $(BIN)) $(dir $(BIN))./kubectl-image
124+
125+
.PHONY: test-image-build-with-secret
126+
test-image-build-with-secret: $(BIN)
127+
make KIM=$(shell pwd)/$(BIN) -C testdata/image-build-with-secret/.
128+
129+
.PHONY: test-image-build-with-ssh
130+
test-image-build-with-ssh: $(BIN)
131+
make KIM=$(shell pwd)/$(BIN) -C testdata/image-build-with-ssh/.

0 commit comments

Comments
 (0)