chore(build): retrieve go version from go.mod as single point of truth

This commit is contained in:
Jan Larwig 2025-01-17 17:56:28 +01:00
parent 4c823a66c7
commit 58527ec6c9
6 changed files with 25 additions and 21 deletions

View File

@ -19,8 +19,7 @@ jobs:
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v5 uses: actions/setup-go@v5
with: with:
# renovate: datasource=golang-version depName=golang go-version-file: go.mod
go-version: 1.23.4
id: go id: go
- name: Get dependencies - name: Get dependencies

View File

@ -33,15 +33,6 @@ jobs:
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2
# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}
# Initializes the CodeQL tools for scanning. # Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL - name: Initialize CodeQL

View File

@ -1,13 +1,18 @@
# This ARG has to be at the top, otherwise the docker daemon does not known what to do with FROM ${RUNTIME_IMAGE} # The image ARGs have to be at the top, otherwise the docker daemon cannot validate
ARG RUNTIME_IMAGE=gcr.io/distroless/static:nonroot # the FROM statements and overall Dockerfile
# version is shared between mutiple buildstages #
# Argument for setting the build image
ARG BUILD_IMAGE=placeholder
# Argument for setting the runtime image
ARG RUNTIME_IMAGE=placeholder
# Argument for setting the oauth2-proxy build version
ARG VERSION ARG VERSION
# All builds should be done using the platform native to the build node to allow # All builds should be done using the platform native to the build node to allow
# cache sharing of the go mod download step. # cache sharing of the go mod download step.
# Go cross compilation is also faster than emulation the go compilation across # Go cross compilation is also faster than emulation the go compilation across
# multiple platforms. # multiple platforms.
FROM --platform=${BUILDPLATFORM} docker.io/library/golang:1.22-bookworm AS builder FROM --platform=${BUILDPLATFORM} ${BUILD_IMAGE} AS builder
# Copy sources # Copy sources
WORKDIR $GOPATH/src/github.com/oauth2-proxy/oauth2-proxy WORKDIR $GOPATH/src/github.com/oauth2-proxy/oauth2-proxy
@ -19,10 +24,12 @@ RUN go mod download
# Now pull in our code # Now pull in our code
COPY . . COPY . .
# Arguments go here so that the previous steps can be cached if no external # Arguments go here so that the previous steps can be cached if no external sources
# sources have changed. # have changed. These arguments are automatically set by the docker engine.
ARG TARGETPLATFORM ARG TARGETPLATFORM
ARG BUILDPLATFORM ARG BUILDPLATFORM
# Reload version argument
ARG VERSION ARG VERSION
# Build binary and make sure there is at least an empty key file. # Build binary and make sure there is at least an empty key file.
@ -46,8 +53,11 @@ RUN case ${TARGETPLATFORM} in \
printf "Building OAuth2 Proxy for arch ${GOARCH}\n" && \ printf "Building OAuth2 Proxy for arch ${GOARCH}\n" && \
GOARCH=${GOARCH} VERSION=${VERSION} make build && touch jwt_signing_key.pem GOARCH=${GOARCH} VERSION=${VERSION} make build && touch jwt_signing_key.pem
# Reload runtime image
ARG RUNTIME_IMAGE
# Copy binary to runtime image # Copy binary to runtime image
FROM ${RUNTIME_IMAGE} FROM ${RUNTIME_IMAGE}
# Reload version
ARG VERSION ARG VERSION
COPY --from=builder /go/src/github.com/oauth2-proxy/oauth2-proxy/oauth2-proxy /bin/oauth2-proxy COPY --from=builder /go/src/github.com/oauth2-proxy/oauth2-proxy/oauth2-proxy /bin/oauth2-proxy

View File

