Publish npm package with github actions!

Nowadays, I am going through my old npm packages and publishing them to npm automatically. I found a way to do it with github actions.

I will show you how to do it.

Create a new github repository

First, you need to create a new github repository.

Create a new npm package

Let’s say we have a package called my-package.

Create a new github action

Create a new file called .github/workflows/npm-publish.yml and add the following code:

name: Npm Publish

on:
  release:
    types: [published]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/
      - run: npm install
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: $

Create a new npm token

You need to create a new npm token. You can do it from NpmJs Settings. Go to Access Tokens and choose Generate new token -> Classic token. Choose Automation and click on Generate Token.

Add the token to the repository secrets

You need to add the token to the repository secrets. Go to the repository settings and click on Secrets and variables -> Actions -> New repository secret. Add the token to the NPM_AUTH_TOKEN and click on Add secret.

Create a new release

You need to create a new release. Go to the repository and click on Releases -> Draft a new release. Add a title and description and click on Publish release.