Skip to content

Publish to NPM and Create Git Tag #12

Publish to NPM and Create Git Tag

Publish to NPM and Create Git Tag #12

Workflow file for this run

name: publish
run-name: Publish to NPM and Create Git Tag
on:
workflow_dispatch:
permissions:
id-token: write # Required for OIDC
contents: write
packages: write
jobs:
publish:
runs-on: ubuntu-latest
# Make sure you have created an environment named "npm-publish" in github under Settings > Environments
environment:
name: npm-publish
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '24'
registry-url: 'https://registry.npmjs.org/'
- name: Get package version
id: get-version
run: echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
- name: Check if version is already published to NPM
run: |
PUBLISHED=$(npm view $(node -p "require('./package.json').name") versions --json | grep -q "\"${{ steps.get-version.outputs.version }}\"" && echo "true" || echo "false")
if [ "$PUBLISHED" = "true" ]; then
echo "ERROR: Version ${{ steps.get-version.outputs.version }} is already published to NPM."
exit 1
fi
- name: Check if git tag exists
run: |
if git rev-parse "v${{ steps.get-version.outputs.version }}" >/dev/null 2>&1; then
echo "ERROR: Git tag v${{ steps.get-version.outputs.version }} already exists."
exit 1
fi
- name: Install dependencies
run: yarn install
# Remove the .npmrc and .yarnrc that is checked into the repo so that "npm publish" would recreate it with the added NPM_TOKEN secret
- name: Cleanup npmrc and yarnrc
run: |
rm -f .npmrc
rm -f .yarnrc
# NB: We explicitly publish to 'latest', because we don't yet have support for
# tag-specific publishing in this action, and Node 24+ will not allow you to
# publish prerelease versions without an explicit tag.
- name: Publish to NPM
run: npm publish --tag latest
- name: Create and push git tag
run: |
GIT_USER="${{ github.actor }}"
GIT_EMAIL="${GIT_USER}@users.noreply.github.com"
git config user.name "$GIT_USER"
git config user.email "$GIT_EMAIL"
git tag "v${{ steps.get-version.outputs.version }}"
git push origin "v${{ steps.get-version.outputs.version }}"