-
Notifications
You must be signed in to change notification settings - Fork 137
94 lines (82 loc) · 3.05 KB
/
Copy pathauto-version-bump.yml
File metadata and controls
94 lines (82 loc) · 3.05 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
name: Auto Version Bump
on:
pull_request:
types:
- opened
- synchronize
- reopened
- closed
branches:
- main
permissions:
contents: write
jobs:
bump-version:
if: github.event.action != 'closed' && github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Determine bump type
id: bump
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: |
BUMP="patch"
if echo "$PR_BODY" | grep -qiE '^\s*-\s*\[[xX]\]\s*Major'; then
BUMP="major"
elif echo "$PR_BODY" | grep -qiE '^\s*-\s*\[[xX]\]\s*Minor'; then
BUMP="minor"
fi
echo "Resolved bump type: $BUMP"
echo "type=$BUMP" >> "$GITHUB_OUTPUT"
- name: Compute version relative to latest main
id: apply
run: |
git fetch origin main
BASE_VERSION=$(git show origin/main:package.json | node -e "process.stdout.write(JSON.parse(require('fs').readFileSync(0,'utf8')).version)")
node -e "
const fs = require('fs');
const p = JSON.parse(fs.readFileSync('package.json', 'utf8'));
p.version = '$BASE_VERSION';
fs.writeFileSync('package.json', JSON.stringify(p, null, 2) + '\n');
"
npm version ${{ steps.bump.outputs.type }} --no-git-tag-version
echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
- name: Commit and push if version changed
env:
VERSION: ${{ steps.apply.outputs.version }}
HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
if git diff --quiet -- package.json package-lock.json; then
echo "package.json already at the correct version, nothing to push"
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add package.json package-lock.json
git commit -m "chore: bump version to $VERSION"
git push origin "HEAD:$HEAD_REF"
tag-release:
if: github.event.action == 'closed' && github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Tag the merged version
run: |
VERSION=$(node -p "require('./package.json').version")
if git rev-parse "v$VERSION" >/dev/null 2>&1; then
echo "Tag v$VERSION already exists, skipping tag creation"
else
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag "v$VERSION"
git push origin "v$VERSION"
fi