💸Save up to $132K/month in CI costs!👉 Try Free
Skip to main content
← Back to workflows

How to Automatically Publish Packages to NPM with GitHub Actions

JS-DevTools-npm-publish -
GitHub Action
v3.1.1
616
Contributors
Contributor - JamesMessingerContributor - mcous
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: Publish to NPM
on:
push:
branches: main

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: "20"
- run: npm ci
- run: npm test
- uses: JS-DevTools/npm-publish@v3
with:
token: ${{ secrets.NPM_TOKEN }}

npm-publish logo

npm-publish

This action automatically publishes your package to NPM using GitHub Actions.


What is npm-publish?

The npm-publish action allows you to automatically publish your NPM packages from a GitHub repository. It ensures that packages are published only if the version in package.json differs from the latest on NPM.

How to Use token

The token input is required to authenticate with NPM or any other registry. Here's how to configure it:

with:
token: ${{ secrets.NPM_TOKEN }}

You can also publish to GitHub Package Registry by using secrets.GITHUB_TOKEN:

with:
token: ${{ secrets.GITHUB_TOKEN }}
registry: "https://npm.pkg.github.com"

How to Set the Registry URL

You can publish to any registry by setting the registry input:

with:
registry: 'https://npm.pkg.github.com'

How to Specify a Custom Package Directory

If your package is not in the root directory, you can specify the path:

with:
package: './path/to/package'

How to Use Distribution Tags

To control which tag the package will be published under (e.g., latest, beta), use the tag input:

with:
tag: 'beta'

How to Set Access Control

By default, packages are published publicly. If you want to restrict access, set the access input:

with:
access: 'restricted'

How to Run in Dry-Run Mode

You can test the publication without actually publishing anything by enabling the dry-run flag:

with:
dry-run: true

Outputs

The npm-publish action exposes several output variables that you can use in subsequent steps of your workflow. Make sure to set an ID for the action to access these outputs.

steps:
- uses: JS-DevTools/npm-publish@v3
id: publish
with:
token: ${{ secrets.NPM_TOKEN }}

- if: ${{ steps.publish.outputs.type }}
run: echo "Version changed!"

Available outputs:

  • new_tag: The newly calculated tag.
  • new_version: The new version of the package.
  • previous_version: The previously published version.
  • release_type: The type of release (major, minor, patch, etc.).