@ -12,8 +12,10 @@ DATE := $(shell date +"%Y%m%d")
GO_MAJOR_VERSION = $(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1) GO_MAJOR_VERSION = $(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1)
GO_MINOR_VERSION = $(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2) GO_MINOR_VERSION = $(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
MINIMUM_SUPPORTED_GO_MAJOR_VERSION = 1
MINIMUM_SUPPORTED_GO_MINOR_VERSION = 20 GO_MOD_VERSION = $(shell sed -En 's/^go ([[:digit:]]\.[[:digit:]]+)\.[[:digit:]]+/\1/p' go.mod)
MINIMUM_SUPPORTED_GO_MAJOR_VERSION = $(shell echo ${GO_MOD_VERSION} | cut -d' ' -f1 | cut -d'.' -f1)
MINIMUM_SUPPORTED_GO_MINOR_VERSION = $(shell echo ${GO_MOD_VERSION} | cut -d' ' -f1 | cut -d'.' -f2)
GO_VERSION_VALIDATION_ERR_MSG = Golang version is not supported, please update to at least $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION).$(MINIMUM_SUPPORTED_GO_MINOR_VERSION) GO_VERSION_VALIDATION_ERR_MSG = Golang version is not supported, please update to at least $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION).$(MINIMUM_SUPPORTED_GO_MINOR_VERSION)
ifeq ($(COVER),true) ifeq ($(COVER),true)
@ -42,9 +44,11 @@ build: validate-go-version clean $(BINARY)
$(BINARY): $(BINARY):
CGO_ENABLED=0 $(GO) build -a -installsuffix cgo -ldflags="-X github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version.VERSION=${VERSION}" -o $@ github.com/oauth2-proxy/oauth2-proxy/v7 CGO_ENABLED=0 $(GO) build -a -installsuffix cgo -ldflags="-X github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version.VERSION=${VERSION}" -o $@ github.com/oauth2-proxy/oauth2-proxy/v7
DOCKER_BUILDX_COMMON_ARGS ?= --build-arg BUILD_IMAGE=docker.io/library/golang:${GO_MOD_VERSION}-bookworm --build-arg VERSION=${VERSION}
DOCKER_BUILD_PLATFORM ?= linux/amd64,linux/arm64,linux/ppc64le,linux/arm/v7,linux/s390x DOCKER_BUILD_PLATFORM ?= linux/amd64,linux/arm64,linux/ppc64le,linux/arm/v7,linux/s390x
DOCKER_BUILD_RUNTIME_IMAGE ?= gcr.io/distroless/static:nonroot DOCKER_BUILD_RUNTIME_IMAGE ?= gcr.io/distroless/static:nonroot
DOCKER_BUILDX_ARGS ?= --build-arg RUNTIME_IMAGE=${DOCKER_BUILD_RUNTIME_IMAGE} --build-arg VERSION=${VERSION} DOCKER_BUILDX_ARGS ?= --build-arg RUNTIME_IMAGE=${DOCKER_BUILD_RUNTIME_IMAGE} ${DOCKER_BUILDX_COMMON_ARGS}
DOCKER_BUILDX := docker buildx build ${DOCKER_BUILDX_ARGS} --pull DOCKER_BUILDX := docker buildx build ${DOCKER_BUILDX_ARGS} --pull
DOCKER_BUILDX_X_PLATFORM := $(DOCKER_BUILDX) --platform ${DOCKER_BUILD_PLATFORM} DOCKER_BUILDX_X_PLATFORM := $(DOCKER_BUILDX) --platform ${DOCKER_BUILD_PLATFORM}
DOCKER_BUILDX_PUSH := $(DOCKER_BUILDX) --push DOCKER_BUILDX_PUSH := $(DOCKER_BUILDX) --push
@ -52,7 +56,7 @@ DOCKER_BUILDX_PUSH_X_PLATFORM := $(DOCKER_BUILDX_PUSH) --platform ${DOCKER_BUILD
DOCKER_BUILD_PLATFORM_ALPINE ?= linux/amd64,linux/arm64,linux/ppc64le,linux/arm/v6,linux/arm/v7,linux/s390x DOCKER_BUILD_PLATFORM_ALPINE ?= linux/amd64,linux/arm64,linux/ppc64le,linux/arm/v6,linux/arm/v7,linux/s390x
DOCKER_BUILD_RUNTIME_IMAGE_ALPINE ?= alpine:3.21.2 DOCKER_BUILD_RUNTIME_IMAGE_ALPINE ?= alpine:3.21.2
DOCKER_BUILDX_ARGS_ALPINE ?= --build-arg RUNTIME_IMAGE=${DOCKER_BUILD_RUNTIME_IMAGE_ALPINE} --build-arg VERSION=${VERSION} DOCKER_BUILDX_ARGS_ALPINE ?= --build-arg RUNTIME_IMAGE=${DOCKER_BUILD_RUNTIME_IMAGE_ALPINE} ${DOCKER_BUILDX_COMMON_ARGS}
DOCKER_BUILDX_X_PLATFORM_ALPINE := docker buildx build ${DOCKER_BUILDX_ARGS_ALPINE} --platform ${DOCKER_BUILD_PLATFORM_ALPINE} DOCKER_BUILDX_X_PLATFORM_ALPINE := docker buildx build ${DOCKER_BUILDX_ARGS_ALPINE} --platform ${DOCKER_BUILD_PLATFORM_ALPINE}
DOCKER_BUILDX_PUSH_X_PLATFORM_ALPINE := $(DOCKER_BUILDX_X_PLATFORM_ALPINE) --push DOCKER_BUILDX_PUSH_X_PLATFORM_ALPINE := $(DOCKER_BUILDX_X_PLATFORM_ALPINE) --push