-
Notifications
You must be signed in to change notification settings - Fork 9
244 lines (219 loc) · 8.73 KB
/
Copy pathrelease.yml
File metadata and controls
244 lines (219 loc) · 8.73 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
name: Release
on:
push:
branches: [ main ]
workflow_dispatch:
inputs:
release_type:
description: 'Release type (major, minor, patch)'
required: true
default: 'patch'
type: choice
options:
- major
- minor
- patch
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: read
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get latest tag
id: get_latest_tag
run: |
# Get the latest tag, or use v0.0.0 if no tags exist
latest_tag=$(git tag -l --sort=-version:refname | head -n 1)
if [ -z "$latest_tag" ]; then
latest_tag="v0.0.0"
fi
echo "latest_tag=$latest_tag" >> $GITHUB_OUTPUT
echo "Latest tag: $latest_tag"
- name: Determine version bump
id: version_bump
run: |
# If triggered by workflow_dispatch, use the input
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
bump_type="${{ github.event.inputs.release_type }}"
else
# Auto-detect bump type from commit messages since last tag
latest_tag="${{ steps.get_latest_tag.outputs.latest_tag }}"
# Get commits since last tag
if [ "$latest_tag" == "v0.0.0" ]; then
commits=$(git log --pretty=format:"%s" --no-merges)
else
commits=$(git log ${latest_tag}..HEAD --pretty=format:"%s" --no-merges)
fi
# Determine bump type based on conventional commits
if echo "$commits" | grep -E "^(feat|feature)(\(.+\))?!:" > /dev/null; then
bump_type="major"
elif echo "$commits" | grep -E "^BREAKING CHANGE:" > /dev/null; then
bump_type="major"
elif echo "$commits" | grep -E "^(feat|feature)(\(.+\))?:" > /dev/null; then
bump_type="minor"
elif echo "$commits" | grep -E "^(fix|bugfix)(\(.+\))?:" > /dev/null; then
bump_type="patch"
elif echo "$commits" | grep -E "^(docs|style|refactor|test|chore)(\(.+\))?:" > /dev/null; then
bump_type="patch"
else
# Default to patch for any other changes
bump_type="patch"
fi
fi
echo "bump_type=$bump_type" >> $GITHUB_OUTPUT
echo "Version bump type: $bump_type"
- name: Calculate new version
id: new_version
run: |
latest_tag="${{ steps.get_latest_tag.outputs.latest_tag }}"
bump_type="${{ steps.version_bump.outputs.bump_type }}"
# Remove 'v' prefix if present
version=${latest_tag#v}
# Split version into major.minor.patch
IFS='.' read -r major minor patch <<< "$version"
# Increment based on bump type
case $bump_type in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch)
patch=$((patch + 1))
;;
esac
new_version="v${major}.${minor}.${patch}"
echo "new_version=$new_version" >> $GITHUB_OUTPUT
echo "New version: $new_version"
- name: Check if release needed
id: check_release
run: |
latest_tag="${{ steps.get_latest_tag.outputs.latest_tag }}"
new_version="${{ steps.new_version.outputs.new_version }}"
# Skip release if triggered by push and no new commits since last tag
if [ "${{ github.event_name }}" == "push" ] && [ "$latest_tag" != "v0.0.0" ]; then
commits_since_tag=$(git rev-list ${latest_tag}..HEAD --count)
if [ "$commits_since_tag" -eq 0 ]; then
echo "needs_release=false" >> $GITHUB_OUTPUT
echo "No new commits since last tag, skipping release"
exit 0
fi
fi
echo "needs_release=true" >> $GITHUB_OUTPUT
echo "Release needed: $new_version"
- name: Generate release notes
id: release_notes
if: steps.check_release.outputs.needs_release == 'true'
run: |
latest_tag="${{ steps.get_latest_tag.outputs.latest_tag }}"
new_version="${{ steps.new_version.outputs.new_version }}"
# Generate release notes
if [ "$latest_tag" == "v0.0.0" ]; then
# First release
echo "## 🎉 Initial Release" > release_notes.md
echo "" >> release_notes.md
echo "This is the initial release of the project." >> release_notes.md
else
# Get commits since last tag
echo "## 📋 Changes in $new_version" > release_notes.md
echo "" >> release_notes.md
# Get all commits and categorize them
features=""
fixes=""
docs=""
refactor=""
tests=""
chores=""
others=""
# Read commits and categorize
while IFS= read -r commit; do
case $commit in
feat:*|feature:*)
features="$features- ${commit#*: }"$'\n'
;;
fix:*|bugfix:*)
fixes="$fixes- ${commit#*: }"$'\n'
;;
docs:*)
docs="$docs- ${commit#*: }"$'\n'
;;
refactor:*)
refactor="$refactor- ${commit#*: }"$'\n'
;;
test:*)
tests="$tests- ${commit#*: }"$'\n'
;;
chore:*)
chores="$chores- ${commit#*: }"$'\n'
;;
*)
others="$others- $commit"$'\n'
;;
esac
done < <(git log ${latest_tag}..HEAD --pretty=format:"%s" --no-merges)
# Output sections with content
if [ -n "$features" ]; then
echo "### ✨ New Features" >> release_notes.md
echo "$features" >> release_notes.md
fi
if [ -n "$fixes" ]; then
echo "### 🐛 Bug Fixes" >> release_notes.md
echo "$fixes" >> release_notes.md
fi
if [ -n "$docs" ]; then
echo "### 📚 Documentation" >> release_notes.md
echo "$docs" >> release_notes.md
fi
if [ -n "$refactor" ]; then
echo "### ♻️ Code Refactoring" >> release_notes.md
echo "$refactor" >> release_notes.md
fi
if [ -n "$tests" ]; then
echo "### 🧪 Tests" >> release_notes.md
echo "$tests" >> release_notes.md
fi
if [ -n "$chores" ]; then
echo "### 🔧 Maintenance" >> release_notes.md
echo "$chores" >> release_notes.md
fi
if [ -n "$others" ]; then
echo "### 🔄 Other Changes" >> release_notes.md
echo "$others" >> release_notes.md
fi
fi
echo "" >> release_notes.md
echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/${latest_tag}...${new_version}" >> release_notes.md
# Output for next step
echo "release_notes<<EOF" >> $GITHUB_OUTPUT
cat release_notes.md >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Release
if: steps.check_release.outputs.needs_release == 'true'
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.new_version.outputs.new_version }}
name: Release ${{ steps.new_version.outputs.new_version }}
body: ${{ steps.release_notes.outputs.release_notes }}
draft: false
prerelease: false
generate_release_notes: false
- name: Summary
if: steps.check_release.outputs.needs_release == 'true'
run: |
echo "## 🚀 Release Created" >> $GITHUB_STEP_SUMMARY
echo "- **Version**: ${{ steps.new_version.outputs.new_version }}" >> $GITHUB_STEP_SUMMARY
echo "- **Type**: ${{ steps.version_bump.outputs.bump_type }}" >> $GITHUB_STEP_SUMMARY
echo "- **Previous**: ${{ steps.get_latest_tag.outputs.latest_tag }}" >> $GITHUB_STEP_SUMMARY
echo "- **Trigger**: ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "View the release: https://github.com/${{ github.repository }}/releases/tag/${{ steps.new_version.outputs.new_version }}" >> $GITHUB_STEP_SUMMARY