-
Notifications
You must be signed in to change notification settings - Fork 0
110 lines (97 loc) · 3.74 KB
/
cd-release-pr.yml
File metadata and controls
110 lines (97 loc) · 3.74 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
name: cd-release-pr
on:
push:
branches:
- main
permissions:
contents: write
pull-requests: write
concurrency:
group: cd-release-pr-${{ github.ref }}
cancel-in-progress: true
jobs:
create-release-pr:
runs-on: blacksmith-2vcpu-ubuntu-2404
if: |
!contains(github.event.head_commit.message, '[skip-release]') &&
!contains(github.event.head_commit.message, 'chore: bump version')
steps:
- name: Checkout
uses: actions/checkout@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Configure git user
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
- name: Skip if bump PR already exists
id: existing_bump_pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
COUNT=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--state open \
--json number,headRefName \
--jq '[.[] | select(.headRefName | startswith("chore/bump-version-"))] | length')
if [ "$COUNT" -gt 0 ]; then
echo "skip=true" >> "$GITHUB_OUTPUT"
echo "Existing bump PR found; skipping."
else
echo "skip=false" >> "$GITHUB_OUTPUT"
fi
- name: Bump patch version
id: bump
if: steps.existing_bump_pr.outputs.skip != 'true'
run: |
node <<'NODE'
const fs = require("node:fs");
const packageJsonPath = "package.json";
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
const version = String(packageJson.version ?? "");
const match = version.match(/^(\d+)\.(\d+)\.(\d+)$/);
if (!match) {
throw new Error(`package.json version must be x.y.z, received "${version}"`);
}
const major = Number.parseInt(match[1], 10);
const minor = Number.parseInt(match[2], 10);
const patch = Number.parseInt(match[3], 10);
const nextVersion = `${major}.${minor}.${patch + 1}`;
packageJson.version = nextVersion;
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `new_version=${nextVersion}\n`);
NODE
- name: Create bump branch and commit
id: bump_branch
if: steps.existing_bump_pr.outputs.skip != 'true'
run: |
BRANCH="chore/bump-version-${{ github.run_id }}-${{ github.run_attempt }}"
git checkout -b "$BRANCH"
git add package.json
git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }} [skip-release]"
git push --set-upstream origin "$BRANCH"
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
- name: Create release PR
id: bump_pr
if: steps.existing_bump_pr.outputs.skip != 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_URL=$(gh pr create \
--repo "$GITHUB_REPOSITORY" \
--base main \
--head "${{ steps.bump_branch.outputs.branch }}" \
--title "chore: bump version to ${{ steps.bump.outputs.new_version }}" \
--body "Automated patch version bump for next release.")
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
- name: Enable auto-merge on release PR
if: steps.existing_bump_pr.outputs.skip != 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr merge "${{ steps.bump_pr.outputs.pr_url }}" \
--repo "$GITHUB_REPOSITORY" \
--squash \
--auto \
--delete-branch