git.iko.soy/typeable/oauth2-proxy
on master help sign in
feat(makefile): simplify validate-go-version (#3147)

Since Go 1.21 the go toolchain validates strictly the "go" version directive in go.mod, and downloads and uses the requested toolchain if necessary. See https://go.dev/doc/toolchain So we can just run "go list" to tell the Go toolchain to validate our build environment according to go.mod. To extract the "go" directive version from go.mod (used to select the Docker build image) we also use "go list".

commitf950dc99426014f3973b2c9193ee5eecf515e39b authorOlivier Mengué <dolmen@cpan.org> dateTue 28 Oct 2025, 09:13 (1 year ago) parent31b275f
1 file changed+7−16
Makefile+7−16
Makefile+7−16
@@ -36,13 +36,12 @@ REPOSITORY ?= oauth2-proxy
3636DATE := $(shell date +"%Y%m%d")
3737.NOTPARALLEL:
3838
39−GO_MAJOR_VERSION = $(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1)
40−GO_MINOR_VERSION = $(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
39+# The go version in go.mod used for the Docker build toolchain, without the patch
40+GO_MOD_VERSION_MINOR := $(shell $(GO) list -f '{{printf "%.4s" .Module.GoVersion}}' )
4141
42−GO_MOD_VERSION = $(shell sed -En 's/^go ([[:digit:]]\.[[:digit:]]+)\.[[:digit:]]+/\1/p' go.mod)
43−MINIMUM_SUPPORTED_GO_MAJOR_VERSION = $(shell echo ${GO_MOD_VERSION} | cut -d' ' -f1 | cut -d'.' -f1)
44−MINIMUM_SUPPORTED_GO_MINOR_VERSION = $(shell echo ${GO_MOD_VERSION} | cut -d' ' -f1 | cut -d'.' -f2)
45−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)
42+# From go1.21 go will transparently download the toolchain declared in go.mod. https://go.dev/doc/toolchain
43+# We don't need to keep this message updated: the important info is in go.mod.
44+GO_VERSION_VALIDATION_ERR_MSG = Golang version is not supported, please update to at least go1.21
4645
4746ifeq ($(COVER),true)
4847TESTCOVER ?= -coverprofile c.out
@@ -56,7 +55,7 @@ build: validate-go-version clean $(BINARY) ## Build and create oauth2-proxy bina
5655$(BINARY):
5756 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
5857
59−DOCKER_BUILDX_COMMON_ARGS ?= --build-arg BUILD_IMAGE=docker.io/library/golang:${GO_MOD_VERSION}-bookworm --build-arg VERSION=${VERSION}
58+DOCKER_BUILDX_COMMON_ARGS ?= --build-arg BUILD_IMAGE=docker.io/library/golang:$(GO_MOD_VERSION_MINOR)-bookworm --build-arg VERSION=$(VERSION)
6059
6160DOCKER_BUILD_PLATFORM ?= linux/amd64,linux/arm64,linux/ppc64le,linux/arm/v7,linux/s390x
6261DOCKER_BUILD_RUNTIME_IMAGE ?= gcr.io/distroless/static:nonroot
@@ -158,15 +157,7 @@ lint: validate-go-version ## Lint all files using golangci-lint
158157
159158.PHONY: validate-go-version
160159validate-go-version: ## Validate Go environment requirements
161− @if [ $(GO_MAJOR_VERSION) -gt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \
162− exit 0 ;\
163− elif [ $(GO_MAJOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \
164− echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\
165− exit 1; \
166− elif [ $(GO_MINOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MINOR_VERSION) ] ; then \
167− echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\
168− exit 1; \
169− fi
160+ @$(GO) list . >/dev/null || { echo '$(GO_VERSION_VALIDATION_ERR_MSG)'; exit 1; }
170161
171162# local-env can be used to interact with the local development environment
172163# eg: