Automate package publishing in Node.js using semantic-release

April 10, 2017
by Rahil Shaikh
  • Fully automated package publishing

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.

  1. Changing the package version
  2. Creating tags
  3. Adding Github release
  4. 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.

Usage

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.

npm i semantic-release-cli -g

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.

semantic-release-cli setup

This will take you through the semantic-release-cli setup. Enter the details as it asks.

semantic release cli

semantic release cli

Once the setup is complete, you will have a Travis configuration file .travis.yml and package.json file looking like this.

.travis.yml

language: node_js
cache:
  directories:
    - node_modules
notifications:
  email: false
node_js:
  - '7'
  - '6'
  - '4'
before_script:
  - npm prune
after_success:
  - npm run semantic-release
branches:
  except:
    - /^v\d+\.\d+\.\d+$/
package.json

{
  "name": "semantically-released",
  "version": "0.0.0-semantically-released",
  "description": "A demo package to demonstrate semantic-release-cli",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 0",
    "semantic-release": "semantic-release pre && npm publish && semantic-release post"
  },
  "keywords": [
    "semantic-release"
  ],
  "author": "Rahil Shaikh",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/rahil471/semantically-released.git"
  },
  "devDependencies": {
    "semantic-release": "^6.3.2"
  }
}

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.

git release

A github release looks like this

Conclusion

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.

About

Engineer. Blogger. Thinker. Loves programming and working with emerging tech. We can also talk on Football, Gaming, World Politics, Monetary Systems.

Free PDF

Subscribe and get AngularJS Tips PDF. We never spam!
First Name:
Email:

Leave a Comment