Skip to content

Commit 3f099ae

Browse files
authored
Sync go.mod toolchain with Dockerfile (#724)
Automate updating the go.mod `toolchain` when Dependabot (or anyone else) bumps the Dockerfile go version Avoid needing manual PRs like #723 Tested by pushing an update to Dockerfile. I'm not 100% sure this will work with 3rd party PRs (i.e. Dependabot) but we will find out soon enough.
1 parent 57818f5 commit 3f099ae

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Sync Go Toolchain
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "Dockerfile"
7+
8+
jobs:
9+
sync:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
14+
steps:
15+
- uses: actions/checkout@v6
16+
with:
17+
ref: ${{ github.head_ref }}
18+
19+
- uses: actions/setup-go@v6
20+
with:
21+
go-version-file: go.mod
22+
23+
- name: Extract Go version from Dockerfile
24+
id: dockerfile
25+
run: |
26+
VERSION=$(sed -n 's/^FROM golang:\([0-9.]*\).*/\1/p' Dockerfile | head -1)
27+
echo "version=$VERSION" >> $GITHUB_OUTPUT
28+
echo "Dockerfile Go version: $VERSION"
29+
30+
- name: Extract current toolchain from go.mod
31+
id: gomod
32+
run: |
33+
TOOLCHAIN=$(go mod edit -json | jq -r '.Toolchain // empty')
34+
echo "toolchain=$TOOLCHAIN" >> $GITHUB_OUTPUT
35+
echo "Current go.mod toolchain: $TOOLCHAIN"
36+
37+
- name: Update go.mod toolchain if needed
38+
if: steps.gomod.outputs.toolchain != format('go{0}', steps.dockerfile.outputs.version)
39+
env:
40+
NEW_VERSION: ${{ steps.dockerfile.outputs.version }}
41+
run: |
42+
echo "Updating toolchain to go${NEW_VERSION}"
43+
go mod edit -toolchain="go${NEW_VERSION}"
44+
45+
- name: Commit and push changes
46+
if: steps.gomod.outputs.toolchain != format('go{0}', steps.dockerfile.outputs.version)
47+
env:
48+
NEW_VERSION: ${{ steps.dockerfile.outputs.version }}
49+
run: |
50+
git config user.name "github-actions[bot]"
51+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
52+
git add go.mod
53+
git commit -m "Sync go.mod toolchain with Dockerfile (go${NEW_VERSION})"
54+
git push

0 commit comments

Comments
 (0)