Skip to content

Publish to npm

Publish to npm #3

Workflow file for this run

name: Publish to npm
on:
push:
branches: [main]
paths:
- 'package.json'
workflow_dispatch:
permissions:
id-token: write
contents: write
jobs:
check-version:
runs-on: ubuntu-latest
outputs:
should_publish: ${{ steps.check.outputs.should_publish }}
version: ${{ steps.check.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Check version change
id: check
run: |
VERSION=$(node -p "require('./package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "should_publish=true" >> $GITHUB_OUTPUT
exit 0
fi
git show HEAD~1:package.json > /tmp/old.json 2>/dev/null || {
echo "should_publish=true" >> $GITHUB_OUTPUT
exit 0
}
OLD_VERSION=$(node -p "require('/tmp/old.json').version" || echo "")
if [ "$VERSION" != "$OLD_VERSION" ]; then
echo "should_publish=true" >> $GITHUB_OUTPUT
else
echo "should_publish=false" >> $GITHUB_OUTPUT
fi
publish:
needs: check-version
if: needs.check-version.outputs.should_publish == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'
- name: Update npm for trusted publishing
run: npm install -g npm@latest
- run: npm ci
- run: npm run build
- run: npm test
- name: Publish
run: |
VERSION=$(node -p "require('./package.json').version")
if [[ "$VERSION" == *"-"* ]]; then
npm publish --tag next
else
npm publish
fi
- name: Create tag
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "v${{ needs.check-version.outputs.version }}" -m "v${{ needs.check-version.outputs.version }}" || true
git push origin "v${{ needs.check-version.outputs.version }}" || true
- name: Create Release
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ needs.check-version.outputs.version }}
generate_release_notes: true