Skip to content

Commit 4633d64

Browse files
committed
feat(github): add workflows for beta release automation and versioning script
1 parent 8245b33 commit 4633d64

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

.github/version-script-beta.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// .github/version-script-beta.js
2+
import { execSync } from 'node:child_process';
3+
import fs from 'node:fs';
4+
5+
const pkgName = process.argv[2];
6+
if (!pkgName) {
7+
console.error('❌ Missing package argument. Usage: node .github/version-script-beta.js <package-name>');
8+
process.exit(1);
9+
}
10+
11+
const pkgPath = `packages/${pkgName}/package.json`;
12+
13+
try {
14+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
15+
const shortHash = execSync('git rev-parse --short HEAD').toString().trim();
16+
pkg.version = `0.0.0-beta.${shortHash}`;
17+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
18+
console.log(`✅ Updated ${pkgName} version → ${pkg.version}`);
19+
} catch (err) {
20+
console.error('Error modifying version:', err);
21+
process.exit(1);
22+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# .github/workflows/comment-beta-release.yml
2+
name: Write Beta Release Comment
3+
4+
on:
5+
workflow_run:
6+
workflows: ["Release - Beta"]
7+
types:
8+
- completed
9+
10+
jobs:
11+
comment:
12+
if: |
13+
github.repository_owner == 'Ohh-889' &&
14+
github.event.workflow_run.conclusion == 'success'
15+
runs-on: ubuntu-latest
16+
name: Comment on PR
17+
18+
steps:
19+
- name: "Get workflow artifacts"
20+
uses: actions/github-script@v6
21+
id: extract
22+
with:
23+
github-token: ${{ secrets.GITHUB_TOKEN }}
24+
script: |
25+
const all = await github.rest.actions.listWorkflowRunArtifacts({
26+
owner: context.repo.owner,
27+
repo: context.repo.repo,
28+
run_id: context.payload.workflow_run.id,
29+
});
30+
31+
for (const artifact of all.data.artifacts) {
32+
const match = /^npm-package-(.*?)@(.*?)-pr-(\d+)/.exec(artifact.name);
33+
if (match) {
34+
const [, pkg, version, pr] = match;
35+
require("fs").appendFileSync(
36+
process.env.GITHUB_ENV,
37+
`\nPKG_NAME=${pkg}\nPKG_VERSION=${version}\nPR_NUMBER=${pr}`
38+
);
39+
break;
40+
}
41+
}
42+
43+
- name: "Comment on PR"
44+
uses: marocchino/sticky-pull-request-comment@v2
45+
with:
46+
number: ${{ env.PR_NUMBER }}
47+
message: |
48+
🚀 A new **beta release** is available for testing!
49+
50+
```bash
51+
pnpm dlx ${{ env.PKG_NAME }}@${{ env.PKG_VERSION }}
52+
```
53+
54+
> This beta was auto-published from commit:
55+
> https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}
56+
57+
- name: "Remove autorelease label"
58+
uses: actions/github-script@v6
59+
with:
60+
github-token: ${{ secrets.GITHUB_TOKEN }}
61+
script: |
62+
await github.rest.issues.removeLabel({
63+
owner: context.repo.owner,
64+
repo: context.repo.repo,
65+
issue_number: process.env.PR_NUMBER,
66+
name: "🚀 autorelease",
67+
});

.github/workflows/release-beta.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# .github/workflows/release-beta.yml
2+
name: Release - Beta
3+
4+
on:
5+
pull_request:
6+
types: [labeled]
7+
branches:
8+
- main
9+
10+
jobs:
11+
prerelease:
12+
if: |
13+
github.repository_owner == 'Ohh-889' &&
14+
contains(github.event.pull_request.labels.*.name, '🚀 autorelease')
15+
name: Build & Publish beta to NPM
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout Repo
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Setup Node & PNPM
25+
uses: pnpm/action-setup@v4
26+
with:
27+
version: 9.0.6
28+
29+
- name: Setup Node.js 20
30+
uses: actions/setup-node@v4
31+
with:
32+
node-version: 20
33+
cache: "pnpm"
34+
35+
- name: Install deps
36+
run: pnpm install --frozen-lockfile
37+
38+
# 👇 自动检测当前 PR 修改的子包路径
39+
- name: Detect changed package
40+
id: detect
41+
run: |
42+
echo "Detecting changed package..."
43+
CHANGED=$(git diff --name-only origin/main | grep "packages/" | cut -d/ -f2 | sort | uniq | head -n 1)
44+
echo "package=$CHANGED" >> $GITHUB_OUTPUT
45+
echo "Detected package: $CHANGED"
46+
47+
# 👇 生成 beta 版本号
48+
- name: Modify package.json version
49+
run: node .github/version-script-beta.js ${{ steps.detect.outputs.package }}
50+
51+
# 👇 登录 npm
52+
- name: Authenticate to npm
53+
run: echo "//registry.npmjs.org/:_authToken=$NPM_ACCESS_TOKEN" >> packages/${{ steps.detect.outputs.package }}/.npmrc
54+
env:
55+
NPM_ACCESS_TOKEN: ${{ secrets.NPM_TOKEN }}
56+
57+
# 👇 发布 beta 包
58+
- name: Publish Beta to npm
59+
run: pnpm --filter "./packages/${{ steps.detect.outputs.package }}" publish --tag beta --access public
60+
61+
# 👇 获取新版本号
62+
- name: Get new package version
63+
id: pkg-version
64+
run: |
65+
PKG_JSON="packages/${{ steps.detect.outputs.package }}/package.json"
66+
VERSION=$(jq -r .version "$PKG_JSON")
67+
echo "version=$VERSION" >> $GITHUB_OUTPUT
68+
69+
# 👇 上传 artifact,用于后续 comment workflow
70+
- name: Upload packaged artifact
71+
uses: actions/upload-artifact@v4
72+
with:
73+
name: npm-package-${{ steps.detect.outputs.package }}@${{ steps.pkg-version.outputs.version }}-pr-${{ github.event.number }}
74+
path: packages/${{ steps.detect.outputs.package }}/dist

0 commit comments

Comments
 (0)