From 2ce93b6b31f8a3dc47bdacaf5e6d4e0b960d1974 Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Tue, 5 Oct 2021 16:58:54 +0100 Subject: [PATCH] Improve build times by sharing cache and allowing platform selection --- CHANGELOG.md | 1 + Dockerfile | 22 +++++++++++++++++++--- Makefile | 12 +++++++----- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d36a0dbc..e350bf2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ ## Changes since v7.1.3 +- [#1391](https://github.com/oauth2-proxy/oauth2-proxy/pull/1391) Improve build times by sharing cache and allowing platform selection (@JoelSpeed) - [#1404](https://github.com/oauth2-proxy/oauth2-proxy/pull/1404) Improve error message when no cookie is found (@JoelSpeed) - [#1315](https://github.com/oauth2-proxy/oauth2-proxy/pull/1315) linkedin: Update provider to v2 (@wuurrd) - [#1348](https://github.com/oauth2-proxy/oauth2-proxy/pull/1348) Using the native httputil proxy code for websockets rather than yhat/wsutil to properly handle HTTP-level failures (@thetrime) diff --git a/Dockerfile b/Dockerfile index 321f5691..1ebe7a87 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,24 @@ -FROM golang:1.16-buster AS builder +# All builds should be done using the platform native to the build node to allow +# cache sharing of the go mod download step. +# Go cross compilation is also faster than emulation the go compilation across +# multiple platforms. +FROM --platform=${BUILDPLATFORM} golang:1.16-buster AS builder # Copy sources WORKDIR $GOPATH/src/github.com/oauth2-proxy/oauth2-proxy # Fetch dependencies COPY go.mod go.sum ./ -RUN GO111MODULE=on go mod download +RUN go mod download # Now pull in our code COPY . . +# Arguments go here so that the previous steps can be cached if no external +# sources have changed. ARG VERSION +ARG TARGETPLATFORM +ARG BUILDPLATFORM # Build binary and make sure there is at least an empty key file. # This is useful for GCP App Engine custom runtime builds, because @@ -18,7 +26,15 @@ ARG VERSION # build the key into the container and then tell it where it is # by setting OAUTH2_PROXY_JWT_KEY_FILE=/etc/ssl/private/jwt_signing_key.pem # in app.yaml instead. -RUN VERSION=${VERSION} make build && touch jwt_signing_key.pem +# Set the cross compilation arguments based on the TARGETPLATFORM which is +# automatically set by the docker engine. +RUN case ${TARGETPLATFORM} in \ + "linux/amd64") GOARCH=amd64 ;; \ + "linux/arm64") GOARCH=arm64 ;; \ + "linux/arm/v6") GOARCH=arm GOARM=6 ;; \ + esac && \ + printf "Building OAuth2 Proxy for arch ${GOARCH}\n" && \ + VERSION=${VERSION} make build && touch jwt_signing_key.pem # Copy binary to alpine FROM alpine:3.14 diff --git a/Makefile b/Makefile index 414dc85f..d0368ebf 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,8 @@ all: lint $(BINARY) .PHONY: clean clean: - rm -rf release - rm -f $(BINARY) + -rm -rf release + -rm -f $(BINARY) .PHONY: distclean distclean: clean @@ -37,10 +37,12 @@ lint: validate-go-version build: validate-go-version clean $(BINARY) $(BINARY): - GO111MODULE=on CGO_ENABLED=0 $(GO) build -a -installsuffix cgo -ldflags="-X main.VERSION=${VERSION}" -o $@ github.com/oauth2-proxy/oauth2-proxy/v7 + CGO_ENABLED=0 $(GO) build -a -installsuffix cgo -ldflags="-X main.VERSION=${VERSION}" -o $@ github.com/oauth2-proxy/oauth2-proxy/v7 +DOCKER_BUILD_PLATFORM ?= linux/amd64,linux/arm64,linux/arm/v6 +DOCKER_BUILDX_ARGS ?= DOCKER_BUILD := docker build --build-arg VERSION=${VERSION} -DOCKER_BUILDX := docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v6 --build-arg VERSION=${VERSION} +DOCKER_BUILDX := docker buildx build ${DOCKER_BUILDX_ARGS} --platform ${DOCKER_BUILD_PLATFORM} --build-arg VERSION=${VERSION} .PHONY: docker docker: @@ -58,7 +60,7 @@ docker-all: docker .PHONY: docker-push docker-push: - docker buildx build --push --platform linux/amd64,linux/arm64,linux/arm/v6 -t $(REGISTRY)/oauth2-proxy:latest . + docker buildx build --push --platform ${DOCKER_BUILD_PLATFORM} -t $(REGISTRY)/oauth2-proxy:latest . .PHONY: docker-push-all docker-push-all: docker-push