Skip to content

Commit

Permalink
Add script to centralise golang version update
Browse files Browse the repository at this point in the history
the script is taking the golang version present in the go.mod file and apply
patch on the differents files that contains the golang version. for example
Dockerfile and github workflow files.
  • Loading branch information
clamoriniere committed Jul 10, 2024
1 parent 2ef6aa3 commit 83b0d6b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ tags
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
.idea/
/bundle/tests/
*.bak
7 changes: 7 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ generate_code:
- make generate
- git diff --exit-code

check-golang-version:
stage: test
tags: ["runner:main", "size:large"]
script:
- make update-golang
- git diff --exit-code

build_image:
stage: image
tags:
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,16 @@ bundle: manifests
bundle-build:
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

# Update the golang version in different repository files from the version present in go.mod file
.PHONY: update-golang
update-golang:
hack/update-golang.sh

#
# Datadog Custom part
#
.PHONY: install-tools
install-tools: bin/$(PLATFORM)/golangci-lint bin/$(PLATFORM)/operator-sdk bin/$(PLATFORM)/yq bin/$(PLATFORM)/kubebuilder bin/$(PLATFORM)/kubebuilder-tools bin/$(PLATFORM)/go-licenses bin/$(PLATFORM)/openapi-gen bin/$(PLATFORM)/controller-gen bin/$(PLATFORM)/openapi-gen bin/$(PLATFORM)/kustomize
install-tools: bin/$(PLATFORM)/golangci-lint bin/$(PLATFORM)/operator-sdk bin/$(PLATFORM)/yq bin/$(PLATFORM)/jq bin/$(PLATFORM)/kubebuilder bin/$(PLATFORM)/kubebuilder-tools bin/$(PLATFORM)/go-licenses bin/$(PLATFORM)/openapi-gen bin/$(PLATFORM)/controller-gen bin/$(PLATFORM)/openapi-gen bin/$(PLATFORM)/kustomize

.PHONY: generate-openapi
generate-openapi: bin/$(PLATFORM)/openapi-gen
Expand Down Expand Up @@ -205,6 +210,9 @@ tidy:
bin/$(PLATFORM)/yq: Makefile
hack/install-yq.sh "bin/$(PLATFORM)" v4.31.2

bin/$(PLATFORM)/jq: Makefile
hack/install-jq.sh "bin/$(PLATFORM)" 1.7.1

bin/$(PLATFORM)/golangci-lint: Makefile
hack/install-golangci-lint.sh -b "bin/$(PLATFORM)" v1.56.0

Expand Down
38 changes: 38 additions & 0 deletions hack/install-jq.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

SCRIPTS_DIR="$(dirname "$0")"
# Provides $OS,$ARCH,$PLATFORM,$ROOT variables
source "$SCRIPTS_DIR/install-common.sh"

cleanup() {
rm -rf "$WORK_DIR"
}
trap "cleanup" EXIT SIGINT

INSTALL_PATH=$1
VERSION=$2

BIN_ARCH=$(uname_arch)
OS=$(uname| tr [:upper:] [:lower:])
if [ "$OS" == "darwin" ]; then
OS="macos"
fi
BINARY="jq-$OS-$BIN_ARCH"

if [ -z "$VERSION" ];
then
echo "usage: bin/install-yq.sh <version>"
exit 1
fi

cd $WORK_DIR
# https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-arm64
curl -Lo ${BINARY} https://github.com/jqlang/jq/releases/download/jq-$VERSION/$BINARY

chmod +x $BINARY
mkdir -p $ROOT/$INSTALL_PATH/
mv $BINARY $ROOT/$INSTALL_PATH/jq
13 changes: 0 additions & 13 deletions hack/install_golang.sh

This file was deleted.

50 changes: 50 additions & 0 deletions hack/update-golang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

SCRIPTS_DIR="$(dirname "$0")"
# Provides $OS,$ARCH,$PLAFORM,$ROOT variables
source "$SCRIPTS_DIR/install-common.sh"
JQ="$ROOT/bin/$PLATFORM/jq"
YQ="$ROOT/bin/$PLATFORM/yq"
GOVERSION=$(go mod edit --json | $JQ -r .Go)

major=`echo $GOVERSION | cut -d. -f1`
minor=`echo $GOVERSION | cut -d. -f2`
revision=`echo $GOVERSION | cut -d. -f3`

echo "----------------------------------------"
echo "Golang version from go.mod: $GOVERSION"
echo "- major: $major"
echo "- minor: $minor"
echo "- revision: $revision"
echo "----------------------------------------"


# update in devcontainer
new_minor_version="$major.$minor"
# use set to update JSON file because JQ doesnt like comments in .json file
dev_container_file=$ROOT/.devcontainer/devcontainer.json
echo "Processing $dev_container_file..."
$SED -E "s|(\"mcr\.microsoft\.com/devcontainers/go:)[^\"]+|\11-$new_minor_version|" $dev_container_file

# update in Dockerfile
dockerfile_file=$ROOT/Dockerfile
echo "Processing $dockerfile_file..."
$SED -E "s|(FROM golang:)[^ ]+|\1$new_minor_version|" $dockerfile_file

# update github actions
actions_directory=$ROOT/.github/workflows
for file in "$actions_directory"/*; do
if [[ -f $file ]]; then
if [[ $($YQ .env.GO_VERSION $file) != "null" ]]; then
echo "Processing $file..."
$YQ -i ".env.GO_VERSION = $new_minor_version" $file
fi
fi
done

# run go mod tidy
go mod tidy

0 comments on commit 83b0d6b

Please sign in to comment.