Skip to content

Publish to npm

Publish to npm #1

Workflow file for this run

name: Publish to npm
on:
push:
branches: [main]
paths:
- 'package.json'
workflow_dispatch:
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
registry-url: 'https://registry.npmjs.org'
- run: pnpm install --frozen-lockfile
- run: pnpm build
- run: pnpm test
- name: Check if version is already published
id: check
run: |
PACKAGE_NAME=$(node -e "console.log(require('./package.json').name)")
PACKAGE_VERSION=$(node -e "console.log(require('./package.json').version)")
echo "name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
echo "version=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
if npm view "$PACKAGE_NAME@$PACKAGE_VERSION" version 2>/dev/null; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "Version $PACKAGE_VERSION already published, skipping."
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "Version $PACKAGE_VERSION not published yet, will publish."
fi
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish to npm
if: steps.check.outputs.exists == 'false'
run: pnpm publish --access public --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}