-
-
Notifications
You must be signed in to change notification settings - Fork 21
191 lines (167 loc) · 7.04 KB
/
release.yml
File metadata and controls
191 lines (167 loc) · 7.04 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
name: Release
on:
push:
branches:
- master
workflow_dispatch:
inputs:
version_bump:
description: 'Version bump type'
required: true
default: 'patch'
type: choice
options:
- patch
- minor
- major
release_notes:
description: 'Additional release notes (optional)'
required: false
type: string
permissions:
contents: write
jobs:
release:
name: Create Release
runs-on: ubuntu-latest
outputs:
new_version: ${{ steps.version.outputs.new_version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get current version
id: current
run: |
CURRENT=$(node -p "require('./src/package.json').version")
echo "version=$CURRENT" >> $GITHUB_OUTPUT
echo "Current version: $CURRENT"
- name: Determine version bump from commits
id: bump
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "type=${{ inputs.version_bump }}" >> $GITHUB_OUTPUT
else
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then RANGE="HEAD"; else RANGE="${LAST_TAG}..HEAD"; fi
COMMITS=$(git log $RANGE --pretty=format:"%s" 2>/dev/null || echo "")
if echo "$COMMITS" | grep -qiE "^(feat|feature)(\(.+\))?!:|BREAKING CHANGE"; then
echo "type=major" >> $GITHUB_OUTPUT
elif echo "$COMMITS" | grep -qiE "^(feat|feature)(\(.+\))?:"; then
echo "type=minor" >> $GITHUB_OUTPUT
else
echo "type=patch" >> $GITHUB_OUTPUT
fi
fi
- name: Calculate new version
id: version
run: |
IFS='.' read -r MAJOR MINOR PATCH <<< "${{ steps.current.outputs.version }}"
case "${{ steps.bump.outputs.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 "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "New version: $NEW_VERSION"
- name: Generate changelog entry
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
TODAY=$(date +%Y-%m-%d)
NEW_VERSION="${{ steps.version.outputs.new_version }}"
ADDED="" CHANGED="" FIXED="" REMOVED=""
while IFS= read -r line; do
HASH=$(echo "$line" | cut -d'|' -f1)
MSG=$(echo "$line" | cut -d'|' -f2-)
SHORT_HASH=$(echo "$HASH" | cut -c1-7)
PR_NUM=$(echo "$MSG" | grep -oP '\(#\K[0-9]+(?=\))' | head -1)
if [ -n "$PR_NUM" ]; then
ATTR="([#${PR_NUM}](https://github.com/proyecto26/nativescript-inappbrowser/pull/${PR_NUM}))"
else
ATTR="([${SHORT_HASH}](https://github.com/proyecto26/nativescript-inappbrowser/commit/${HASH}))"
fi
CLEAN_MSG=$(echo "$MSG" | sed -E 's/^(feat|fix|perf|refactor|chore|docs|style|test|build|ci)(\([^)]*\))?(!)?:\s*//')
CLEAN_MSG=$(echo "$CLEAN_MSG" | sed -E 's/\s*\(#[0-9]+\)\s*$//')
if echo "$MSG" | grep -qiE "^(feat|feature)(\(.+\))?:"; then
ADDED="${ADDED}- ${CLEAN_MSG} ${ATTR}.\n"
elif echo "$MSG" | grep -qiE "^fix(\(.+\))?:"; then
FIXED="${FIXED}- ${CLEAN_MSG} ${ATTR}.\n"
elif echo "$MSG" | grep -qiE "^(perf|refactor|chore|docs|style|build|ci)(\(.+\))?:"; then
CHANGED="${CHANGED}- ${CLEAN_MSG} ${ATTR}.\n"
elif echo "$MSG" | grep -qiE "^(revert)(\(.+\))?:"; then
REMOVED="${REMOVED}- ${CLEAN_MSG} ${ATTR}.\n"
else
CHANGED="${CHANGED}- ${CLEAN_MSG} ${ATTR}.\n"
fi
done < <(git log $RANGE --pretty=format:"%H|%s" --no-merges 2>/dev/null)
if [ -n "${{ inputs.release_notes }}" ]; then
ADDED="${ADDED}- ${{ inputs.release_notes }}\n"
fi
BODY=""
[ -n "$ADDED" ] && BODY="${BODY}### Added\n${ADDED}\n"
[ -n "$CHANGED" ] && BODY="${BODY}### Changed\n${CHANGED}\n"
[ -n "$FIXED" ] && BODY="${BODY}### Fixed\n${FIXED}\n"
[ -n "$REMOVED" ] && BODY="${BODY}### Removed\n${REMOVED}\n"
[ -z "$BODY" ] && BODY="### Changed\n- Release version ${NEW_VERSION}.\n"
echo -e "$BODY" > /tmp/release_body.md
# Update CHANGELOG.md
PREV_VERSION="${{ steps.current.outputs.version }}"
HEADER="## [${NEW_VERSION}] - ${TODAY}"
LINK="[${NEW_VERSION}]: https://github.com/proyecto26/nativescript-inappbrowser/compare/v${PREV_VERSION}...v${NEW_VERSION}"
sed -i "/^## \[Unreleased\]/a\\\\n${HEADER}\\n" CHANGELOG.md
sed -i "/^## \[${NEW_VERSION}\]/r /tmp/release_body.md" CHANGELOG.md
sed -i "s|\[Unreleased\]: .*|[Unreleased]: https://github.com/proyecto26/nativescript-inappbrowser/compare/v${NEW_VERSION}...HEAD|" CHANGELOG.md
sed -i "/^\[Unreleased\]:/a ${LINK}" CHANGELOG.md
- name: Update version in src/package.json
run: |
node -e "
const pkg = require('./src/package.json');
pkg.version = '${{ steps.version.outputs.new_version }}';
require('fs').writeFileSync('./src/package.json', JSON.stringify(pkg, null, '\t') + '\n');
"
- name: Commit version bump
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add -A
git commit -m "chore(release): v${{ steps.version.outputs.new_version }}
- Updated src/package.json
- Updated CHANGELOG.md"
git tag "v${{ steps.version.outputs.new_version }}"
git push origin master --follow-tags
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ steps.version.outputs.new_version }}
name: Release ${{ steps.version.outputs.new_version }}
body_path: /tmp/release_body.md
draft: false
prerelease: false
publish-npm:
name: Publish to npm
needs: release
runs-on: ubuntu-latest
if: needs.release.outputs.new_version != ''
steps:
- uses: actions/checkout@v4
with:
ref: master
- name: Pull latest (includes version bump commit)
run: git pull origin master
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'
- name: Build and publish
run: |
cd src
npm install
npm run build || true
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}