From cdaecab7ae2aeb610287cc9985b3ef4dec29117a Mon Sep 17 00:00:00 2001 From: anghunk Date: Sat, 31 Jan 2026 12:03:38 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E5=B0=86=20npm=20=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E5=99=A8=E4=BB=8E=E6=A0=87=E7=AD=BE=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E6=94=B9=E4=B8=BA=E5=88=86=E6=94=AF=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 GitHub Actions 工作流,将发布触发条件从推送特定标签改为推送到 main 或 master 分支。 添加版本检查逻辑,仅在本地 package.json 版本与 npm 上已发布版本不同时才执行发布步骤,避免重复发布相同版本。 --- .github/workflows/npm_publish.yml | 38 ++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/.github/workflows/npm_publish.yml b/.github/workflows/npm_publish.yml index 62d3911..4586abf 100644 --- a/.github/workflows/npm_publish.yml +++ b/.github/workflows/npm_publish.yml @@ -2,8 +2,9 @@ name: Publish npm on: push: - tags: - - "widget-v*" + branches: + - main + - master jobs: publish: @@ -22,16 +23,47 @@ jobs: node-version: "20" registry-url: "https://registry.npmjs.org" + - name: Get local package info + id: pkg + working-directory: docs/widget + run: | + name=$(node -p "require('./package.json').name") + version=$(node -p "require('./package.json').version") + echo "name=$name" >> $GITHUB_OUTPUT + echo "version=$version" >> $GITHUB_OUTPUT + + - name: Get npm published version + id: npm + run: | + PUBLISHED_VERSION=$(npm view "${{ steps.pkg.outputs.name }}" version || echo "none") + echo "version=$PUBLISHED_VERSION" >> $GITHUB_OUTPUT + + - name: Decide whether to publish + id: decision + run: | + echo "local=${{ steps.pkg.outputs.version }}" + echo "remote=${{ steps.npm.outputs.version }}" + if [ "${{ steps.npm.outputs.version }}" = "none" ]; then + echo "should_publish=true" >> $GITHUB_OUTPUT + elif [ "${{ steps.pkg.outputs.version }}" != "${{ steps.npm.outputs.version }}" ]; then + echo "should_publish=true" >> $GITHUB_OUTPUT + else + echo "should_publish=false" >> $GITHUB_OUTPUT + fi + - name: Install dependencies + if: steps.decision.outputs.should_publish == 'true' working-directory: docs/widget run: npm ci - name: Build widget + if: steps.decision.outputs.should_publish == 'true' working-directory: docs/widget run: npm run build - name: Publish to npm + if: steps.decision.outputs.should_publish == 'true' working-directory: docs/widget env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - run: npm publish --access public \ No newline at end of file + run: npm publish --access public