@@ -1,16 +1,24 @@
1−FROM golang:1.16-buster AS builder
1+# All builds should be done using the platform native to the build node to allow
2+# cache sharing of the go mod download step.
3+# Go cross compilation is also faster than emulation the go compilation across
4+# multiple platforms.
5+FROM --platform=${BUILDPLATFORM} golang:1.16-buster AS builder
26
37# Copy sources
48WORKDIR $GOPATH/src/github.com/oauth2-proxy/oauth2-proxy
59
610# Fetch dependencies
711COPY go.mod go.sum ./
8−RUN GO111MODULE=on go mod download
12+RUN go mod download
913
1014# Now pull in our code
1115COPY . .
1216
17+# Arguments go here so that the previous steps can be cached if no external
18+# sources have changed.
1319ARG VERSION
20+ARG TARGETPLATFORM
21+ARG BUILDPLATFORM
1422
1523# Build binary and make sure there is at least an empty key file.
1624# This is useful for GCP App Engine custom runtime builds, because
@@ -18,7 +26,15 @@ ARG VERSION
1826# build the key into the container and then tell it where it is
1927# by setting OAUTH2_PROXY_JWT_KEY_FILE=/etc/ssl/private/jwt_signing_key.pem
2028# in app.yaml instead.
21−RUN VERSION=${VERSION} make build && touch jwt_signing_key.pem
29+# Set the cross compilation arguments based on the TARGETPLATFORM which is
30+# automatically set by the docker engine.
31+RUN case ${TARGETPLATFORM} in \
32+ "linux/amd64") GOARCH=amd64 ;; \
33+ "linux/arm64") GOARCH=arm64 ;; \
34+ "linux/arm/v6") GOARCH=arm GOARM=6 ;; \
35+ esac && \
36+ printf "Building OAuth2 Proxy for arch ${GOARCH}\n" && \
37+ VERSION=${VERSION} make build && touch jwt_signing_key.pem
2238
2339# Copy binary to alpine
2440FROM alpine:3.14