-
Notifications
You must be signed in to change notification settings - Fork 4
248 lines (218 loc) · 7.08 KB
/
Copy pathrelease.yml
File metadata and controls
248 lines (218 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
name: Release
on:
workflow_run:
workflows: ["CI"]
branches: [main]
types: [completed]
workflow_dispatch:
# Prevent concurrent releases
concurrency:
group: release
cancel-in-progress: false
jobs:
check:
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.check.outputs.should_release }}
next_version: ${{ steps.version.outputs.next_version }}
changelog: ${{ steps.changelog.outputs.changelog }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check if release needed
id: check
run: |
# Skip if the last commit is a release commit (avoid infinite loop)
LAST_MSG=$(git log -1 --pretty=%s)
if [[ "$LAST_MSG" == chore:\ release* ]]; then
echo "should_release=false" >> $GITHUB_OUTPUT
exit 0
fi
# Skip if no meaningful changes since last tag
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
echo "should_release=true" >> $GITHUB_OUTPUT
exit 0
fi
# Check for feat/fix/refactor commits since last tag
COMMITS=$(git log ${LAST_TAG}..HEAD --pretty=%s)
if echo "$COMMITS" | grep -qE '^(feat|fix|refactor|perf|breaking)'; then
echo "should_release=true" >> $GITHUB_OUTPUT
else
echo "should_release=false" >> $GITHUB_OUTPUT
fi
- name: Determine next version
if: steps.check.outputs.should_release == 'true'
id: version
run: |
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "Last tag: $LAST_TAG"
# Parse semver
VERSION=${LAST_TAG#v}
MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
PATCH=$(echo $VERSION | cut -d. -f3)
# Determine bump type from commits
COMMITS=$(git log ${LAST_TAG}..HEAD --pretty=%s 2>/dev/null || git log --pretty=%s)
if echo "$COMMITS" | grep -qiE '^breaking|BREAKING CHANGE'; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
elif echo "$COMMITS" | grep -qE '^feat'; then
MINOR=$((MINOR + 1))
PATCH=0
else
PATCH=$((PATCH + 1))
fi
NEXT="v${MAJOR}.${MINOR}.${PATCH}"
echo "Next version: $NEXT"
echo "next_version=$NEXT" >> $GITHUB_OUTPUT
- name: Generate changelog
if: steps.check.outputs.should_release == 'true'
id: changelog
run: |
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
RANGE="HEAD"
else
RANGE="${LAST_TAG}..HEAD"
fi
{
echo 'changelog<<CHANGELOG_EOF'
# Features
FEATS=$(git log $RANGE --pretty="- %s (%h)" --grep="^feat" --extended-regexp 2>/dev/null)
if [ -n "$FEATS" ]; then
echo "### Features"
echo "$FEATS"
echo ""
fi
# Fixes
FIXES=$(git log $RANGE --pretty="- %s (%h)" --grep="^fix" --extended-regexp 2>/dev/null)
if [ -n "$FIXES" ]; then
echo "### Bug Fixes"
echo "$FIXES"
echo ""
fi
# Other changes (refactor, perf, etc.)
OTHERS=$(git log $RANGE --pretty="- %s (%h)" --grep="^refactor|^perf|^chore|^ci|^docs|^test|^build" --extended-regexp 2>/dev/null)
if [ -n "$OTHERS" ]; then
echo "### Other Changes"
echo "$OTHERS"
echo ""
fi
echo "### Platforms"
echo "- Linux (amd64, arm64)"
echo "- macOS (amd64, arm64)"
echo "- Windows (amd64, arm64)"
echo 'CHANGELOG_EOF'
} >> $GITHUB_OUTPUT
build:
needs: check
if: needs.check.outputs.should_release == 'true'
strategy:
matrix:
include:
- platform: ubuntu-latest
goos: linux
goarch: amd64
can_test: true
- platform: ubuntu-latest
goos: linux
goarch: arm64
can_test: false
- platform: windows-latest
goos: windows
goarch: amd64
can_test: true
- platform: windows-latest
goos: windows
goarch: arm64
can_test: false
- platform: macos-latest
goos: darwin
goarch: amd64
can_test: false
- platform: macos-latest
goos: darwin
goarch: arm64
can_test: true
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.24.x'
cache: false
- name: Install dependencies
run: go mod download
- name: Build
shell: bash
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
CGO_ENABLED: '0'
run: |
VERSION=${{ needs.check.outputs.next_version }}
COMMIT=$(git rev-parse --short HEAD)
DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS="-s -w -X codes/internal/commands.Version=${VERSION} -X codes/internal/commands.Commit=${COMMIT} -X codes/internal/commands.Date=${DATE}"
if [ "${{ matrix.goos }}" = "windows" ]; then
go build -ldflags "$LDFLAGS" -o codes.exe ./cmd/codes
else
go build -ldflags "$LDFLAGS" -o codes ./cmd/codes
fi
- name: Test
if: matrix.can_test
shell: bash
run: |
if [ "${{ matrix.goos }}" = "windows" ]; then
./codes.exe version
else
./codes version
fi
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: codes-${{ matrix.goos }}-${{ matrix.goarch }}
path: codes${{ matrix.goos == 'windows' && '.exe' || '' }}
retention-days: 7
release:
needs: [check, build]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
- name: Prepare release files
run: |
mkdir -p release
for dir in artifacts/codes-*; do
platform=$(basename $dir | sed 's/codes-//')
if [ -f "$dir/codes.exe" ]; then
mv "$dir/codes.exe" "release/codes-$platform.exe"
elif [ -f "$dir/codes" ]; then
mv "$dir/codes" "release/codes-$platform"
chmod +x "release/codes-$platform"
fi
done
ls -la release/
- name: Create release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ needs.check.outputs.next_version }}
name: ${{ needs.check.outputs.next_version }}
body: ${{ needs.check.outputs.changelog }}
files: release/*
draft: false
prerelease: false
make_latest: true