Skip to content

Commit e11bf0f

Browse files
authored
chore: use Trust Publish (#27)
* chore: use Trust Publish * chore: prettier
1 parent a0bb3e4 commit e11bf0f

1 file changed

Lines changed: 41 additions & 11 deletions

File tree

.github/workflows/auto-release.yml

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
name: 🚀 Auto Release
22
on:
33
push:
4-
branches:
5-
- main
4+
tags:
5+
- 'v*'
66

77
jobs:
88
release:
99
runs-on: macos-latest
10-
if: startsWith(github.event.head_commit.message , 'chore(release):')
1110
permissions:
1211
id-token: write
1312
contents: write
@@ -21,7 +20,7 @@ jobs:
2120
- name: Use Node.js
2221
uses: actions/setup-node@v4
2322
with:
24-
node-version: 20
23+
node-version: 24
2524
registry-url: 'https://registry.npmjs.org/'
2625

2726
- name: Install dependencies
@@ -38,22 +37,53 @@ jobs:
3837
- name: test
3938
run: npm run test
4039

41-
- name: Get Version from package.json
42-
id: get_version
43-
run: echo "VERSION=$$(node -p "require('./package.json').version")" >> $$GITHUB_OUTPUT
40+
- name: Parse Version and Tag
41+
id: meta
42+
run: |
43+
# 获取当前的 ref 名称,例如 v0.8.1-alpha.0
44+
REF_NAME=${{ github.ref_name }}
45+
echo "Current Ref: $REF_NAME"
46+
47+
# 去掉 'v' 前缀,得到 0.8.1-alpha.0
48+
VERSION=${REF_NAME#v}
49+
echo "clean_version=$VERSION" >> $GITHUB_OUTPUT
50+
51+
# 判断 tag 策略
52+
if [[ "$VERSION" == *"-"* ]]; then
53+
# 如果版本号带横杠 (如 -alpha.0),提取横杠后的第一个单词作为 tag (如 alpha)
54+
# 逻辑:取第一个 - 后面的内容,再取第一个 . 前面的内容
55+
NPM_TAG=$(echo "$VERSION" | cut -d'-' -f2 | cut -d'.' -f1)
56+
echo "Detected pre-release version. Setting npm tag to: $NPM_TAG"
57+
echo "npm_tag=$NPM_TAG" >> $GITHUB_OUTPUT
58+
else
59+
# 否则就是正式版
60+
echo "Detected stable version. Setting npm tag to: latest"
61+
echo "npm_tag=latest" >> $GITHUB_OUTPUT
62+
fi
4463
4564
- name: Publish to npm
46-
run: npm publish --provenance --access public
65+
run: |
66+
TAG_VERSION="${{ steps.meta.outputs.clean_version }}"
67+
PKG_VERSION=$(node -p "require('./package.json').version")
68+
69+
if [ "$TAG_VERSION" != "$PKG_VERSION" ]; then
70+
echo "Version mismatch: Tag ($TAG_VERSION) != Package ($PKG_VERSION). Syncing..."
71+
npm version $TAG_VERSION --no-git-tag-version --allow-same-version
72+
else
73+
echo "Versions match ($TAG_VERSION). Proceeding..."
74+
fi
75+
76+
npm publish --provenance --access public --tag ${{ steps.meta.outputs.npm_tag }}
4777
env:
4878
CI: true
4979

5080
- name: Create GitHub Release
5181
uses: softprops/action-gh-release@v1
5282
with:
53-
tag_name: v${{ steps.get_version.outputs.VERSION }}
54-
name: Release v${{ steps.get_version.outputs.VERSION }}
83+
tag_name: ${{ github.ref_name }}
84+
name: Release ${{ github.ref_name }}
5585
generate_release_notes: true
5686
draft: false
57-
prerelease: false
87+
prerelease: ${{ contains(github.ref_name, '-') }}
5888
env:
5989
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)