Skip to content

Commit 7097014

Browse files
committed
feat: optimiz workflows for release
1 parent 70fcf08 commit 7097014

4 files changed

Lines changed: 264 additions & 36 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Dispatch develop-synced after release
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
8+
jobs:
9+
dispatch_develop_synced:
10+
if: contains(github.event.head_commit.message, 'prelease version')
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
15+
steps:
16+
- name: Checkout develop
17+
uses: actions/checkout@v4
18+
with:
19+
ref: develop
20+
21+
- name: Extract version from commit message
22+
id: meta
23+
env:
24+
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
25+
run: |
26+
set -euo pipefail
27+
echo "Commit message: ${COMMIT_MESSAGE}"
28+
VERSION_WITH_V="$(echo "${COMMIT_MESSAGE}" | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' || true)"
29+
if [ -n "${VERSION_WITH_V}" ]; then
30+
VERSION="${VERSION_WITH_V#v}"
31+
else
32+
VERSION="$(echo "${COMMIT_MESSAGE}" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n1 || true)"
33+
fi
34+
if [ -z "${VERSION}" ]; then
35+
echo "Failed to parse version from commit message" >&2
36+
exit 1
37+
fi
38+
echo "Parsed version: ${VERSION}"
39+
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
40+
41+
- name: Send develop-synced repository_dispatch
42+
uses: actions/github-script@v7
43+
env:
44+
VERSION: ${{ steps.meta.outputs.version }}
45+
with:
46+
github-token: ${{ secrets.GITHUB_TOKEN }}
47+
script: |
48+
const version = process.env.VERSION;
49+
if (!version) {
50+
core.setFailed('VERSION env is not set');
51+
return;
52+
}
53+
core.info(`Sending repository_dispatch develop-synced for version ${version}`);
54+
await github.rest.repos.createDispatchEvent({
55+
owner: context.repo.owner,
56+
repo: context.repo.repo,
57+
event_type: 'develop-synced',
58+
client_payload: { version }
59+
});

