Skip to content

Commit 0419b5d

Browse files
committed
feat: add automation workflow to check for new Golang releases and update Dockerfile
fixes #11
1 parent 5acf53b commit 0419b5d

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
name: Check for new Golang releases
2+
3+
on:
4+
schedule:
5+
# Run every 6 hours
6+
- cron: '0 */6 * * *'
7+
workflow_dispatch: # Allow manual trigger
8+
9+
jobs:
10+
check-golang-version:
11+
runs-on: ubuntu-latest
12+
outputs:
13+
new-version: ${{ steps.check.outputs.new-version }}
14+
version: ${{ steps.check.outputs.version }}
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Check for new Go version
20+
id: check
21+
run: |
22+
# Get the latest stable Go version from official Go download page
23+
LATEST_VERSION=$(curl -s https://go.dev/VERSION?m=text | head -n1 | sed 's/go//')
24+
echo "Latest Go version: $LATEST_VERSION"
25+
26+
# Get current version from Dockerfile
27+
CURRENT_VERSION=$(grep -oP 'FROM golang:\K[0-9]+\.[0-9]+\.[0-9]+' Dockerfile | head -n1)
28+
echo "Current Dockerfile version: $CURRENT_VERSION"
29+
30+
# Compare versions
31+
if [ "$LATEST_VERSION" != "$CURRENT_VERSION" ]; then
32+
echo "New version detected: $LATEST_VERSION"
33+
echo "new-version=true" >> $GITHUB_OUTPUT
34+
echo "version=$LATEST_VERSION" >> $GITHUB_OUTPUT
35+
else
36+
echo "No new version"
37+
echo "new-version=false" >> $GITHUB_OUTPUT
38+
fi
39+
40+
- name: Create issue for new version
41+
if: steps.check.outputs.new-version == 'true'
42+
uses: actions/github-script@v7
43+
with:
44+
script: |
45+
const version = '${{ steps.check.outputs.version }}';
46+
const issues = await github.rest.issues.listForRepo({
47+
owner: context.repo.owner,
48+
repo: context.repo.repo,
49+
state: 'open',
50+
labels: 'golang-update'
51+
});
52+
53+
// Check if issue already exists
54+
const existingIssue = issues.data.find(issue =>
55+
issue.title.includes(version)
56+
);
57+
58+
if (!existingIssue) {
59+
await github.rest.issues.create({
60+
owner: context.repo.owner,
61+
repo: context.repo.repo,
62+
title: `New Golang version ${version} available`,
63+
body: `A new Golang version **${version}** has been released.\n\nWorkflow will automatically build and push the new Docker image.`,
64+
labels: ['golang-update', 'automation']
65+
});
66+
}
67+
68+
build-and-push:
69+
needs: check-golang-version
70+
if: needs.check-golang-version.outputs.new-version == 'true'
71+
runs-on: ubuntu-latest
72+
permissions:
73+
contents: write
74+
packages: write
75+
steps:
76+
- name: Checkout repository
77+
uses: actions/checkout@v4
78+
with:
79+
token: ${{ secrets.GITHUB_TOKEN }}
80+
81+
- name: Update Dockerfile
82+
run: |
83+
NEW_VERSION="${{ needs.check-golang-version.outputs.version }}"
84+
sed -i "s/FROM golang:[0-9]\+\.[0-9]\+\.[0-9]\+/FROM golang:${NEW_VERSION}/" Dockerfile
85+
echo "Updated Dockerfile to Go version ${NEW_VERSION}"
86+
87+
- name: Set up QEMU
88+
uses: docker/setup-qemu-action@v3
89+
90+
- name: Set up Docker Buildx
91+
uses: docker/setup-buildx-action@v3
92+
93+
- name: Login to Docker Hub
94+
uses: docker/login-action@v3
95+
with:
96+
username: ${{ secrets.DOCKER_USERNAME }}
97+
password: ${{ secrets.DOCKER_PASSWORD }}
98+
99+
- name: Login to GitHub Container Registry
100+
uses: docker/login-action@v3
101+
with:
102+
registry: ghcr.io
103+
username: ${{ github.actor }}
104+
password: ${{ secrets.GITHUB_TOKEN }}
105+
106+
- name: Extract metadata for Docker
107+
id: meta
108+
uses: docker/metadata-action@v5
109+
with:
110+
images: |
111+
devopsworks/golang-upx
112+
ghcr.io/${{ github.repository }}
113+
tags: |
114+
type=raw,value=${{ needs.check-golang-version.outputs.version }}
115+
type=raw,value=latest
116+
117+
- name: Build and push Docker image
118+
uses: docker/build-push-action@v5
119+
with:
120+
context: .
121+
platforms: linux/amd64,linux/arm64
122+
push: true
123+
tags: ${{ steps.meta.outputs.tags }}
124+
labels: ${{ steps.meta.outputs.labels }}
125+
cache-from: type=gha
126+
cache-to: type=gha,mode=max
127+
128+
- name: Commit updated Dockerfile
129+
run: |
130+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
131+
git config --local user.name "github-actions[bot]"
132+
git add Dockerfile
133+
git commit -m "chore: update Golang to version ${{ needs.check-golang-version.outputs.version }}" || echo "No changes to commit"
134+
git push || echo "No changes to push"
135+
136+
- name: Create Release
137+
uses: actions/github-script@v7
138+
with:
139+
script: |
140+
const version = '${{ needs.check-golang-version.outputs.version }}';
141+
await github.rest.repos.createRelease({
142+
owner: context.repo.owner,
143+
repo: context.repo.repo,
144+
tag_name: `v${version}`,
145+
name: `Golang ${version}`,
146+
body: `Automated release for Golang version ${version}\n\n- Updated base image to golang:${version}\n- Built for linux/amd64 and linux/arm64`,
147+
draft: false,
148+
prerelease: false
149+
});

0 commit comments

Comments
 (0)