name: Publish to npm on: workflow_run: workflows: ["Node.js CI"] types: [completed] branches: [main] jobs: check: if: github.event.workflow_run.conclusion == 'success' runs-on: ubuntu-latest permissions: contents: read outputs: should_publish: ${{ steps.version-check.outputs.should_publish }} steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v5 with: node-version: 24.x - name: Check if version is already published id: version-check run: | NAME=$(node -p "require('./package.json').name") LOCAL=$(node -p "require('./package.json').version") if npm view "${NAME}@${LOCAL}" version 2>/dev/null; then echo "should_publish=false" >> "$GITHUB_OUTPUT" echo "Version $LOCAL already published, skipping." else echo "should_publish=true" >> "$GITHUB_OUTPUT" echo "New version detected: $LOCAL" fi publish: needs: check if: needs.check.outputs.should_publish == 'true' runs-on: ubuntu-latest permissions: contents: write id-token: write steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v5 with: node-version: 24.x - run: npm ci - name: Build run: npm run dist - name: Determine npm tag id: npm-tag run: | VERSION=$(node -p "require('./package.json').version") if [[ "$VERSION" == *-* ]]; then echo "tag=beta" >> "$GITHUB_OUTPUT" else echo "tag=latest" >> "$GITHUB_OUTPUT" fi - name: Publish run: npm publish --provenance --access public --tag ${{ steps.npm-tag.outputs.tag }} - name: Create GitHub Release run: | VERSION=$(node -p "require('./package.json').version") PRERELEASE="" if [[ "$VERSION" == *-* ]]; then PRERELEASE="--prerelease" fi gh release create "v${VERSION}" --generate-notes $PRERELEASE env: GH_TOKEN: ${{ github.token }}