Skip to content

Commit abbed85

Browse files
authored
Use buildkit on cloudbuild (#16)
1 parent 6adc8df commit abbed85

File tree

5 files changed

+141
-79
lines changed

5 files changed

+141
-79
lines changed

.dockerignore

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
*
2-
*/
1+
.git/
2+
hack/
3+
.dockerignore
4+
.gcloudignore
5+
.gitattributes
6+
.gitignore
7+
Dockerfile
8+
LICENSE
9+
README.md
10+
cloudbuild.yaml

.gcloudignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!include:.dockerignore
2+
23
!cloudbuild.*.yaml
34
!Dockerfile
5+
!hack/

Dockerfile

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# syntax = docker.io/docker/dockerfile-upstream:master-experimental
1+
# syntax=docker/dockerfile:1.3
22

33
ARG GOLANG_VERSION
44
ARG ALPINE_VERSION
55

6-
# target: protoc-builder
7-
FROM docker.io/debian:buster-slim AS protoc-builder
6+
FROM debian:bullseye-slim AS protoc-builder
87
ENV DEBIAN_FRONTEND=noninteractive
98
ARG PROTOC_VERSION
109
RUN set -eux && \
@@ -18,71 +17,66 @@ RUN set -eux && \
1817
unzip protoc-${PROTOC_VERSION}-linux-x86_64.zip -d protoc && \
1918
rm protoc-${PROTOC_VERSION}-linux-x86_64.zip
2019

21-
# target: protoc
2220
FROM gcr.io/distroless/base:nonroot AS protoc
2321
COPY --from=protoc-builder --chown=nonroot:nonroot /protoc/bin/protoc /
2422
COPY --from=protoc-builder --chown=nonroot:nonroot /protoc/include/google/ /usr/local/include/google
2523
USER nonroot:nonroot
26-
LABEL \
27-
maintainer="The containerz Authors" \
24+
LABEL maintainer="The containerz Authors" \
2825
org.opencontainers.image.title="gcr.io/containerz/protoc/protoc" \
2926
org.opencontainers.image.description="protoc container image" \
3027
org.opencontainers.image.url="https://github.com/containerz-dev/protoc" \
3128
org.opencontainers.image.source="[email protected]:containerz-dev/protoc"
3229
ENTRYPOINT ["/protoc"]
3330

34-
# target: protoc-debug
3531
FROM gcr.io/distroless/base:debug-nonroot AS protoc-debug
3632
COPY --from=protoc-builder --chown=nonroot:nonroot /protoc/bin/protoc /
3733
COPY --from=protoc-builder --chown=nonroot:nonroot /protoc/include/google/ /usr/local/include/google
3834
USER nonroot:nonroot
39-
LABEL \
40-
maintainer="The containerz Authors" \
35+
LABEL maintainer="The containerz Authors" \
4136
org.opencontainers.image.title="gcr.io/containerz/protoc/protoc:debug" \
4237
org.opencontainers.image.description="protoc container image" \
4338
org.opencontainers.image.url="https://github.com/containerz-dev/protoc" \
4439
org.opencontainers.image.source="[email protected]:containerz-dev/protoc"
4540
ENTRYPOINT ["/protoc"]
4641

47-
# target: golang-builder
48-
FROM docker.io/golang:${GOLANG_VERSION}-alpine${ALPINE_VERSION} AS golang-builder
42+
FROM golang:${GOLANG_VERSION}-alpine${ALPINE_VERSION} AS golang-builder
4943
ENV \
5044
OUTDIR=/out \
5145
CGO_ENABLED=0 \
5246
GO111MODULE=on
5347
RUN set -eux && \
54-
apk --no-cache add \
48+
apk add --no-cache \
49+
build-base \
5550
git
5651
ARG PROTOC_GEN_GO_VERSION
52+
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache \
53+
set -eux && \
54+
GOBIN="${OUTDIR}/usr/local/bin" go install -v -tags='osusergo,netgo,static,static_build' -buildmode=pie -ldflags='-s -w -d -linkmode external "-extldflags=-static-pie"' -installsuffix='netgo' \
55+
google.golang.org/protobuf/cmd/protoc-gen-go@${PROTOC_GEN_GO_VERSION}
5756
ARG PROTOC_GEN_GO_GRPC_VERSION
58-
RUN set -eux && \
59-
GOBIN="${OUTDIR}/usr/local/bin" go install -v -trimpath -tags='osusergo,netgo,static' -gcflags="all=-trimpath=${GOPATH}" -ldflags="-d -s -w '-extldflags=-static'" -asmflags="all=-trimpath=${GOPATH}" -installsuffix='netgo' \
60-
google.golang.org/protobuf/cmd/protoc-gen-go@${PROTOC_GEN_GO_VERSION} && \
61-
GOBIN="${OUTDIR}/usr/local/bin" go install -v -trimpath -tags='osusergo,netgo,static' -gcflags="all=-trimpath=${GOPATH}" -ldflags="-d -s -w '-extldflags=-static'" -asmflags="all=-trimpath=${GOPATH}" -installsuffix='netgo' \
57+
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache \
58+
set -eux && \
59+
GOBIN="${OUTDIR}/usr/local/bin" go install -v -tags='osusergo,netgo,static,static_build' -buildmode=pie -ldflags='-s -w -d -linkmode external "-extldflags=-static-pie"' -installsuffix='netgo' \
6260
google.golang.org/grpc/cmd/protoc-gen-go-grpc@${PROTOC_GEN_GO_GRPC_VERSION}
6361

64-
# target: golang
6562
FROM gcr.io/distroless/base:nonroot AS golang
6663
USER nonroot:nonroot
6764
COPY --from=golang-builder --chown=nonroot:nonroot /out/ /
6865
COPY --from=protoc --chown=nonroot:nonroot /protoc /usr/local/bin/protoc
6966
COPY --from=protoc --chown=nonroot:nonroot /usr/local/include /usr/local/include/
70-
LABEL \
71-
maintainer="The containerz Authors" \
67+
LABEL maintainer="The containerz Authors" \
7268
org.opencontainers.image.title="gcr.io/containerz/protoc/golang" \
7369
org.opencontainers.image.description="protoc and protoc-gen-go related binaries container image" \
7470
org.opencontainers.image.url="https://github.com/containerz-dev/protoc" \
7571
org.opencontainers.image.source="[email protected]:containerz-dev/protoc"
7672
ENTRYPOINT ["protoc"]
7773

78-
# target: golang-debug
7974
FROM gcr.io/distroless/base:debug-nonroot AS golang-debug
8075
USER nonroot:nonroot
8176
COPY --from=golang-builder --chown=nonroot:nonroot /out/ /
8277
COPY --from=protoc-debug --chown=nonroot:nonroot /protoc /usr/local/bin/protoc
8378
COPY --from=protoc-debug --chown=nonroot:nonroot /usr/local/include /usr/local/include/
84-
LABEL \
85-
maintainer="The containerz Authors" \
79+
LABEL maintainer="The containerz Authors" \
8680
org.opencontainers.image.title="gcr.io/containerz/protoc/golang:debug" \
8781
org.opencontainers.image.description="protoc and protoc-gen-go related binaries container image" \
8882
org.opencontainers.image.url="https://github.com/containerz-dev/protoc" \

cloudbuild.yaml

Lines changed: 103 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,142 @@
11
steps:
2+
- id: docker
3+
name: docker:20.10
4+
args:
5+
- build
6+
- --target=docker
7+
- --build-arg=DOCKER_VERSION=${_DOCKER_VERSION}
8+
- --build-arg=BUILDX_DIGEST=${_BUILDX_DIGEST}
9+
- --tag=docker-buildx:cloudbuild
10+
- --file=hack/dockerfiles/buildx.dockerfile
11+
- hack/dockerfiles
12+
env:
13+
- "DOCKER_BUILDKIT=1"
14+
waitFor: ["-"]
15+
16+
- id: protoc-builder
17+
name: docker-buildx:cloudbuild
18+
args:
19+
- buildx
20+
- build
21+
- --target=protoc-builder
22+
- --build-arg=BUILDKIT_INLINE_CACHE=1
23+
- --build-arg=PROTOC_VERSION=${_PROTOC_VERSION}
24+
- --build-arg=GOLANG_VERSION=${_GOLANG_VERSION}
25+
- --build-arg=ALPINE_VERSION=${_ALPINE_VERSION}
26+
- --allow=network.host
27+
- --network=host
28+
- .
29+
waitFor: ["docker"]
30+
231
- id: protoc
3-
name: gcr.io/kaniko-project/executor:latest
32+
name: docker-buildx:cloudbuild
433
args:
5-
- --destination=gcr.io/$PROJECT_ID/protoc/protoc:${_PROTOC_VERSION}
6-
- --destination=gcr.io/$PROJECT_ID/protoc/protoc:latest
7-
- --cache=true
8-
- --cache-repo=gcr.io/$PROJECT_ID/protoc/protoc/cache
9-
- --dockerfile=Dockerfile
10-
- --context=dir:///workspace
11-
- --reproducible
12-
- --log-timestamp
13-
- --skip-unused-stages=true
34+
- buildx
35+
- build
1436
- --target=protoc
15-
- --use-new-run
37+
- --build-arg=BUILDKIT_INLINE_CACHE=1
1638
- --build-arg=PROTOC_VERSION=${_PROTOC_VERSION}
1739
- --build-arg=GOLANG_VERSION=${_GOLANG_VERSION}
1840
- --build-arg=ALPINE_VERSION=${_ALPINE_VERSION}
19-
waitFor: ['-']
41+
- --output=type=image,name=gcr.io/$PROJECT_ID/protoc/protoc:${_PROTOC_VERSION},push=false
42+
- --allow=network.host
43+
- --network=host
44+
- .
45+
waitFor:
46+
- "protoc-builder"
2047

2148
- id: protoc-debug
22-
name: gcr.io/kaniko-project/executor:latest
49+
name: docker-buildx:cloudbuild
2350
args:
24-
- --destination=gcr.io/$PROJECT_ID/protoc/protoc:${_PROTOC_VERSION}-debug
25-
- --destination=gcr.io/$PROJECT_ID/protoc/protoc:debug
26-
- --cache=true
27-
- --cache-repo=gcr.io/$PROJECT_ID/protoc/protoc/cache
28-
- --dockerfile=Dockerfile
29-
- --context=dir:///workspace
30-
- --reproducible
31-
- --log-timestamp
32-
- --skip-unused-stages=true
51+
- buildx
52+
- build
3353
- --target=protoc-debug
34-
- --use-new-run
54+
- --build-arg=BUILDKIT_INLINE_CACHE=1
55+
- --build-arg=PROTOC_VERSION=${_PROTOC_VERSION}
56+
- --build-arg=GOLANG_VERSION=${_GOLANG_VERSION}
57+
- --build-arg=ALPINE_VERSION=${_ALPINE_VERSION}
58+
- --output=type=image,name=gcr.io/$PROJECT_ID/protoc/protoc:${_PROTOC_VERSION}-debug,push=false
59+
- --allow=network.host
60+
- --network=host
61+
- .
62+
waitFor:
63+
- "protoc-builder"
64+
65+
- id: golang-builder
66+
name: docker-buildx:cloudbuild
67+
args:
68+
- buildx
69+
- build
70+
- --target=golang-builder
71+
- --build-arg=BUILDKIT_INLINE_CACHE=1
3572
- --build-arg=PROTOC_VERSION=${_PROTOC_VERSION}
3673
- --build-arg=GOLANG_VERSION=${_GOLANG_VERSION}
3774
- --build-arg=ALPINE_VERSION=${_ALPINE_VERSION}
38-
waitFor: ['-']
75+
- --build-arg=PROTOC_GEN_GO_VERSION=${_PROTOC_GEN_GO_VERSION}
76+
- --build-arg=PROTOC_GEN_GO_GRPC_VERSION=${_PROTOC_GEN_GO_GRPC_VERSION}
77+
- --allow=network.host
78+
- --network=host
79+
- .
80+
waitFor:
81+
- "protoc"
82+
- "protoc-debug"
3983

4084
- id: golang
41-
name: gcr.io/kaniko-project/executor:latest
85+
name: docker-buildx:cloudbuild
4286
args:
43-
- --destination=gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-${_GOLANG_VERSION}
44-
- --destination=gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}
45-
- --destination=gcr.io/$PROJECT_ID/protoc/golang:latest
46-
- --cache=true
47-
- --cache-repo=gcr.io/$PROJECT_ID/protoc/golang/cache
48-
- --dockerfile=Dockerfile
49-
- --context=dir:///workspace
50-
- --reproducible
51-
- --log-timestamp
52-
- --skip-unused-stages=true
87+
- buildx
88+
- build
5389
- --target=golang
54-
- --use-new-run
90+
- --build-arg=BUILDKIT_INLINE_CACHE=1
5591
- --build-arg=PROTOC_VERSION=${_PROTOC_VERSION}
5692
- --build-arg=GOLANG_VERSION=${_GOLANG_VERSION}
5793
- --build-arg=ALPINE_VERSION=${_ALPINE_VERSION}
5894
- --build-arg=PROTOC_GEN_GO_VERSION=${_PROTOC_GEN_GO_VERSION}
5995
- --build-arg=PROTOC_GEN_GO_GRPC_VERSION=${_PROTOC_GEN_GO_GRPC_VERSION}
96+
- --tag=gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-${_GOLANG_VERSION}
97+
- --tag=gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}
98+
- --allow=network.host
99+
- --network=host
100+
- .
60101
waitFor:
61-
- protoc
102+
- "golang-builder"
62103