.github/workflows/post-release.yml

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
name: Post release after develop synced
2+
3+
on:
4+
repository_dispatch:
5+
types: [develop-synced]
6+
7+
jobs:
8+
post_release:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
13+
steps:
14+
- name: Read version from repository_dispatch payload
15+
id: meta
16+
env:
17+
PAYLOAD_VERSION: ${{ github.event.client_payload.version }}
18+
run: |
19+
set -euo pipefail
20+
if [ -z "${PAYLOAD_VERSION}" ]; then
21+
echo "No version in client_payload, skip post-release."
22+
echo "skip=true" >> "$GITHUB_OUTPUT"
23+
exit 0
24+
fi
25+
echo "Using version from payload: ${PAYLOAD_VERSION}"
26+
echo "version=${PAYLOAD_VERSION}" >> "$GITHUB_OUTPUT"
27+
echo "skip=false" >> "$GITHUB_OUTPUT"
28+
29+
- name: Checkout main
30+
if: steps.meta.outputs.skip != 'true'
31+
uses: actions/checkout@v4
32+
with:
33+
ref: main
34+
fetch-depth: 0
35+
persist-credentials: false
36+
37+
- name: Rebind origin to PAT remote
38+
if: steps.meta.outputs.skip != 'true'
39+
env:
40+
GH_PAT: ${{ secrets.CREATE_TAG_RELEASE_TOKEN }}
41+
run: |
42+
set -euo pipefail
43+
if [ -z "${GH_PAT}" ]; then
44+
echo "CREATE_TAG_RELEASE_TOKEN is not configured."
45+
exit 1
46+
fi
47+
git remote set-url origin "https://x-access-token:${GH_PAT}@github.com/${GITHUB_REPOSITORY}.git"
48+
49+
- name: Fetch tags
50+
if: steps.meta.outputs.skip != 'true'
51+
run: |
52+
set -euo pipefail
53+
git fetch --tags --force
54+
55+
- name: Check existing tag and release
56+
id: exist
57+
if: steps.meta.outputs.skip != 'true'
58+
env:
59+
VERSION: ${{ steps.meta.outputs.version }}
60+
GH_TOKEN: ${{ secrets.CREATE_TAG_RELEASE_TOKEN }}
61+
run: |
62+
set -euo pipefail
63+
TAG="v${VERSION}"
64+
if git rev-parse "refs/tags/${TAG}" >/dev/null 2>&1; then
65+
echo "Tag ${TAG} already exists, skip post-release."
66+
echo "skip=true" >> "$GITHUB_OUTPUT"
67+
exit 0
68+
fi
69+
if gh release view "${TAG}" >/dev/null 2>&1; then
70+
echo "Release ${TAG} already exists, skip post-release."
71+
echo "skip=true" >> "$GITHUB_OUTPUT"
72+
exit 0
73+
fi
74+
echo "skip=false" >> "$GITHUB_OUTPUT"
75+
76+
- name: Fetch develop changelog
77+
if: steps.meta.outputs.skip != 'true' && steps.exist.outputs.skip != 'true'
78+
run: |
79+
set -euo pipefail
80+
git fetch origin develop:refs/remotes/origin/develop --depth=1
81+
git show origin/develop:docs/assets/changelog/en/release.md > release-develop.md
82+
83+
- name: Extract release body from develop changelog
84+
id: body
85+
if: steps.meta.outputs.skip != 'true' && steps.exist.outputs.skip != 'true'
86+
env:
87+
VERSION: ${{ steps.meta.outputs.version }}
88+
run: |
89+
set -euo pipefail
90+
if [ ! -f "release-develop.md" ]; then
91+
echo "develop changelog file not found, skip post-release."
92+
echo "has_body=false" >> "$GITHUB_OUTPUT"
93+
exit 0
94+
fi
95+
if node <<'NODE'
96+
const fs = require('fs');
97+
const version = process.env.VERSION;
98+
const content = fs.readFileSync('release-develop.md', 'utf8');
99+
function escapeRegExp(str) {
100+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
101+
}
102+
const headerPattern = new RegExp('^#\\s*v?' + escapeRegExp(version) + '\\b', 'm');
103+
const match = headerPattern.exec(content);
104+
if (!match) {
105+
console.log('No changelog block for version', version, 'found in develop.');
106+
process.exit(1);
107+
}
108+
const startIndex = match.index;
109+
const rest = content.slice(startIndex);
110+
const nextHeaderPattern = /^#\s*v?\d+\.\d+\.\d+[^\n]*$/gm;
111+
let nextIndex = rest.length;
112+
let m;
113+
if ((m = nextHeaderPattern.exec(rest)) !== null && m.index !== 0) {
114+
nextIndex = m.index;
115+
}
116+
const block = rest.slice(0, nextIndex).trimEnd() + '\n';
117+
fs.writeFileSync('release-body.md', block, 'utf8');
118+
NODE
119+
then
120+
echo "has_body=true" >> "$GITHUB_OUTPUT"
121+
else
122+
echo "has_body=false" >> "$GITHUB_OUTPUT"
123+
fi
124+
125+
- name: Verify GitHub identity
126+
if: steps.meta.outputs.skip != 'true' && steps.exist.outputs.skip != 'true' && steps.body.outputs.has_body == 'true'
127+
env:
128+
GH_TOKEN: ${{ secrets.CREATE_TAG_RELEASE_TOKEN }}
129+
run: |
130+
set -euo pipefail
131+
gh api user -q '.login'
132+
133+
- name: Create tag
134+
if: steps.meta.outputs.skip != 'true' && steps.exist.outputs.skip != 'true' && steps.body.outputs.has_body == 'true'
135+
env:
136+
VERSION: ${{ steps.meta.outputs.version }}
137+
run: |
138+
set -euo pipefail
139+
TAG="v${VERSION}"
140+
git fetch origin main:refs/remotes/origin/main --depth=1
141+
MAIN_SHA="$(git rev-parse origin/main)"
142+
echo "Creating tag ${TAG} at ${MAIN_SHA}"
143+
git tag "${TAG}" "${MAIN_SHA}"
144+
git push origin "${TAG}"
145+
146+
- name: Create GitHub Release
147+
if: steps.meta.outputs.skip != 'true' && steps.exist.outputs.skip != 'true' && steps.body.outputs.has_body == 'true'
148+
env:
149+
VERSION: ${{ steps.meta.outputs.version }}
150+
GH_TOKEN: ${{ secrets.CREATE_TAG_RELEASE_TOKEN }}
151+
run: |
152+
set -euo pipefail
153+
TAG="v${VERSION}"
154+
echo "Creating GitHub Release ${TAG}"
155+
gh release create "${TAG}" \
156+
--target "main" \
157+
--title "${TAG}" \
158+
--notes-file "release-body.md"

.github/workflows/release.yml

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ on:
77

88
jobs:
99
build:
10-
runs-on: macOS-latest # 如果用了electron,记得改成 macOS-latest
10+
runs-on: macOS-latest
1111
permissions:
1212
contents: write
1313
pull-requests: write
1414

1515
strategy:
1616
matrix:
1717
node-version: [18.x]
18-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
1918

