Skip to content

Commit f7cf24d

Browse files
Copilotmbg
andcommitted
Add Go version update workflow
Co-authored-by: mbg <278086+mbg@users.noreply.github.com>
1 parent c3bafac commit f7cf24d

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
name: Update Go version
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 3 * * 1" # Run weekly on Monday at 3 AM UTC
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
update-go-version:
14+
name: Check and update Go version
15+
if: github.repository == 'github/codeql'
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v5
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Set up Git
25+
run: |
26+
git config user.name "github-actions[bot]"
27+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
28+
29+
- name: Fetch latest Go version
30+
id: fetch-version
31+
run: |
32+
LATEST_GO_VERSION=$(curl -s https://go.dev/dl/\?mode\=json | jq -r '.[0].version')
33+
echo "Latest Go version from go.dev: $LATEST_GO_VERSION"
34+
echo "version=$LATEST_GO_VERSION" >> $GITHUB_OUTPUT
35+
36+
# Extract version numbers (e.g., go1.26.0 -> 1.26.0)
37+
LATEST_VERSION_NUM=$(echo $LATEST_GO_VERSION | sed 's/^go//')
38+
echo "version_num=$LATEST_VERSION_NUM" >> $GITHUB_OUTPUT
39+
40+
# Extract major.minor version (e.g., 1.26.0 -> 1.26)
41+
LATEST_MAJOR_MINOR=$(echo $LATEST_VERSION_NUM | grep -oP '^\d+\.\d+')
42+
echo "major_minor=$LATEST_MAJOR_MINOR" >> $GITHUB_OUTPUT
43+
44+
- name: Check current Go version
45+
id: current-version
46+
run: |
47+
CURRENT_VERSION=$(grep -oP 'go_sdk.download\(version = "\K[^"]+' MODULE.bazel)
48+
echo "Current Go version in MODULE.bazel: $CURRENT_VERSION"
49+
echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
50+
51+
# Extract major.minor version
52+
CURRENT_MAJOR_MINOR=$(echo $CURRENT_VERSION | grep -oP '^\d+\.\d+')
53+
echo "major_minor=$CURRENT_MAJOR_MINOR" >> $GITHUB_OUTPUT
54+
55+
- name: Compare versions
56+
id: compare
57+
run: |
58+
LATEST="${{ steps.fetch-version.outputs.version_num }}"
59+
CURRENT="${{ steps.current-version.outputs.version }}"
60+
61+
echo "Latest: $LATEST"
62+
echo "Current: $CURRENT"
63+
64+
if [ "$LATEST" = "$CURRENT" ]; then
65+
echo "Go version is up to date"
66+
echo "needs_update=false" >> $GITHUB_OUTPUT
67+
else
68+
echo "Go version needs update from $CURRENT to $LATEST"
69+
echo "needs_update=true" >> $GITHUB_OUTPUT
70+
fi
71+
72+
- name: Update Go version in files
73+
if: steps.compare.outputs.needs_update == 'true'
74+
run: |
75+
LATEST_VERSION_NUM="${{ steps.fetch-version.outputs.version_num }}"
76+
LATEST_MAJOR_MINOR="${{ steps.fetch-version.outputs.major_minor }}"
77+
CURRENT_VERSION="${{ steps.current-version.outputs.version }}"
78+
CURRENT_MAJOR_MINOR="${{ steps.current-version.outputs.major_minor }}"
79+
80+
echo "Updating from $CURRENT_VERSION to $LATEST_VERSION_NUM"
81+
82+
# Update MODULE.bazel
83+
sed -i "s/go_sdk.download(version = \"$CURRENT_VERSION\")/go_sdk.download(version = \"$LATEST_VERSION_NUM\")/" MODULE.bazel
84+
85+
# Update go/extractor/go.mod
86+
sed -i "s/^go $CURRENT_MAJOR_MINOR$/go $LATEST_MAJOR_MINOR/" go/extractor/go.mod
87+
sed -i "s/^toolchain go$CURRENT_VERSION$/toolchain go$LATEST_VERSION_NUM/" go/extractor/go.mod
88+
89+
# Update go/extractor/autobuilder/build-environment.go
90+
sed -i "s/var maxGoVersion = util.NewSemVer(\"$CURRENT_MAJOR_MINOR\")/var maxGoVersion = util.NewSemVer(\"$LATEST_MAJOR_MINOR\")/" go/extractor/autobuilder/build-environment.go
91+
92+
# Update go/actions/test/action.yml
93+
sed -i "s/default: \"~$CURRENT_VERSION\"/default: \"~$LATEST_VERSION_NUM\"/" go/actions/test/action.yml
94+
95+
# Show what changed
96+
git diff
97+
98+
- name: Check for changes
99+
id: check-changes
100+
if: steps.compare.outputs.needs_update == 'true'
101+
run: |
102+
if git diff --quiet; then
103+
echo "No changes detected"
104+
echo "has_changes=false" >> $GITHUB_OUTPUT
105+
else
106+
echo "Changes detected"
107+
echo "has_changes=true" >> $GITHUB_OUTPUT
108+
fi
109+
110+
- name: Check for existing PR
111+
if: steps.check-changes.outputs.has_changes == 'true'
112+
id: check-pr
113+
env:
114+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
115+
run: |
116+
BRANCH_NAME="workflow/go-version-update"
117+
PR_NUMBER=$(gh pr list --head "$BRANCH_NAME" --state open --json number --jq '.[0].number // ""')
118+
119+
if [ -n "$PR_NUMBER" ]; then
120+
echo "Existing PR found: #$PR_NUMBER"
121+
echo "pr_exists=true" >> $GITHUB_OUTPUT
122+
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
123+
else
124+
echo "No existing PR found"
125+
echo "pr_exists=false" >> $GITHUB_OUTPUT
126+
fi
127+
128+
- name: Commit and push changes
129+
if: steps.check-changes.outputs.has_changes == 'true'
130+
run: |
131+
BRANCH_NAME="workflow/go-version-update"
132+
LATEST_VERSION_NUM="${{ steps.fetch-version.outputs.version_num }}"
133+
LATEST_MAJOR_MINOR="${{ steps.fetch-version.outputs.major_minor }}"
134+
135+
# Create or switch to branch
136+
git checkout -B "$BRANCH_NAME"
137+
138+
# Stage and commit changes
139+
git add MODULE.bazel go/extractor/go.mod go/extractor/autobuilder/build-environment.go go/actions/test/action.yml
140+
git commit -m "Go: Update to $LATEST_MAJOR_MINOR"
141+
142+
# Push changes
143+
git push -f origin "$BRANCH_NAME"
144+
145+
- name: Create or update PR
146+
if: steps.check-changes.outputs.has_changes == 'true'
147+
env:
148+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
149+
run: |
150+
BRANCH_NAME="workflow/go-version-update"
151+
LATEST_MAJOR_MINOR="${{ steps.fetch-version.outputs.major_minor }}"
152+
CURRENT_MAJOR_MINOR="${{ steps.current-version.outputs.major_minor }}"
153+
154+
PR_TITLE="Go: Update to $LATEST_MAJOR_MINOR"
155+
PR_BODY="This PR updates Go from $CURRENT_MAJOR_MINOR to $LATEST_MAJOR_MINOR.
156+
157+
Updated files:
158+
- \`MODULE.bazel\` - go_sdk.download version
159+
- \`go/extractor/go.mod\` - go directive and toolchain
160+
- \`go/extractor/autobuilder/build-environment.go\` - maxGoVersion
161+
- \`go/actions/test/action.yml\` - default go-test-version
162+
163+
This PR was automatically created by the [Go version update workflow](.github/workflows/go-version-update.yml)."
164+
165+
if [ "${{ steps.check-pr.outputs.pr_exists }}" = "true" ]; then
166+
echo "Updating existing PR #${{ steps.check-pr.outputs.pr_number }}"
167+
gh pr edit "${{ steps.check-pr.outputs.pr_number }}" --title "$PR_TITLE" --body "$PR_BODY"
168+
else
169+
echo "Creating new PR"
170+
gh pr create \
171+
--title "$PR_TITLE" \
172+
--body "$PR_BODY" \
173+
--base main \
174+
--head "$BRANCH_NAME" \
175+
--label "Go"
176+
fi

0 commit comments

Comments
 (0)