DEV Community

Cover image for Publish to NPM with Github Actions
Daniel Thompson-Yvetot for Tauri

Posted on

Publish to NPM with Github Actions

If you are as addicted to CI and CD as we are at Tauri, this brief article will show you how we solved publishing to NPM on the release tag event at Github.

Background

Our org is growing, and we don't want individuals to bear the responsibility for publishing to crates.io and npm. That's brittle and bus-factor waiting to happen. And doing things manually is always error prone.

What we did:

  1. Setup a CI user at NPM (don't choose 2FA), and copy their token.
  2. Create a secret at the repo settings, call it npm_token and paste the token as the secret value.
  3. Create a file at .github/workflows/publish.ymlwith the following contents:
name: NPM Publish

on:
  release:
    types: [published]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Use Node 12
      uses: actions/setup-node@v1
      with:
        # specify node version and the registry for the RELEASE build
        node-version: 12
        registry-url: https://registry.npmjs.org/
    - name: Build package
      run: |
        npm install -g yarn
        yarn install
        yarn rollup -c
    - name: Register Token
      run: |
        echo "//registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN" > /home/runner/work/_temp/.npmrc
        echo "_auth=$NODE_AUTH_TOKEN" >>  /home/runner/work/_temp/.npmrc
        echo "email=<your@email.address>" >>  /home/runner/work/_temp/.npmrc
        echo "always-auth=true" >>  /home/runner/work/_temp/.npmrc
      env:
        NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
    - name: Publish
      run: npm publish
Enter fullscreen mode Exit fullscreen mode

Now just publish a release and let the runner do its thing.

Let us know in the comments if you've got any improvements!


Top comments (3)

Collapse
 
nothingismagick profile image
Daniel Thompson-Yvetot • Edited

Just a heads up: in this case we’re actually running our build step with yarn rollup -c

But you might need to do something else!

Collapse
 
nombrekeff profile image
Keff

Cool, I will give this a try!!
How do you guys increment versions? Do you do that manually or use some kind of automation like standard-release or semantic-release?

Collapse
 
nothingismagick profile image
Daniel Thompson-Yvetot

That’s something at this library that we’re doing manually, but really this was just the testing ground for our main repo - which is a complicated mono repo with multiple crates and npm artifacts. We’ll be publishing that complete workflow really soon!