Skip to content

Commit 1f7ec2d

Browse files
korniltsevJunie+Tolyan
andauthored
Refactor workflows to streamline Go version update (#4101)
Rearranged steps in update-examples-cron workflow and added a dedicated step for Go version upgrades. Introduced scripts for fetching the latest Go patch version and updating build files automatically. Improved automation for managing Golang updates and generating corresponding pull requests. Co-authored-by: Junie+Tolyan <[email protected]>
1 parent f41fcbc commit 1f7ec2d

File tree

4 files changed

+96
-13
lines changed

4 files changed

+96
-13
lines changed

.github/workflows/update-examples-cron.yml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ jobs:
1313
update-examples-cron:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v4
17-
with:
18-
persist-credentials: false
19-
2016
- id: get-secrets
2117
uses: grafana/shared-workflows/actions/get-vault-secrets@main
2218
with:
@@ -34,12 +30,34 @@ jobs:
3430
installation_retrieval_payload: ${{ env.GITHUB_APP_INSTALLATION_ID }}
3531
private_key: ${{ env.GITHUB_APP_PRIVATE_KEY }}
3632

37-
- run: |
33+
- uses: actions/checkout@v4
34+
with:
35+
persist-credentials: false
36+
clean: true
37+
38+
- name: "Update examples"
39+
run: |
3840
make tools/update_examples
3941
if ! git diff --exit-code;
4042
then
4143
make tools/update_examples_pr
4244
fi
4345
env:
4446
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
45-
GITHUB_REPOSITORY: ${{ github.repository }}
47+
GITHUB_REPOSITORY: ${{ github.repository }}
48+
49+
- uses: actions/checkout@v4
50+
with:
51+
persist-credentials: false
52+
clean: true
53+
54+
- name: "Upgrade golang"
55+
run: |
56+
./tools/upgrade-go-version.sh
57+
if ! git diff --exit-code ${{ github.sha }};
58+
then
59+
make tools/update_golang_pr
60+
fi
61+
env:
62+
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
63+
GITHUB_REPOSITORY: ${{ github.repository }}

Makefile.examples

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ tools/update_examples_pr:
1616
gh pr create --head gh_bot_examples_update --title "chore(examples): update examples" --body "make tools/update_examples" --repo $(GITHUB_REPOSITORY) || \
1717
gh pr edit gh_bot_examples_update --title "chore(examples): update examples" --body "make tools/update_examples" --repo $(GITHUB_REPOSITORY)
1818

19+
.PHONY: tools/update_golang_pr
20+
tools/update_golang_pr:
21+
git config --local user.name 'Pyroscope Bot'
22+
git config --local user.email '[email protected]'
23+
git checkout -b gh_bot_golang_update
24+
git push -f https://x-access-token:$(GITHUB_TOKEN)@github.com/$(GITHUB_REPOSITORY).git gh_bot_golang_update
25+
gh pr create --head gh_bot_golang_update --title "chore: $(git log -1 --pretty=%B)" --body "" --repo $(GITHUB_REPOSITORY) || \
26+
gh pr edit gh_bot_golang_update --title "chore: $(git log -1 --pretty=%B)" --body "" --repo $(GITHUB_REPOSITORY)
1927

2028
.PHONY: rideshare/docker/push
2129
rideshare/docker/push: IMAGE_PREFIX := us-docker.pkg.dev/grafanalabs-dev/docker-pyroscope-dev/

tools/get-latest-go-version.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Check if GITHUB_TOKEN is provided
6+
if [ -z "${GITHUB_TOKEN:-}" ]; then
7+
echo "Error: GITHUB_TOKEN environment variable is required" >&2
8+
exit 1
9+
fi
10+
11+
# Extract current Go version from go.mod
12+
CURRENT_VERSION=$(grep -E "^go [0-9]+\.[0-9]+\.[0-9]+" go.mod | awk '{print $2}')
13+
14+
if [ -z "$CURRENT_VERSION" ]; then
15+
echo "Error: Could not determine Go version from go.mod" >&2
16+
exit 1
17+
fi
18+
19+
# Extract major and minor version
20+
MAJOR_MINOR=$(echo "$CURRENT_VERSION" | cut -d. -f1-2)
21+
22+
echo "Current Go version in go.mod: $CURRENT_VERSION" >&2
23+
echo "Fetching latest patch version for $MAJOR_MINOR.x..." >&2
24+
25+
# Fetch the latest patch version from golang/go repository
26+
ALL_TAGS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" "https://api.github.com/repos/golang/go/git/refs/tags")
27+
LATEST_VERSION=$(echo ${ALL_TAGS}|
28+
grep -o '"ref": "refs/tags/go[0-9]\+\.[0-9]\+\.[0-9]\+"' |
29+
grep "go$MAJOR_MINOR" |
30+
sort -V |
31+
tail -n 1 |
32+
grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+')
33+
34+
if [ -z "$LATEST_VERSION" ]; then
35+
echo "Error: Could not determine latest patch version for $MAJOR_MINOR.x" >&2
36+
exit 1
37+
fi
38+
39+
echo "Debug: Latest patch version for $MAJOR_MINOR.x: $LATEST_VERSION" >&2
40+
echo "$LATEST_VERSION"

tools/upgrade-go-version.sh

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,39 @@
22

33
set -euo pipefail
44

5-
if [ $# -ne 1 ]; then
6-
echo "Usage: $0 <version>"
7-
exit 1
5+
# If no arguments provided, determine the version from go.mod and fetch the latest patch version
6+
if [ $# -eq 0 ]; then
7+
# Use the get-latest-go-version.sh script to get the latest patch version
8+
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
9+
VERSION=$("$SCRIPT_DIR/get-latest-go-version.sh")
10+
11+
if [ -z "$VERSION" ]; then
12+
echo "Error: Could not determine latest patch version"
13+
exit 1
14+
fi
15+
else
16+
# Use the provided version
17+
if [ $# -ne 1 ]; then
18+
echo "Usage: $0 [version]"
19+
echo "If version is not provided, the latest patch version matching the current go.mod minor version will be used."
20+
exit 1
21+
fi
22+
VERSION="$1"
823
fi
924

25+
echo "Updating Go version to $VERSION"
26+
1027
# update ci/cd versions
11-
git ls-files .github/workflows | xargs sed -i 's/go-version:\([ \["]\+\)\([0-9\.\=<>]\+\)/go-version:\1'$1'/g'
28+
git ls-files .github/workflows | xargs sed -i 's/go-version:\([ \["]\+\)\([0-9\.\=<>]\+\)/go-version:\1'$VERSION'/g'
1229

1330
# update goreleaser check
14-
sed -i 's/go version go[0-9\.]\+/go version go'$1'/g' .goreleaser.yaml
31+
sed -i 's/go version go[0-9\.]\+/go version go'$VERSION'/g' .goreleaser.yaml
1532

1633
# update all dockerfile versions, skips the elf tests from ebpf
1734
DOCKER_FILES=$(git ls-files '**/Dockerfile*' | grep -v ebpf/symtab/elf/testdata/Dockerfile)
18-
sed -i 's/golang:[0-9\.]\+/golang:'$1'/g' $DOCKER_FILES
35+
sed -i 's/golang:[0-9\.]\+/golang:'$VERSION'/g' $DOCKER_FILES
1936
git add -u $DOCKER_FILES
2037

2138
# add changes
2239
git add -u .github/workflows .goreleaser.yaml
23-
git commit -m "Update golang version to $1"
40+
git commit -m "Update golang version to $VERSION"

0 commit comments

Comments
 (0)