Skip to content

Commit f8ec83f

Browse files
authored
DevOps: fix golang version handling in bash scripts (#6288)
1 parent 160bcf3 commit f8ec83f

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ else
2020
endif
2121
S3_RELEASE_BUCKET = $$S3_RELEASE_BUCKET
2222

23-
GOLANG_VERSIONS := $(shell ./scripts/get_golang_version.sh all)
24-
GOLANG_VERSION_BUILD := $(firstword $(GOLANG_VERSIONS))
25-
GOLANG_VERSION_SUPPORT := $(lastword $(GOLANG_VERSIONS))
26-
GOLANG_VERSION_BUILD_MAJOR := $(shell echo $(GOLANG_VERSION_BUILD) | cut -d'.' -f1,2)
27-
CURRENT_GO_VERSION := $(shell go version | cut -d " " -f 3 | tr -d 'go')
28-
CURRENT_GO_VERSION_MAJOR := $(shell echo $(CURRENT_GO_VERSION) | cut -d'.' -f1,2)
23+
GOLANG_VERSIONS := $(shell ./scripts/get_golang_version.sh all)
24+
GOLANG_VERSION_BUILD := $(firstword $(GOLANG_VERSIONS))
25+
GOLANG_VERSION_BUILD_MAJOR := $(shell echo $(GOLANG_VERSION_BUILD) | cut -d'.' -f1,2)
26+
GOLANG_VERSION_MIN := $(lastword $(GOLANG_VERSIONS))
27+
GOLANG_VERSION_SUPPORT := $(shell echo $(GOLANG_VERSION_MIN) | cut -d'.' -f1,2)
28+
CURRENT_GO_VERSION := $(shell go version | cut -d " " -f 3 | tr -d 'go')
29+
CURRENT_GO_VERSION_MAJOR := $(shell echo $(CURRENT_GO_VERSION) | cut -d'.' -f1,2)
2930

3031
# If build number already set, use it - to ensure same build number across multiple platforms being built
3132
BUILDNUMBER ?= $(shell ./scripts/compute_build_number.sh)

scripts/check_golang_version.sh

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,31 @@ set -eo pipefail
1515
read -ra GOLANG_VERSIONS <<< "$(./scripts/get_golang_version.sh all)"
1616
BUILD_VERSION="${GOLANG_VERSIONS[0]}"
1717
MIN_VERSION="${GOLANG_VERSIONS[1]}"
18-
GO_MOD_SUPPORT="${GOLANG_VERSIONS[2]}"
1918

2019
# Get the field "go1.1.1" and then remove the "go" prefix.
2120
SYSTEM_GOLANG_VERSION=$(go version | awk '{ gsub(/go/, "", $3); print $3 }')
2221

2322
# https://golang.org/doc/go1.11#modules
24-
if [[ "${SYSTEM_GOLANG_VERSION}" < "$GO_MOD_SUPPORT" ]]; then
25-
echo "[$0] ERROR: The version of go on the system (${SYSTEM_GOLANG_VERSION}) is too old and does not support go modules. Please update to at least ${MIN_VERSION}"
23+
if [[ "$(printf '%s\n' ${SYSTEM_GOLANG_VERSION} 1.11 | sort -V | head -n1)" != "1.11" ]]; then
24+
echo "[$0] ERROR: The version of go on the system (${SYSTEM_GOLANG_VERSION}) is too old and does not support go modules. Please update to at least 1.11."
2625
exit 1
2726
fi
2827

29-
if [ "$1" == "dev" ]; then
30-
if [[ "${SYSTEM_GOLANG_VERSION}" < "${MIN_VERSION}" ]]; then
31-
echo "[$0] WARNING: The version of go on the system (${SYSTEM_GOLANG_VERSION}) is below the recommended version (${MIN_VERSION}) and therefore may not build correctly."
28+
if [[ "$(printf '%s\n' "$SYSTEM_GOLANG_VERSION" "$MIN_VERSION" | sort -V | head -n1)" != "$MIN_VERSION" ]]; then
29+
# We are below the minimum version
30+
if [ "$1" == "dev" ]; then
31+
echo "[$0] WARNING: The version of go on the system (${SYSTEM_GOLANG_VERSION}) is below the recommended minimum version (${MIN_VERSION}) and therefore may not build correctly."
3232
echo "[$0] Please update to at least ${MIN_VERSION}"
33-
fi
34-
elif [ "$1" == "build" ]; then
35-
if [[ "${SYSTEM_GOLANG_VERSION}" < "${MIN_VERSION}" ]]; then
36-
echo "[$0] ERROR: The version of go on the system (${SYSTEM_GOLANG_VERSION}) is below the necessary version (${MIN_VERSION}) and therefore will not build correctly."
33+
elif [ "$1" == "build" ]; then
34+
echo "[$0] ERROR: The version of go on the system (${SYSTEM_GOLANG_VERSION}) is below the necessary minimum version (${MIN_VERSION}) and therefore will not build correctly."
3735
exit 1
3836
fi
3937
else
4038
# Check to make sure that it matches what is specified in `go.mod`.
41-
GOMOD_VERSION=$(go mod edit -print | awk '/^go[ \t]+[0-9]+\.[0-9]+(\.[0-9]+)?[ \t]*$/{print $2}')
39+
GOMOD_TOOL_VERSION=$(go mod edit -print | awk '$1 == "toolchain" {sub(/^go/, "", $2); print $2}')
4240

43-
if [[ ! "${BUILD_VERSION}" =~ ^"${GOMOD_VERSION}" ]]; then
44-
echo "[$0] ERROR: go version mismatch, go mod version ${GOMOD_VERSION} does not match required version ${BUILD_VERSION}"
41+
if [[ "${BUILD_VERSION}" != "${GOMOD_TOOL_VERSION}" ]]; then
42+
echo "[$0] ERROR: go version mismatch, go mod tool version ${GOMOD_TOOL_VERSION} does not match required version ${BUILD_VERSION}"
4543
exit 1
4644
else
4745
echo "${BUILD_VERSION}"

scripts/get_golang_version.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
# build a new image whenever the version number has been changed.
1313

1414
BUILD=1.23.3
15-
MIN=1.23
16-
GO_MOD_SUPPORT=1.23
15+
MIN=$(echo $BUILD | cut -d. -f1-2).0
1716

1817
if [ "$1" = all ]
1918
then
20-
echo $BUILD $MIN $GO_MOD_SUPPORT
19+
echo $BUILD $MIN
2120
elif [ "$1" = dev ]
2221
then
2322
echo $MIN

0 commit comments

Comments
 (0)