Publishing a real NodeJS package: Publishing with GitHub Actions

Róbert Darida
Webtips
Published in
3 min readApr 7, 2021

--

For one of my TypeScript projects, I have needed a Windows-compatible unzip tool. I haven’t found one, which has a d.ts declaration file. So I decided that I will implement a zip/unzip NodeJs package with build-in TypeScript declarations.

Of course, it would be easier to write a declaration file for one of the existing tools, but a step-by-step guide about publishing a NodeJS package would be more useful.

Due to the length of the topic, I split the guide into four articles:

  • Publishing with GitHub Actions (current)

You can find the source code of this article here:

Publishing with GitHub Actions

What is GitHub Actions

GitHub Actions is a CI / CD tool for building, testing, and deploying your software. Here is a short introduction video about GitHub Actions:

Generating Access Token

For the automatic publishing to npmjs.com, we need to generate a new access token at npmjs.com > Account > Access Tokens page:

Creating a new npm access token.

After clicking on the “Generate New Token” button, the following page will appear. Choose the “Automation” option, and click on the “Generate Token” button:

Here is our new access token:

The generated token. (Sorry, I removed my token :D)

We should add this token into our GitHub repository as a secret on the following page:

Adding a new secret.

The Release Script

GitHub Workflows

Create a .github/workflows directory structure in the base directory of your local repository, then add an empty release.yml file.

Name the script

We should add a name for the script. This name will be used by GitHub Actions.

name: Release

Set up triggers

The script will run on any push into the main branch where the CHANGELOG.md file is updated. The CHANGELOG.md will be updated by the standard-version tool. This means the script will be triggered only if we create a new release.

on:
push:
braches:
- 'main'
paths:
- 'CHANGELOG.md'

Here is the content of the final release.yml file:

The content of the release.yml file.

Only two things are left: pushing everything into the git repository, and creating a new release:

git add .
git commit -m "feat: implement automatic release"
npm run release

The release script will update the CHANGELOG.md file, which will trigger GitHub Actions to run our release.yml :

The output of the release action on the GitHub Actions panel.

The script also updates the GitHub page of the windows-zip tool with the latest documentation and coverage report:

The GitHub page of the windows-zip tool.

If you enjoyed this article, please share, give a few claps, and most important stay tuned for the next one, which will about implementing a cross-platform NodeJS tool for zipping and unzipping.

Thanks for reading!

--

--