-
Notifications
You must be signed in to change notification settings - Fork 0
271 lines (235 loc) · 9.84 KB
/
release.yml
File metadata and controls
271 lines (235 loc) · 9.84 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
name: Release
on:
pull_request:
types: [closed]
branches:
- main
workflow_dispatch:
inputs:
version_type:
description: 'Version bump type (major, minor, patch)'
required: true
default: 'minor'
type: choice
options:
- major
- minor
- patch
generate_changelog:
description: 'Generate comprehensive changelog'
required: false
default: true
type: boolean
permissions:
contents: write
pull-requests: read
jobs:
release:
name: Create Release
runs-on: ubuntu-latest
# Only run on:
# 1. Manual workflow_dispatch
# 2. Merged PRs with 'release:major' or 'release:minor' labels
if: |
github.event_name == 'workflow_dispatch' ||
(github.event.pull_request.merged == true &&
(contains(github.event.pull_request.labels.*.name, 'release:major') ||
contains(github.event.pull_request.labels.*.name, 'release:minor')))
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for changelog generation
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Read current version
id: current_version
run: |
if [ -f "package.json" ]; then
VERSION=$(node -p "require('./package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Current version: $VERSION"
else
# Default to 0.1.0 if no package.json exists
echo "version=0.1.0" >> $GITHUB_OUTPUT
echo "No package.json found, using default version: 0.1.0"
fi
- name: Determine version bump type
id: bump_type
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
# Manual dispatch - use input
echo "type=${{ github.event.inputs.version_type }}" >> $GITHUB_OUTPUT
echo "Releasing with version bump: ${{ github.event.inputs.version_type }}"
else
# PR merge - detect from labels
PR_LABELS="${{ join(github.event.pull_request.labels.*.name, ',') }}"
if echo "$PR_LABELS" | grep -q "release:major"; then
echo "type=major" >> $GITHUB_OUTPUT
echo "Detected major release from PR labels"
elif echo "$PR_LABELS" | grep -q "release:minor"; then
echo "type=minor" >> $GITHUB_OUTPUT
echo "Detected minor release from PR labels"
else
echo "Error: No valid release label found (release:major or release:minor)"
exit 1
fi
fi
- name: Bump version
id: new_version
run: |
CURRENT="${{ steps.current_version.outputs.version }}"
BUMP_TYPE="${{ steps.bump_type.outputs.type }}"
# Parse semver
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT"
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="$MAJOR.$MINOR.$PATCH"
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "tag=v$NEW_VERSION" >> $GITHUB_OUTPUT
echo "New version: $NEW_VERSION"
- name: Update package.json version
if: hashFiles('package.json') != ''
run: |
NEW_VERSION="${{ steps.new_version.outputs.version }}"
node -e "const fs=require('fs'); const pkg=JSON.parse(fs.readFileSync('package.json')); pkg.version='$NEW_VERSION'; fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');"
echo "Updated package.json to version $NEW_VERSION"
- name: Generate changelog content
id: changelog_content
run: |
TAG="${{ steps.new_version.outputs.tag }}"
PREV_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
BUMP_TYPE="${{ steps.bump_type.outputs.type }}"
# Comprehensive changelog for major/minor releases
# For workflow_dispatch, respect the input; for PR merge, default to true
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
GENERATE_COMPREHENSIVE="${{ github.event.inputs.generate_changelog }}"
else
GENERATE_COMPREHENSIVE="true"
fi
{
echo "changelog<<EOF"
echo "## Release ${{ steps.new_version.outputs.version }}"
echo ""
if [ -n "$PREV_TAG" ]; then
echo "### 📦 Changes since $PREV_TAG"
else
echo "### 📦 Initial Release"
fi
echo ""
# Categorize commits for minor/major releases
if [ "$BUMP_TYPE" = "minor" ] || [ "$BUMP_TYPE" = "major" ] || [ "$GENERATE_COMPREHENSIVE" = "true" ]; then
echo "#### ✨ Features & Enhancements"
git log ${PREV_TAG:+$PREV_TAG..}HEAD --pretty=format:"- %s (%h)" --no-merges --grep="feat\|feature\|add" -i || echo "- No new features"
echo ""
echo ""
echo "#### 🐛 Bug Fixes"
git log ${PREV_TAG:+$PREV_TAG..}HEAD --pretty=format:"- %s (%h)" --no-merges --grep="fix\|bug" -i || echo "- No bug fixes"
echo ""
echo ""
echo "#### 🔒 Security"
git log ${PREV_TAG:+$PREV_TAG..}HEAD --pretty=format:"- %s (%h)" --no-merges --grep="security\|vuln\|cve" -i || echo "- No security updates"
echo ""
echo ""
echo "#### 📚 Documentation"
git log ${PREV_TAG:+$PREV_TAG..}HEAD --pretty=format:"- %s (%h)" --no-merges --grep="doc\|readme" -i || echo "- No documentation changes"
echo ""
echo ""
echo "#### 🔧 Chores & Maintenance"
git log ${PREV_TAG:+$PREV_TAG..}HEAD --pretty=format:"- %s (%h)" --no-merges --grep="chore\|refactor\|style" -i || echo "- No maintenance updates"
echo ""
echo ""
else
# Simple changelog for patch releases
echo "#### Changes:"
git log ${PREV_TAG:+$PREV_TAG..}HEAD --pretty=format:"- %s (%h)" --no-merges || echo "- Initial release"
echo ""
echo ""
fi
# Add contributors
echo "#### 👥 Contributors"
git log ${PREV_TAG:+$PREV_TAG..}HEAD --pretty=format:"- @%an" --no-merges | sort -u
echo ""
echo ""
if [ -n "$PREV_TAG" ]; then
echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/$PREV_TAG...$TAG"
else
echo "**Full Changelog**: https://github.com/${{ github.repository }}/commits/$TAG"
fi
echo "EOF"
} >> $GITHUB_OUTPUT
- name: Update CHANGELOG.md
run: |
TAG="${{ steps.new_version.outputs.tag }}"
CHANGELOG_ENTRY="${{ steps.changelog_content.outputs.changelog }}"
# Create CHANGELOG.md if it doesn't exist
if [ ! -f "CHANGELOG.md" ]; then
echo "# Changelog" > CHANGELOG.md
echo "" >> CHANGELOG.md
echo "All notable changes to OSA (Open Source Automation) will be documented in this file." >> CHANGELOG.md
echo "" >> CHANGELOG.md
echo "The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)," >> CHANGELOG.md
echo "and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)." >> CHANGELOG.md
echo "" >> CHANGELOG.md
echo "---" >> CHANGELOG.md
echo "" >> CHANGELOG.md
fi
# Prepend new release to CHANGELOG.md (after header)
{
head -n 9 CHANGELOG.md # Keep header
echo ""
echo "$CHANGELOG_ENTRY"
echo ""
echo "---"
echo ""
tail -n +10 CHANGELOG.md # Rest of the file
} > CHANGELOG.md.tmp
mv CHANGELOG.md.tmp CHANGELOG.md
git add CHANGELOG.md
echo "Updated CHANGELOG.md with release notes"
- name: Commit version bump
run: |
TAG="${{ steps.new_version.outputs.tag }}"
if [ -f "package.json" ]; then
git add package.json
fi
# Add CHANGELOG.md if it was updated
if [ -f "CHANGELOG.md" ]; then
git add CHANGELOG.md
fi
git commit -m "chore: bump version to $TAG" --allow-empty || echo "No changes to commit"
- name: Create and push tag
run: |
TAG="${{ steps.new_version.outputs.tag }}"
git tag -a "$TAG" -m "Release $TAG"
git push origin "$TAG"
git push origin main
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.new_version.outputs.tag }}
name: Release ${{ steps.new_version.outputs.version }}
body: ${{ steps.changelog_content.outputs.changelog }}
draft: false
prerelease: false
generate_release_notes: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
outputs:
version: ${{ steps.new_version.outputs.version }}
tag: ${{ steps.new_version.outputs.tag }}