feat: 🎸 update #18
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish to NPM | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- 'package.json' | |
jobs: | |
check-and-publish: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
id-token: write | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 2 # 需要获取前一个提交来比较版本 | |
- name: Check version change | |
id: check | |
run: | | |
OLD_VERSION=$(git show HEAD^:package.json | jq -r .version) | |
NEW_VERSION=$(jq -r .version package.json) | |
if [ "$OLD_VERSION" != "$NEW_VERSION" ] && [ "$NEW_VERSION" > "$OLD_VERSION" ]; then | |
echo "Version changed from $OLD_VERSION to $NEW_VERSION" | |
echo "version_changed=true" >> $GITHUB_OUTPUT | |
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
else | |
echo "Version unchanged or decreased" | |
echo "version_changed=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Setup Node.js | |
if: steps.check.outputs.version_changed == 'true' | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '22' | |
registry-url: 'https://registry.npmjs.org' | |
- name: Setup pnpm | |
if: steps.check.outputs.version_changed == 'true' | |
uses: pnpm/action-setup@v2 | |
with: | |
version: 8 | |
- name: Install dependencies | |
if: steps.check.outputs.version_changed == 'true' | |
run: pnpm install | |
- name: Build | |
if: steps.check.outputs.version_changed == 'true' | |
run: pnpm run build | |
- name: Publish to NPM | |
if: steps.check.outputs.version_changed == 'true' | |
run: pnpm publish --no-git-checks | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
NPM_CONFIG_PROVENANCE: true | |
- name: Create a tag | |
uses: salsify/action-detect-and-tag-new-version@v2 | |
if: steps.check.outputs.version_changed == 'true' |