Home

Publish npm package with GitHub Actions

I will walk you through my process when publishing packages to npm.

Creating repo

Create a new github repository with some name.

Creating new repository might be easier than you think, if you're not familiar with this method.

You can simply visit https://github.com/new in your browser to create a new repo.

Then give a name and some description for your repo and click on Create repository.

Create npm Auth token

Login to your account on npm.

Click on your profile picture in the top-right corner of the page.

Click on Auth Tokens.

You cannnot view the existing tokens, if you have any.

So create a new one, by clicking on Create New Token button.

Select the access level as Read and Publish.

Copy the new token to the clipboard. (just click on it to copy)

Add an encrypted secret

Go the GitHub repo.

Click on settings.

Click on secrets in the sidebar.

Click on Add a new secret.

Fill the name field as npm_token

Setup a local repo and connect it to GitHub.

Create a new folder on your PC.

Open your terminal(Git Bash is preferred), and navigate to newly created folder to make it as Current working directory.

You can check this by running pwd in your terminal.

Now initialize a git repo inside the folder using git init.

Initialize npm in that repo using npm init.

Give some unique name(which doesn't exist on npm) for name.

Provide valid values for all properties in package.json.

Make sure test command in package.json doesn't contain exit 1. Otherwise the workflow script might fail.

Connect this local repo to GitHub repo created earlier.

The command to this connection will be something like

git remote add origin https://github.com/<OWNER>/<REPO_NAME>.git

Add a workflow file

The workflow for GitHub action is created within a YAML file.

So create a workflow file in the path .github/workflows/main.yml

You can rename main.yml with any name but the extension should be .yml

My workflow to test and publish npm package will be:

name: CI

on:
  push:
    tags:
      - v*

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Use Node.js v12
        uses: actions/setup-node@v1
        with:
          node-version: 12
      - name: Test package
        run: |
          npm ci
          npm test

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Use Node.js v12
        uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/
      - name: Publish package
        run: |
          npm ci
          npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}

Note: The workflow runs from the second commit, since it is registered and activated in the first commit

Commit and Push the code

Code your package and commit your code.

Push your code using

git push -u origin master

Bonus

In the workflow that I stated earlier, the package will be published only when you push a tag. Commands I use in order:

git init
npm init
git add .
git commit -m "first commit"
git remote add origin https://github.com/<OWNER>/<REPO>.git
git push -u origin master
git tag -a v1.x.x -m 'first tag'
git push --follow-tags

You can change the workflow to the following to publish your package when you push your code to github.

name: CI

# Trigger on push
on: push

jobs:
  build:
    #  ...

Summary



Last Updated on

Next Post: Rules vs Guidelines →

Comments