Skip to content

Commit 5ed78f8

Browse files
committed
add action to bump .go-version file
1 parent 3e8a2a2 commit 5ed78f8

4 files changed

Lines changed: 541 additions & 0 deletions

File tree

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
name: Test Bump Go Version Action
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'bump-go-version-file/**'
7+
- '.github/workflows/test-bump-go-version.yaml'
8+
9+
permissions: {}
10+
11+
jobs:
12+
test-dry-run:
13+
runs-on: ubuntu-slim
14+
15+
permissions:
16+
contents: read
17+
18+
steps:
19+
- uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
20+
with:
21+
egress-policy: audit
22+
23+
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
24+
with:
25+
persist-credentials: false
26+
27+
- name: Create test .go-version file
28+
run: |
29+
echo "1.20.0" > .go-version
30+
cat .go-version
31+
32+
- name: Test action in dry-run mode
33+
id: test-dry-run
34+
uses: ./bump-go-version-file
35+
with:
36+
go-version-file: '.go-version'
37+
token: ${{ secrets.GITHUB_TOKEN }}
38+
dry-run: 'true'
39+
40+
- name: Verify outputs
41+
run: |
42+
echo "Old version: ${{ steps.test-dry-run.outputs.old-version }}"
43+
echo "New version: ${{ steps.test-dry-run.outputs.new-version }}"
44+
if [ -z "${{ steps.test-dry-run.outputs.old-version }}" ]; then
45+
echo "Error: old-version output is empty"
46+
exit 1
47+
fi
48+
if [ -z "${{ steps.test-dry-run.outputs.new-version }}" ]; then
49+
echo "Error: new-version output is empty"
50+
exit 1
51+
fi
52+
echo "✅ Outputs are valid"
53+
54+
- name: Check file was updated
55+
run: |
56+
CURRENT=$(cat .go-version | tr -d '[:space:]')
57+
NEW_VERSION="${{ steps.test-dry-run.outputs.new-version }}"
58+
echo "Current version in file: $CURRENT"
59+
echo "Expected new version: $NEW_VERSION"
60+
if [ "$CURRENT" != "$NEW_VERSION" ]; then
61+
echo "Error: File was not updated correctly"
62+
exit 1
63+
fi
64+
echo "✅ File was updated correctly"
65+
66+
test-already-up-to-date:
67+
runs-on: ubuntu-slim
68+
69+
permissions:
70+
contents: read
71+
72+
steps:
73+
- uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
74+
with:
75+
egress-policy: audit
76+
77+
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
78+
with:
79+
persist-credentials: false
80+
81+
- name: Get latest Go version
82+
id: get-latest
83+
run: |
84+
LATEST=$(curl -s https://go.dev/VERSION?m=text | head -n1 | sed 's/go//')
85+
echo "latest=$LATEST" >> $GITHUB_OUTPUT
86+
echo "Latest Go version: $LATEST"
87+
88+
- name: Create up-to-date .go-version file
89+
run: |
90+
echo "${{ steps.get-latest.outputs.latest }}" > .go-version
91+
cat .go-version
92+
93+
- name: Test action with up-to-date version
94+
id: test-uptodate
95+
uses: ./bump-go-version-file
96+
with:
97+
go-version-file: '.go-version'
98+
token: ${{ secrets.GITHUB_TOKEN }}
99+
dry-run: 'true'
100+
101+
- name: Verify no PR would be created
102+
run: |
103+
echo "Old version: ${{ steps.test-uptodate.outputs.old-version }}"
104+
echo "New version: ${{ steps.test-uptodate.outputs.new-version }}"
105+
if [ "${{ steps.test-uptodate.outputs.old-version }}" != "${{ steps.test-uptodate.outputs.new-version }}" ]; then
106+
echo "Error: Versions should be the same"
107+
exit 1
108+
fi
109+
echo "✅ Version is already up to date"
110+
111+
test-custom-file-path:
112+
runs-on: ubuntu-slim
113+
114+
permissions:
115+
contents: read
116+
117+
steps:
118+
- uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
119+
with:
120+
egress-policy: audit
121+
122+
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
123+
with:
124+
persist-credentials: false
125+
126+
- name: Create custom path .go-version file
127+
run: |
128+
mkdir -p .github
129+
echo "1.19.0" > .github/go-version
130+
cat .github/go-version
131+
132+
- name: Test action with custom file path
133+
id: test-custom
134+
uses: ./bump-go-version-file
135+
with:
136+
go-version-file: '.github/go-version'
137+
token: ${{ secrets.GITHUB_TOKEN }}
138+
dry-run: 'true'
139+
140+
- name: Verify custom file was updated
141+
run: |
142+
CURRENT=$(cat .github/go-version | tr -d '[:space:]')
143+
NEW_VERSION="${{ steps.test-custom.outputs.new-version }}"
144+
echo "Current version in custom file: $CURRENT"
145+
echo "Expected new version: $NEW_VERSION"
146+
if [ "$CURRENT" != "$NEW_VERSION" ]; then
147+
echo "Error: Custom file was not updated correctly"
148+
exit 1
149+
fi
150+
echo "✅ Custom file path works correctly"
151+
152+
test-missing-file:
153+
runs-on: ubuntu-slim
154+
155+
permissions:
156+
contents: read
157+
158+
steps:
159+
- uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
160+
with:
161+
egress-policy: audit
162+
163+
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
164+
with:
165+
persist-credentials: false
166+
167+
- name: Test action with missing file (should fail)
168+
id: test-missing
169+
continue-on-error: true
170+
uses: ./bump-go-version-file
171+
with:
172+
go-version-file: '.go-version-missing'
173+
token: ${{ secrets.GITHUB_TOKEN }}
174+
dry-run: 'true'
175+
176+
- name: Verify failure
177+
run: |
178+
if [ "${{ steps.test-missing.outcome }}" != "failure" ]; then
179+
echo "Error: Action should have failed with missing file"
180+
exit 1
181+
fi
182+
echo "✅ Action correctly fails with missing file"

bump-go-version-file/README.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Bump Go Version File Action
2+
3+
This GitHub Action automatically checks for the latest stable Go version and updates your `.go-version` file if needed, then creates a pull request with the changes.
4+
5+
## Features
6+
7+
- 🔍 Automatically fetches the latest stable Go version from go.dev
8+
- 📝 Updates your `.go-version` file when a new version is available
9+
- 🔄 Creates or updates a pull request with the version bump
10+
- ⚙️ Fully customizable PR titles, messages, and branch names
11+
- 🎯 Simple and focused - does one thing well
12+
13+
## Usage
14+
15+
### Basic Usage
16+
17+
```yaml
18+
name: Bump Go Version
19+
on:
20+
schedule:
21+
- cron: '0 0 * * 1' # Run every Monday at midnight
22+
workflow_dispatch: # Allow manual triggers
23+
24+
jobs:
25+
bump-go:
26+
runs-on: ubuntu-latest
27+
permissions:
28+
contents: write
29+
pull-requests: write
30+
steps:
31+
- uses: actions/checkout@v4
32+
33+
- uses: chainguard-dev/actions/bump-go-version-file@main
34+
with:
35+
token: ${{ secrets.GITHUB_TOKEN }}
36+
```
37+
38+
### Advanced Usage
39+
40+
```yaml
41+
name: Bump Go Version
42+
on:
43+
schedule:
44+
- cron: '0 0 * * 1'
45+
workflow_dispatch:
46+
47+
jobs:
48+
bump-go:
49+
runs-on: ubuntu-latest
50+
permissions:
51+
contents: write
52+
pull-requests: write
53+
steps:
54+
- uses: actions/checkout@v4
55+
56+
- uses: chainguard-dev/actions/bump-go-version-file@main
57+
with:
58+
go-version-file: '.go-version'
59+
token: ${{ secrets.GITHUB_TOKEN }}
60+
branch-for-pr: 'automated/bump-go-version'
61+
title-for-pr: '🚀 Update Go version'
62+
description-for-pr: |
63+
## Go Version Update
64+
65+
This PR updates the Go version to the latest stable release.
66+
67+
Please review and merge if tests pass.
68+
commit-message: 'chore(deps): bump Go version'
69+
labels-for-pr: 'automated pr, dependencies, go'
70+
base-branch: 'main'
71+
author: '${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com>'
72+
signoff: true
73+
```
74+
75+
### Dry-run Mode (Testing)
76+
77+
Use dry-run mode to test the action without creating a PR:
78+
79+
```yaml
80+
- uses: chainguard-dev/actions/bump-go-version-file@main
81+
with:
82+
token: ${{ secrets.GITHUB_TOKEN }}
83+
dry-run: 'true'
84+
```
85+
86+
In dry-run mode, the action will:
87+
- Check for Go version updates
88+
- Show what changes would be made
89+
- Display the diff in the workflow summary
90+
- **Not** create a pull request
91+
92+
## Inputs
93+
94+
| Input | Description | Required | Default |
95+
|-------|-------------|----------|---------|
96+
| `go-version-file` | Path to the go version file | No | `.go-version` |
97+
| `token` | GITHUB_TOKEN or a `repo` scoped PAT | Yes | `${{ github.token }}` |
98+
| `signoff` | Add `Signed-off-by` line to commit message | No | `false` |
99+
| `author` | Author name and email in format `Name <email>` | No | `${{ github.actor }} <...>` |
100+
| `committer` | Committer name and email in format `Name <email>` | No | `github-actions[bot] <...>` |
101+
| `labels-for-pr` | Comma or newline separated list of PR labels | No | `automated pr, dependencies` |
102+
| `branch-for-pr` | Branch name for the PR | No | `bump-go-version` |
103+
| `title-for-pr` | PR title | No | `chore: bump Go version` |
104+
| `description-for-pr` | PR body/description | No | See action.yml |
105+
| `commit-message` | Commit message | No | `chore: bump Go version` |
106+
| `base-branch` | Base branch for the PR | No | `main` |
107+
| `dry-run` | Run in dry-run mode (show changes without creating PR) | No | `false` |
108+
109+
## Outputs
110+
111+
| Output | Description |
112+
|--------|-------------|
113+
| `pull_request_number` | The PR number if created |
114+
| `old-version` | The old Go version (e.g., `1.23.4`) |
115+
| `new-version` | The new Go version (e.g., `1.24.0`) |
116+
117+
## Example `.go-version` File
118+
119+
Your repository should contain a `.go-version` file with just the Go version:
120+
121+
```
122+
1.23.4
123+
```
124+
125+
The action will update this file to the latest stable version when available.
126+
127+
## Permissions
128+
129+
Make sure your workflow has the necessary permissions:
130+
131+
```yaml
132+
permissions:
133+
contents: write # To push the branch
134+
pull-requests: write # To create PRs
135+
```
136+
137+
## How It Works
138+
139+
1. The action fetches the latest stable Go version from `https://go.dev/VERSION?m=text`
140+
2. It reads your `.go-version` file and compares the versions
141+
3. If the versions match, it exits without making changes
142+
4. If an update is available:
143+
- Updates the `.go-version` file
144+
- Checks if there are any changes using `git diff`
145+
- Uses `peter-evans/create-pull-request` to create/update a PR
146+
- Automatically includes the diff in the PR description
147+
148+
## Tips
149+
150+
- Use the `workflow_dispatch` trigger to manually run the action
151+
- Schedule the workflow to run weekly or monthly to stay up to date
152+
- Customize the PR labels and description to match your team's conventions
153+
- The action uses `peter-evans/create-pull-request` which handles branch management automatically
154+
- If a PR already exists, it will be updated with the new changes
155+
156+
## Example Output
157+
158+
When a new version is found, the action will:
159+
- Update `.go-version` from `1.23.4` to `1.24.0`
160+
- Create/update a PR with title like "chore: bump Go version (from 1.23.4 to 1.24.0)"
161+
- Include the old version, new version, and diff in the PR description
162+
- Output the PR number for use in subsequent workflow steps

0 commit comments

Comments
 (0)