Skip to content

Commit 0dd726b

Browse files
committed
create a new automated release workflow for version upgrade
1 parent aa77842 commit 0dd726b

1 file changed

Lines changed: 155 additions & 0 deletions

File tree

.github/workflows/release.yml

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
name: Release
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version_type:
7+
description: 'Version bump type'
8+
required: true
9+
type: choice
10+
options:
11+
- patch
12+
- minor
13+
- major
14+
- custom
15+
custom_version:
16+
description: 'Custom version (only used if version_type is custom, e.g., 1.2.3)'
17+
required: false
18+
type: string
19+
20+
jobs:
21+
release:
22+
runs-on: ubuntu-latest
23+
permissions:
24+
contents: write
25+
pull-requests: write
26+
steps:
27+
- name: Checkout repo
28+
uses: actions/checkout@v4
29+
with:
30+
fetch-depth: 0
31+
32+
- name: Configure Git
33+
run: |
34+
git config user.name "github-actions[bot]"
35+
git config user.email "github-actions[bot]@users.noreply.github.com"
36+
37+
- name: Use Node
38+
uses: actions/setup-node@v4
39+
with:
40+
node-version: lts/*
41+
42+
- name: Install dependencies
43+
run: yarn install --frozen-lockfile
44+
45+
- name: Run tests
46+
run: yarn test run
47+
48+
- name: Run build
49+
run: yarn build
50+
51+
- name: Get current version
52+
id: current_version
53+
run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT
54+
55+
- name: Bump version
56+
id: bump_version
57+
run: |
58+
if [ "${{ github.event.inputs.version_type }}" = "custom" ]; then
59+
NEW_VERSION="${{ github.event.inputs.custom_version }}"
60+
npm version $NEW_VERSION --no-git-tag-version
61+
else
62+
npm version ${{ github.event.inputs.version_type }} --no-git-tag-version
63+
fi
64+
NEW_VERSION=$(node -p "require('./package.json').version")
65+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
66+
67+
- name: Generate changelog
68+
id: changelog
69+
run: |
70+
NEW_VERSION="${{ steps.bump_version.outputs.new_version }}"
71+
72+
if git describe --tags --abbrev=0 2>/dev/null; then
73+
LAST_TAG=$(git describe --tags --abbrev=0)
74+
git log $LAST_TAG..HEAD --pretty=format:"- %s" --no-merges > /tmp/commits.txt
75+
else
76+
git log --pretty=format:"- %s" --no-merges > /tmp/commits.txt
77+
fi
78+
79+
DATE=$(date +%Y-%m-%d)
80+
81+
cat > /tmp/changelog_entry.md << 'CHANGELOG_EOF'
82+
## [$NEW_VERSION] - $DATE
83+
84+
### Changes
85+
CHANGELOG_EOF
86+
87+
sed "s/\$NEW_VERSION/$NEW_VERSION/g; s/\$DATE/$DATE/g" /tmp/changelog_entry.md > /tmp/changelog_header.md
88+
cat /tmp/commits.txt >> /tmp/changelog_header.md
89+
echo "" >> /tmp/changelog_header.md
90+
91+
if [ -f CHANGELOG.md ]; then
92+
cat /tmp/changelog_header.md CHANGELOG.md > /tmp/new_changelog.md
93+
mv /tmp/new_changelog.md CHANGELOG.md
94+
else
95+
echo "# Changelog" > CHANGELOG.md
96+
echo "" >> CHANGELOG.md
97+
echo "All notable changes to this project will be documented in this file." >> CHANGELOG.md
98+
echo "" >> CHANGELOG.md
99+
cat /tmp/changelog_header.md >> CHANGELOG.md
100+
fi
101+
102+
echo "changelog<<CHANGELOG_OUTPUT_EOF" >> $GITHUB_OUTPUT
103+
cat /tmp/changelog_header.md >> $GITHUB_OUTPUT
104+
echo "CHANGELOG_OUTPUT_EOF" >> $GITHUB_OUTPUT
105+
106+
- name: Create release branch and commit
107+
id: create_branch
108+
run: |
109+
BRANCH_NAME="release/v${{ steps.bump_version.outputs.new_version }}"
110+
git checkout -b "$BRANCH_NAME"
111+
git add package.json CHANGELOG.md
112+
git commit -m "chore: release v${{ steps.bump_version.outputs.new_version }}"
113+
git push origin "$BRANCH_NAME"
114+
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
115+
116+
- name: Create Pull Request
117+
uses: actions/github-script@v7
118+
with:
119+
script: |
120+
const changelog = `${{ steps.changelog.outputs.changelog }}`;
121+
const version = '${{ steps.bump_version.outputs.new_version }}';
122+
const branchName = '${{ steps.create_branch.outputs.branch_name }}';
123+
124+
const prBody = `## Release v${version}
125+
126+
This PR was automatically created by the release workflow.
127+
128+
### Changelog
129+
${changelog}
130+
131+
### Pre-merge Checklist
132+
- [ ] Review version bump is correct
133+
- [ ] Review changelog entries
134+
- [ ] All tests passing
135+
- [ ] Build successful
136+
137+
### Post-merge Steps
138+
After merging this PR:
139+
1. Create a GitHub release with tag \`v${version}\`
140+
2. Manually publish to npm: \`npm publish\`
141+
`;
142+
143+
const { data: pr } = await github.rest.pulls.create({
144+
owner: context.repo.owner,
145+
repo: context.repo.repo,
146+
title: `chore: release v${version}`,
147+
head: branchName,
148+
base: 'main',
149+
body: prBody
150+
});
151+
152+
console.log('Pull request created:', pr.html_url);
153+
core.summary.addHeading('Release PR Created');
154+
core.summary.addLink('View Pull Request', pr.html_url);
155+
await core.summary.write();

0 commit comments

Comments
 (0)