Skip to content

Commit f91d9d1

Browse files
committed
add release workflow
1 parent 1fdafe3 commit f91d9d1

File tree

4 files changed

+175
-2
lines changed

4 files changed

+175
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
go-version: ["1.25.x"]
18+
go-version: ["1.25.1"]
1919

2020
steps:
2121
- uses: actions/checkout@v4

.github/workflows/release.yml

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
name: Release
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
release_type:
7+
description: "Type of release"
8+
required: true
9+
default: "patch"
10+
type: choice
11+
options:
12+
- major
13+
- minor
14+
- patch
15+
prerelease:
16+
description: "Is this a pre-release?"
17+
required: false
18+
default: false
19+
type: boolean
20+
21+
permissions:
22+
contents: write
23+
packages: write
24+
25+
jobs:
26+
release:
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v4
31+
with:
32+
# Fetch full history for changelog generation
33+
fetch-depth: 0
34+
35+
- name: Set up Go
36+
uses: actions/setup-go@v5
37+
with:
38+
go-version: "1.25.1"
39+
40+
- name: Cache Go modules
41+
uses: actions/cache@v4
42+
with:
43+
path: ~/go/pkg/mod
44+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
45+
restore-keys: |
46+
${{ runner.os }}-go-
47+
48+
- name: Download dependencies
49+
run: go mod download
50+
51+
- name: Run tests
52+
run: go test -v ./...
53+
54+
- name: Calculate next version
55+
id: version
56+
run: |
57+
# Get the latest tag, default to v0.0.0 if no tags exist
58+
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
59+
echo "Latest tag: $LATEST_TAG"
60+
61+
# Remove 'v' prefix and split version parts
62+
VERSION=${LATEST_TAG#v}
63+
IFS='.' read -ra VERSION_PARTS <<< "$VERSION"
64+
MAJOR=${VERSION_PARTS[0]:-0}
65+
MINOR=${VERSION_PARTS[1]:-0}
66+
PATCH=${VERSION_PARTS[2]:-0}
67+
68+
# Remove any pre-release suffix from patch version
69+
PATCH=$(echo $PATCH | cut -d'-' -f1)
70+
71+
echo "Current version: $MAJOR.$MINOR.$PATCH"
72+
73+
# Calculate next version based on release type
74+
case "${{ github.event.inputs.release_type }}" in
75+
"major")
76+
MAJOR=$((MAJOR + 1))
77+
MINOR=0
78+
PATCH=0
79+
;;
80+
"minor")
81+
MINOR=$((MINOR + 1))
82+
PATCH=0
83+
;;
84+
"patch")
85+
PATCH=$((PATCH + 1))
86+
;;
87+
esac
88+
89+
NEXT_VERSION="v$MAJOR.$MINOR.$PATCH"
90+
echo "Next version: $NEXT_VERSION"
91+
echo "tag=$NEXT_VERSION" >> $GITHUB_OUTPUT
92+
93+
- name: Check if calculated tag already exists
94+
run: |
95+
if git rev-parse "refs/tags/${{ steps.version.outputs.tag }}" >/dev/null 2>&1; then
96+
echo "Error: Tag ${{ steps.version.outputs.tag }} already exists"
97+
exit 1
98+
fi
99+
100+
- name: Create and push tag
101+
run: |
102+
git config user.name "github-actions[bot]"
103+
git config user.email "github-actions[bot]@users.noreply.github.com"
104+
git tag -a "${{ steps.version.outputs.tag }}" -m "Release ${{ steps.version.outputs.tag }}"
105+
git push origin "${{ steps.version.outputs.tag }}"
106+
107+
- name: Run GoReleaser
108+
uses: goreleaser/goreleaser-action@v6
109+
with:
110+
distribution: goreleaser
111+
version: "~> v2"
112+
args: release --clean
113+
env:
114+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
115+
GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.tag }}
116+
117+
- name: Mark as pre-release if specified
118+
if: ${{ github.event.inputs.prerelease == 'true' }}
119+
run: |
120+
# Get the release ID and mark it as pre-release
121+
release_id=$(gh api repos/${{ github.repository }}/releases/tags/${{ steps.version.outputs.tag }} --jq '.id')
122+
gh api repos/${{ github.repository }}/releases/$release_id \
123+
--method PATCH \
124+
--field prerelease=true
125+
env:
126+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
127+
128+
- name: Release Summary
129+
run: |
130+
echo "🎉 Release ${{ steps.version.outputs.tag }} has been successfully created!"
131+
echo "📦 View the release: https://github.com/${{ github.repository }}/releases/tag/${{ steps.version.outputs.tag }}"
132+
echo "🔄 Release type: ${{ github.event.inputs.release_type }}"
133+
if [[ "${{ github.event.inputs.prerelease }}" == "true" ]]; then
134+
echo "⚠️ This is marked as a pre-release"
135+
fi

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ go.work
2424

2525
# Temporary files
2626
tmp
27-
*_tmp.go
27+
*_tmp.go
28+
29+
# Added by goreleaser init:
30+
dist/

.goreleaser.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: 2
2+
3+
before:
4+
hooks:
5+
- go mod tidy
6+
7+
builds:
8+
- env:
9+
- CGO_ENABLED=0
10+
goos:
11+
- linux
12+
- windows
13+
- darwin
14+
15+
archives:
16+
- formats: [tar.gz]
17+
# this name template makes the OS and Arch compatible with the results of `uname`.
18+
name_template: >-
19+
{{ .ProjectName }}_
20+
{{- title .Os }}_
21+
{{- if eq .Arch "amd64" }}x86_64
22+
{{- else if eq .Arch "386" }}i386
23+
{{- else }}{{ .Arch }}{{ end }}
24+
{{- if .Arm }}v{{ .Arm }}{{ end }}
25+
# use zip for windows archives
26+
format_overrides:
27+
- goos: windows
28+
formats: [zip]
29+
30+
changelog:
31+
sort: asc
32+
filters:
33+
exclude:
34+
- "^docs:"
35+
- "^test:"

0 commit comments

Comments
 (0)