DEV Community

Cover image for ๐Ÿš€ Stop Copy-Pasting NPM Publish Workflows - I Built a Reusable GitHub Action
Phuc (Felix) Bui
Phuc (Felix) Bui

Posted on

๐Ÿš€ Stop Copy-Pasting NPM Publish Workflows - I Built a Reusable GitHub Action

Ever find yourself copying the same GitHub Actions workflow across multiple npm packages? I got tired of maintaining duplicate publishing workflows, so I built a reusable action that handles it all.

The Problem ๐Ÿ˜ค

I was copy-pasting this same workflow across 10+ repositories:

  • Setup Node.js and PNPM
  • Run tests (if they exist)
  • Update package.json version
  • Build the package
  • Publish to NPM
  • Commit changes back

Every time I wanted to improve the workflow, I had to update it in multiple repos. Not fun!

The Solution โœจ

I created Publish NPM Action - a single reusable action that handles the entire npm publishing pipeline.

What it does:

  • ๐Ÿงช Smart testing - Automatically detects and runs tests
  • ๐Ÿ“ฆ Version sync - Updates package.json with your release tag
  • ๐Ÿ—๏ธ Flexible building - Works with any build system
  • ๐Ÿ”„ Git integration - Commits build files back to your repo
  • ๐Ÿ›ก๏ธ Secure publishing - Handles NPM authentication safely

Usage - Dead Simple ๐Ÿ’ซ

Just create .github/workflows/publish.yml:

name: Publish on Release

on:
  release:
    types: [published]

permissions:
  contents: write
  packages: write

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: main
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0

      - name: Publish NPM Package
        uses: phucbm/publish-npm-action@v1
        with:
          npm-token: ${{ secrets.NPM_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

That's it! Create a release, and your package automatically publishes to NPM.

Advanced Configuration ๐Ÿ› ๏ธ

Need customization? The action is highly configurable:

- name: Publish NPM Package
  uses: phucbm/publish-npm-action@v1
  with:
    npm-token: ${{ secrets.NPM_TOKEN }}
    # node-version: '20'                               # Node.js version, default is '18'
    # build-command: 'npm run build:prod'              # Build command, default is 'pnpm build'
    # output-dir: 'build/'                             # Output directory, default is 'dist/'
    # target-branch: 'develop'                         # Target branch, default is 'main'
    # skip-tests: 'true'                               # Skip tests, default is 'false'
    # commit-files: 'CHANGELOG.md docs/ types/'        # Additional files to commit, default is ''
Enter fullscreen mode Exit fullscreen mode

Perfect for different project setups:

  • React apps: output-dir: 'build/'
  • TypeScript libs: commit-files: 'types/'
  • Monorepos: Custom build commands and paths

Real-World Benefits ๐ŸŽฏ

Since switching to this action:

  • โœ… 15-line workflows instead of 80+ lines
  • โœ… Centralized updates - improve once, benefits everywhere
  • โœ… Zero maintenance per repository
  • โœ… Consistent publishing across all projects
  • โœ… Less copy-paste errors

Quick Setup Guide ๐Ÿ“‹

  1. Add NPM token to GitHub Secrets as NPM_TOKEN
  2. Copy the workflow above to .github/workflows/publish.yml
  3. Create a release with semantic versioning (e.g., v1.2.3)
  4. Watch the magic happen! ๐Ÿช„

The action handles everything: tests, builds, version updates, and publishing.

Try It Out! ๐Ÿš€

The action is available on the GitHub Marketplace.

Have feedback or feature requests? Drop them in the GitHub repo!


What's your npm publishing workflow like? Have you automated it? Drop a comment below! ๐Ÿ‘‡

Top comments (0)