Publish NPM Package #6
This file contains hidden or 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 NPM Package | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| id-token: write | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| registry-url: https://registry.npmjs.org | |
| - name: Verify npm token | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| run: | | |
| if [ -z "$NODE_AUTH_TOKEN" ]; then | |
| echo "NPM_TOKEN secret is required." | |
| exit 1 | |
| fi | |
| - name: Check published version | |
| id: package-version | |
| run: | | |
| PACKAGE_NAME="$(node -p "require('./package.json').name")" | |
| PACKAGE_VERSION="$(node -p "require('./package.json').version")" | |
| echo "package_name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT" | |
| echo "package_version=$PACKAGE_VERSION" >> "$GITHUB_OUTPUT" | |
| if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version >/dev/null 2>&1; then | |
| echo "already_published=true" >> "$GITHUB_OUTPUT" | |
| echo "Package ${PACKAGE_NAME}@${PACKAGE_VERSION} is already published. Skipping npm publish." | |
| exit 0 | |
| fi | |
| echo "already_published=false" >> "$GITHUB_OUTPUT" | |
| - name: Publish package | |
| if: steps.package-version.outputs.already_published != 'true' | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| run: npm publish --access public --provenance |