Automate package publishing in Node.js using semantic-release
If you have managed an open-source project you already know what steps are involved in releasing a new version of the package or a library.
- Changing the package version
- Creating tags
- Adding Github release
- Publishing to npm
Developers hate repeating the same things. Automation is the solution. That’s why we have a great package called semantic-release-cli an npm tool which uses semantic-release to completely automate the package publishing process.
Semantic release CLI as the name suggests follows semantic versioning. Let’s take a quick look at what semantic versioning is.
Given the version number say (1.0.0) (Major.Minor.Patch).
- Increment the Major number if you make API-incompatible changes.
- Increment the Minor number if you add features that are backward compatible.
- Increment the Patch number if you make backward compatible bug fixes.
You can read more about it here.
The semantic-release-cli works on existing projects. Which means your project must follow a standard node project convention, more importantly, it should have a valid package.json.
Let’s first install the package.
Semantic release CLI works with a Continuous Integration software. It also sets up Travis CI for us. If you wish to use any other CI software you can print tokens and use them.
In this tutorial, I’ll be using Travis CI. So before we go any further head over to travis-ci.org and create an account with your GitHub id if you haven’t.
Next, we will run the CLI setup.
This will take you through the semantic-release-cli setup. Enter the details as it asks.
Once the setup is complete, you will have a Travis configuration file .travis.yml and package.json file looking like this.
- npm prune
- npm run semantic-release
"description": "A demo package to demonstrate semantic-release-cli",
"test": "echo "Error: no test specified" && exit 0",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
"author": "Rahil Shaikh",
I have made a few changes to the package.json. I’ve set the package version to 0.0.0-semantically-released and I’ve set the test in the scripts to exit with 0, since we don’t have any tests for the demo.
To trigger a release we need to commit our code with messages following special conventions.
- Patch release – fix(pencil): stop graphite breaking when too much pressure applied: This Increments the patch number.
- Feature Release – feat(pencil): add ‘graphiteWidth’ option: This increments the MINOR number.
- Breaking Release – perf(pencil): remove graphiteWidth option: This increments the MAJOR number.
So all you now have to do is.
- git add
- git commit <message in the above format>
And semantic release CLI will take care of your releases. After Travis has successfully run all the tests and the build is successful you will see your package published on npm as well as a GitHub release.
Repeating steps in development can be time-consuming and will lead to reduced productivity. Developers must look to automate repetitive steps in the development process. Semantic Release CLI is an awesome utility one can use to automate the package release process for Node.js applications. Here we learned how we can set it up and use it. Give it a try in one of your projects I’m sure you will love it.
Leave a Comment