63104
- id: golang-debug
64-
name: gcr.io/kaniko-project/executor:latest
105+
name: docker-buildx:cloudbuild
65106
args:
66-
- --destination=gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-${_GOLANG_VERSION}-debug
67-
- --destination=gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-debug
68-
- --destination=gcr.io/$PROJECT_ID/protoc/golang:debug
69-
- --cache=true
70-
- --cache-repo=gcr.io/$PROJECT_ID/protoc/golang/cache
71-
- --dockerfile=Dockerfile
72-
- --context=dir:///workspace
73-
- --reproducible
74-
- --log-timestamp
75-
- --skip-unused-stages=true
107+
- buildx
108+
- build
76109
- --target=golang-debug
77-
- --use-new-run
110+
- --build-arg=BUILDKIT_INLINE_CACHE=1
78111
- --build-arg=PROTOC_VERSION=${_PROTOC_VERSION}
79112
- --build-arg=GOLANG_VERSION=${_GOLANG_VERSION}
80113
- --build-arg=ALPINE_VERSION=${_ALPINE_VERSION}
81114
- --build-arg=PROTOC_GEN_GO_VERSION=${_PROTOC_GEN_GO_VERSION}
82115
- --build-arg=PROTOC_GEN_GO_GRPC_VERSION=${_PROTOC_GEN_GO_GRPC_VERSION}
116+
- --tag=gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-${_GOLANG_VERSION}-debug
117+
- --tag=gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-debug
118+
- --allow=network.host
119+
- --network=host
120+
- .
83121
waitFor:
84-
- protoc-debug
122+
- "golang-builder"
85123

