Skip to content

Commit 81a17c1

Browse files
committed
feat: Trigger publish workflow on release publication and automatically extract version from tag instead of requiring manual input.
1 parent 6969c9b commit 81a17c1

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

.github/workflows/publish.yml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
name: Publish to NPM
22

33
on:
4+
release:
5+
types: [published]
46
workflow_dispatch:
5-
inputs:
6-
version:
7-
description: "发布版本号 (例如: 1.0.0)"
8-
required: true
9-
type: string
107

118
permissions:
129
contents: write
@@ -24,6 +21,20 @@ jobs:
2421
fetch-depth: 0
2522
token: ${{ secrets.GITHUB_TOKEN }}
2623

24+
- name: Extract version from tag
25+
id: get_version
26+
run: |
27+
# Get tag from release event or use ref for workflow_dispatch
28+
if [ "${{ github.event_name }}" = "release" ]; then
29+
TAG="${{ github.event.release.tag_name }}"
30+
else
31+
TAG="${{ github.ref_name }}"
32+
fi
33+
# Remove 'v' prefix if present
34+
VERSION="${TAG#v}"
35+
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
36+
echo "Extracted version: $VERSION"
37+
2738
- name: Setup pnpm
2839
uses: pnpm/action-setup@v4
2940
with:
@@ -46,7 +57,7 @@ jobs:
4657
run: pnpm build
4758

4859
- name: Update version
49-
run: npm version ${{ github.event.inputs.version }} --no-git-tag-version --allow-same-version
60+
run: npm version ${{ steps.get_version.outputs.VERSION }} --no-git-tag-version --allow-same-version
5061

5162
- name: Publish to npm
5263
env:
@@ -58,5 +69,5 @@ jobs:
5869
git config user.name "github-actions[bot]"
5970
git config user.email "github-actions[bot]@users.noreply.github.com"
6071
git add package.json
61-
git diff --staged --quiet || git commit -m "chore: bump version to ${{ github.event.inputs.version }}"
72+
git diff --staged --quiet || git commit -m "chore: bump version to ${{ steps.get_version.outputs.VERSION }}"
6273
git push origin HEAD:main

0 commit comments

Comments
 (0)