Publishing a real NodeJS package: Publishing with GitHub Actions
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:
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:
We should add this token into our GitHub repository as a secret on the following page:
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:
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 script also updates the GitHub page of the windows-zip tool with the latest documentation and coverage report:
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!