diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..30f933f --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,61 @@ +name: Publish to NPM + +on: + push: + tags: ['v*.*.*'] + +jobs: + publish-npm: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '18' + registry-url: 'https://registry.npmjs.org' + + - name: Setup pnpm + uses: pnpm/action-setup@v2 + with: + version: 8 + run_install: false + + - name: Get pnpm store directory + id: pnpm-cache + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT + + - name: Setup pnpm cache + uses: actions/cache@v4 + with: + path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Update version from tag + run: | + # 提取标签版本号(移除 'v' 前缀) + VERSION=${GITHUB_REF#refs/tags/v} + echo "Updating package.json version to $VERSION" + # 使用 jq 更新 package.json 中的版本号 + jq ".version = \"$VERSION\"" package.json > package.json.tmp + mv package.json.tmp package.json + # 显示更新后的版本号 + echo "Updated version in package.json:" + grep -m 1 "version" package.json + + - name: Build package + run: pnpm build + + - name: Publish to NPM + run: pnpm publish --no-git-checks --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}