@@ -21,14 +21,14 @@ jobs:
2121
2222 steps :
2323 - name : 检出代码
24- uses : actions/checkout@v4
24+ uses : actions/checkout@v5
2525 with :
2626 fetch-depth : 0
2727
2828 - name : 设置 Node.js
2929 uses : actions/setup-node@v4
3030 with :
31- node-version : ' 18 '
31+ node-version : 22
3232 registry-url : ' https://registry.npmjs.org'
3333
3434 - name : 安装 pnpm
@@ -77,48 +77,50 @@ jobs:
7777 - name : 更新版本号 (手动触发时)
7878 if : github.event_name == 'workflow_dispatch'
7979 run : |
80- npm version ${{ steps.get_version.outputs.version }} --no-git-tag-version
81- git config --local user.email "action@github.com"
82- git config --local user.name "GitHub Action"
83- git add package.json
84- git commit -m "chore: bump version to ${{ steps.get_version.outputs.version }}"
85- git tag ${{ steps.get_version.outputs.tag_name }}
86- git push origin HEAD:${{ github.ref_name }}
87- git push origin ${{ steps.get_version.outputs.tag_name }}
80+ # 检查 tag 是否已经存在
81+ if git show-ref --tags --verify --quiet "refs/tags/${{ steps.get_version.outputs.tag_name }}"; then
82+ echo "⚠️ Tag ${{ steps.get_version.outputs.tag_name }} 已存在,跳过创建"
83+ else
84+ npm version ${{ steps.get_version.outputs.version }} --no-git-tag-version
85+ git config --local user.email "action@github.com"
86+ git config --local user.name "GitHub Action"
87+ git add package.json
88+ git commit -m "chore: bump version to ${{ steps.get_version.outputs.version }}"
89+ git tag ${{ steps.get_version.outputs.tag_name }}
90+ git push origin HEAD:${{ github.ref_name }}
91+ git push origin ${{ steps.get_version.outputs.tag_name }}
92+ fi
8893
8994 - name : 生成变更日志
9095 id : changelog
96+ run : npx changelogithub
97+ continue-on-error : true
98+ env :
99+ GITHUB_TOKEN : ${{secrets.GITHUB_TOKEN}}
100+
101+ - name : 发布到 npm
91102 run : |
92- # 获取上一个标签
93- PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
103+ # 检查版本是否已经发布到 npm
104+ PACKAGE_NAME=$(node -p "require('./package.json').name")
105+ CURRENT_VERSION=$(node -p "require('./package.json').version")
94106
95- if [ -z "$PREV_TAG" ]; then
96- # 如果没有上一个标签,获取所有提交
97- CHANGELOG=$(git log --pretty=format:"- %s (%h)" --no-merges)
107+ # 尝试获取 npm 上的版本信息
108+ if npm view "${PACKAGE_NAME}@${CURRENT_VERSION}" version 2>/dev/null; then
109+ echo "⚠️ 版本 ${CURRENT_VERSION} 已存在于 npm,跳过发布"
98110 else
99- # 获取两个标签之间的提交
100- CHANGELOG=$(git log ${PREV_TAG}..HEAD --pretty=format:"- %s (%h)" -- no-merges)
111+ echo "📦 发布版本 ${CURRENT_VERSION} 到 npm..."
112+ pnpm publish -- no-git-checks
101113 fi
102-
103- # 保存到文件以避免特殊字符问题
104- echo "$CHANGELOG" > changelog.txt
105- echo "changelog_file=changelog.txt" >> $GITHUB_OUTPUT
106-
107- - name : 发布到 npm
108- run : pnpm publish --no-git-checks
109114 env :
110115 NODE_AUTH_TOKEN : ${{ secrets.NPM_TOKEN }}
111116
112- - name : 创建 GitHub Release
113- uses : actions/create-release@v1
114- env :
115- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
117+ - name : Create GitHub Release
118+ uses : ncipollo/release-action@v1
116119 with :
117- tag_name : ${{ steps.get_version.outputs.tag_name }}
118- release_name : Release ${{ steps.get_version.outputs.tag_name }}
119- body_path : ${{ steps.changelog.outputs.changelog_file }}
120- draft : false
121- prerelease : false
120+ generateReleaseNotes : " true"
121+ allowUpdates : true
122+ skipIfReleaseExists : false
123+ omitBodyDuringUpdate : false
122124
123125 - name : 通知发布成功
124126 run : |
0 commit comments