2019
steps:
21-
- uses: actions/checkout@v3
22-
- run: |
20+
- name: Checkout
21+
uses: actions/checkout@v3
22+
23+
- name: Configure git user
24+
run: |
2325
git config user.name ${{ github.actor }}
2426
git config user.email ${{ github.actor }}@users.noreply.github.com
2527
@@ -30,7 +32,6 @@ jobs:
3032
cache: 'npm'
3133
cache-dependency-path: './common/config/rush/pnpm-lock.yaml'
3234

33-
# Install rush
3435
- name: Install rush
3536
run: node common/scripts/install-run-rush.js install --bypass-policy
3637

@@ -40,9 +41,9 @@ jobs:
4041
with:
4142
path: packages/vtable
4243
semver_string: ${{ github.ref_name }}
43-
semver_pattern: '^release/(.*)$' # ^v?(.*)$ by default
44+
semver_pattern: '^release/(.*)$'
4445

45-
- name: update nextBump of version policies
46+
- name: Update nextBump of version policies
4647
uses: xile611/set-next-bump-of-rush@main
4748
with:
4849
release_version: ${{ steps.semver_parser.outputs.full }}
@@ -53,33 +54,24 @@ jobs:
5354

5455
- name: Build packages
5556
env:
56-
NODE_OPTIONS: "--max_old_space_size=4096"
57-
NO_EMIT_ON_ERROR: "true"
57+
NODE_OPTIONS: '--max_old_space_size=4096'
58+
NO_EMIT_ON_ERROR: 'true'
5859
run: |
59-
# 设置环境变量确保错误信息完整输出
6060
export NODE_OPTIONS="--max_old_space_size=4096"
6161
export NO_EMIT_ON_ERROR="true"
62-
# 运行构建,并将输出保存到文件
6362
node common/scripts/install-run-rush.js build --only tag:package 2>&1 | tee build.log || {
6463
echo "=== Build failed, showing last 1000 lines of build.log ==="
6564
tail -n 1000 build.log
6665
echo "=== Full error details ==="
67-
# 尝试从日志中提取错误信息
6866
grep -A 50 "TypeScript Compilation Errors" build.log || true
6967
grep -A 50 "Build Error" build.log || true
7068
exit 1
7169
}
7270
73-
# - name: Run Bugserver
74-
# working-directory: ./packages/vtable
75-
# env:
76-
# BUG_SERVER_TOKEN: ${{ secrets.BUG_SERVER_TOKEN }}
77-
# run: node ../../common/scripts/install-run-rushx.js ci
78-
7971
- name: Publish to npm
8072
env:
81-
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
82-
NPM_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
73+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
74+
NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
8375
run: node common/scripts/install-run-rush.js publish --publish --include-all
8476

8577
- name: Update shrinkwrap
@@ -91,7 +83,7 @@ jobs:
9183
with:
9284
path: packages/vtable
9385

94-
- name: Commit & Push changes
86+
- name: Commit and push changes
9587
uses: actions-js/push@master
9688
with:
9789
github_token: ${{ secrets.GITHUB_TOKEN }}
@@ -104,27 +96,14 @@ jobs:
10496
with:
10597
version: ${{ steps.package-version.outputs.current_version }}
10698

107-
- name: Create Release for Tag
108-
id: release_tag
109-
uses: ncipollo/release-action@v1.12.0
110-
env:
111-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
112-
with:
113-
tag: v${{ steps.package-version.outputs.current_version }}
114-
commit: main
115-
prerelease: false
116-
body: |
117-
${{ steps.changelog.outputs.markdown }}
118-
draft: true #
119-
12099
- name: Create Pull Request
121100
uses: dustinirving/create-pr@v1.0.2
122101
with:
123102
token: ${{ secrets.GITHUB_TOKEN }}
124103
title: '[Auto release] release ${{ steps.package-version.outputs.current_version }}'
125104
base: main
126105
head: ${{ github.ref_name }}
127-
labels: release # default labels, the action will throw error if not specified
128-
reviewers: fangsmile,Rui-Sun # default reviewers, the action will throw error if not specified
106+
labels: release
107+
reviewers: fangsmile,Rui-Sun
129108
body: |
130109
${{ steps.changelog.outputs.markdown }}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Sync main to develop after release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
sync_main_to_develop:
10+
if: contains(github.event.head_commit.message, 'prelease version')
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
pull-requests: write
15+
16+
steps:
17+
- name: Checkout main
18+
uses: actions/checkout@v4
19+
with:
20+
ref: main
21+
22+
- name: Create PR from main to develop
23+
env:
24+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25+
run: |
26+
set -euo pipefail
27+
gh pr create \
28+
-B develop \
29+
-H main \
30+
-t "Sync main -> develop" \
31+
-b "Sync after release ${GITHUB_SHA}" \
32+
|| echo 'PR may already exist'

0 commit comments

Comments
 (0)