-
Notifications
You must be signed in to change notification settings - Fork 0
164 lines (148 loc) · 5.98 KB
/
Copy pathrelease.yml
File metadata and controls
164 lines (148 loc) · 5.98 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
name: Release on PR merge
on:
pull_request:
types: [closed]
branches:
- main
permissions:
contents: write
pull-requests: read
jobs:
release:
name: Create GitHub Release
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Get version from package.json
id: package_version
run: |
VERSION=$(node -p "require('./package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "tag=v$VERSION" >> $GITHUB_OUTPUT
echo "Package version: $VERSION"
- name: Check if tag already exists
id: check_tag
run: |
TAG="v${{ steps.package_version.outputs.version }}"
if git rev-parse --verify "refs/tags/$TAG" >/dev/null 2>&1; then
echo "tag_exists=true" >> $GITHUB_OUTPUT
echo "Tag $TAG already exists, skipping release"
else
echo "tag_exists=false" >> $GITHUB_OUTPUT
echo "Tag $TAG does not exist, proceeding with release"
fi
- name: Extract release notes from CHANGELOG
if: steps.check_tag.outputs.tag_exists == 'false'
id: release_notes
run: |
VERSION="${{ steps.package_version.outputs.version }}"
echo "Extracting release notes for version $VERSION from CHANGELOG.md"
if [ ! -f CHANGELOG.md ]; then
echo "CHANGELOG.md not found, using PR information"
echo "## Changes" > release_notes.md
echo "Merged PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}" >> release_notes.md
echo "" >> release_notes.md
echo "${{ github.event.pull_request.body }}" >> release_notes.md
else
# Extract the section for this version from CHANGELOG.md
awk -v version="$VERSION" '
BEGIN {
found=0
in_section=0
content_found=0
}
# Match version headers like ## [1.18.0], ## [v1.18.0], ## [1.18.0] - 2025-10-14, or ## [Unreleased]
/^## \[/ {
if (in_section && content_found) {
# We hit the next section, stop here
exit
}
if ($0 ~ "\\[v?"version"\\]" || ($0 ~ "\\[Unreleased\\]" && !found)) {
found=1
in_section=1
next
} else {
in_section=0
}
}
# Skip empty lines at the start of a section
in_section && /^$/ && !content_found {
next
}
# Capture content within the section
in_section && !/^## \[/ {
print
if ($0 !~ /^$/) content_found=1
}
' CHANGELOG.md > release_notes.md
# If no content found in CHANGELOG, try Unreleased section
if [ ! -s release_notes.md ]; then
echo "No specific version found, trying Unreleased section"
awk '
BEGIN { in_unreleased=0; content_found=0 }
/^## \[Unreleased\]/ { in_unreleased=1; next }
/^## \[/ && in_unreleased { exit }
in_unreleased && /^$/ && !content_found { next }
in_unreleased {
print
if ($0 !~ /^$/) content_found=1
}
' CHANGELOG.md > release_notes.md
fi
# Final fallback to PR information
if [ ! -s release_notes.md ]; then
echo "No CHANGELOG content found, using PR information"
echo "## Changes" > release_notes.md
echo "Merged PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}" >> release_notes.md
if [ -n "${{ github.event.pull_request.body }}" ]; then
echo "" >> release_notes.md
echo "${{ github.event.pull_request.body }}" >> release_notes.md
fi
fi
fi
echo "Generated release notes:"
echo "========================"
cat release_notes.md
echo "========================"
# Set output for GitHub Actions
echo "notes<<EOF" >> $GITHUB_OUTPUT
cat release_notes.md >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Get current date
if: steps.check_tag.outputs.tag_exists == 'false'
id: date
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
- name: Create Git tag
if: steps.check_tag.outputs.tag_exists == 'false'
run: |
TAG="${{ steps.package_version.outputs.tag }}"
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git tag -a "$TAG" -m "Release $TAG"
git push origin "$TAG"
echo "Created and pushed tag: $TAG"
- name: Create GitHub Release
if: steps.check_tag.outputs.tag_exists == 'false'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.package_version.outputs.tag }}
release_name: ${{ steps.package_version.outputs.tag }} - ${{ steps.date.outputs.date }}
body: ${{ steps.release_notes.outputs.notes }}
draft: false
prerelease: false
- name: Skip release (tag exists)
if: steps.check_tag.outputs.tag_exists == 'true'
run: |
echo "Tag ${{ steps.package_version.outputs.tag }} already exists, skipping release creation"
echo "If you need to create a new release, update the version in package.json"