86124
substitutions:
87-
_PROTOC_VERSION: "3.15.7"
88-
_GOLANG_VERSION: "1.16"
89-
_ALPINE_VERSION: "3.13"
90-
_PROTOC_GEN_GO_VERSION: "v1.26.0"
91-
_PROTOC_GEN_GO_GRPC_VERSION: "v1.1.0"
125+
_DOCKER_VERSION: "20.10"
126+
_BUILDX_DIGEST: "2ec838c5f74e1cdd5e6ea4e789c0c0f5573807550b50b2ddc6deb2c2033a286b" # https://github.com/docker/buildx/tree/084b6c0a95ce, https://github.com/docker/buildx/runs/3947339794
127+
_PROTOC_VERSION: "3.19.0" # https://github.com/protocolbuffers/protobuf/releases/tag/v3.19.0
128+
_GOLANG_VERSION: "1.17"
129+
_ALPINE_VERSION: "3.14"
130+
_PROTOC_GEN_GO_VERSION: "v1.27.1" # https://github.com/protocolbuffers/protobuf-go/releases/tag/v1.27.1
131+
_PROTOC_GEN_GO_GRPC_VERSION: "9c668aeab869" # https://github.com/grpc/grpc-go/tree/9c668aeab869
132+
133+
images:
134+
- "gcr.io/$PROJECT_ID/protoc/protoc:${_PROTOC_VERSION}"
135+
- "gcr.io/$PROJECT_ID/protoc/protoc:${_PROTOC_VERSION}-debug"
136+
- "gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-${_GOLANG_VERSION}"
137+
- "gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}"
138+
- "gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-${_GOLANG_VERSION}-debug"
139+
- "gcr.io/$PROJECT_ID/protoc/golang:${_PROTOC_VERSION}-debug"
92140

93141
tags:
94142
- "protoc.protoc"

hack/dockerfiles/buildx.dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# syntax=docker/dockerfile:1.3
2+
3+
ARG DOCKER_VERSION
4+
ARG BUILDX_DIGEST
5+
6+
FROM --platform=$BUILDPLATFORM docker/buildx-bin@sha256:${BUILDX_DIGEST} AS buildx
7+
8+
FROM --platform=$BUILDPLATFORM docker:${DOCKER_VERSION} AS docker
9+
COPY --from=buildx /buildx /usr/libexec/docker/cli-plugins/docker-buildx
10+
RUN docker buildx version

0 commit comments

Comments
 (0)