-
Notifications
You must be signed in to change notification settings - Fork 61
86 lines (76 loc) · 3.53 KB
/
Copy pathrelease.yml
File metadata and controls
86 lines (76 loc) · 3.53 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
name: Release
on:
push:
branches: [main]
concurrency: ${{ github.workflow }}-${{ github.ref }}
jobs:
release:
name: Release
runs-on: ubuntu-latest
# The repo's default workflow token is read-only, so without this the job 403s
# when pushing the tag / creating the release (releases had to be made by hand).
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check for version change
id: version_check
run: |
# Get current version from package.json
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
# Get previous version from the parent commit
PREVIOUS_VERSION=$(git show HEAD^:package.json 2>/dev/null | node -p "JSON.parse(require('fs').readFileSync('/dev/stdin', 'utf8')).version" 2>/dev/null || echo "")
echo "Current version: $CURRENT_VERSION"
echo "Previous version: $PREVIOUS_VERSION"
# Check if tag already exists
if git rev-parse "v$CURRENT_VERSION" >/dev/null 2>&1; then
echo "Tag v$CURRENT_VERSION already exists"
echo "should_release=false" >> $GITHUB_OUTPUT
elif [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ] && [ -n "$PREVIOUS_VERSION" ]; then
echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION, will create release"
echo "should_release=true" >> $GITHUB_OUTPUT
else
echo "No version change detected"
echo "should_release=false" >> $GITHUB_OUTPUT
fi
# Create and push tag
- name: Create and push tag
if: steps.version_check.outputs.should_release == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag "v${{ steps.version_check.outputs.current_version }}"
git push origin "v${{ steps.version_check.outputs.current_version }}"
# Extract release notes from CHANGELOG.md for this version
- name: Extract release notes
if: steps.version_check.outputs.should_release == 'true'
id: release_notes
run: |
VERSION="${{ steps.version_check.outputs.current_version }}"
# Print the body between this version's header and the next version header,
# excluding both headers. Handles both "## 1.2.3" and "## [1.2.3]" styles.
# (The old awk range failed because its end pattern also matched the start
# header, collapsing the range to a single line — release notes were empty.)
NOTES=$(awk -v ver="$VERSION" '
$0 ~ "^## \\[?" ver "([^0-9]|$)" { collecting = 1; next }
collecting && /^## \[?[0-9]/ { exit }
collecting { print }
' CHANGELOG.md)
# Use a delimiter for multiline output
echo "notes<<EOF" >> $GITHUB_OUTPUT
echo "$NOTES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
# Create GitHub Release
- name: Create GitHub Release
if: steps.version_check.outputs.should_release == 'true'
uses: softprops/action-gh-release@v1
with:
tag_name: v${{ steps.version_check.outputs.current_version }}
name: v${{ steps.version_check.outputs.current_version }}
body: ${{ steps.release_notes.outputs.notes }}
generate_release_notes: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}