< back

Automating Releases with Github, Travis and NPM

For some time I've been running into issues creating releases of my NPM packages. The main problem was in the manual steps involving package versioning. In this post I propose a simple method to automate the release of a package.

The big picture

The main problem was caused by coupling versioning with functionality. In the past I used to update version in package.json when new functionality was added as part of a pull request. Shortly afterwards, when Travis finished a build in master, a new release was produced.

With the new process, I can produce a new release+tag any time, by just specifying the version I want to be deployed, and Travis will make sure it gets deployed to NPM.

Configure Travis

First of all you'll need to setup your .travis.yml file to release on new created tags using tags: true:

  provider: npm
  email: <MY_EMAIL>
    secure: <MY_API_KEY>
    repo: <MY_REPO>
    tags: true

Instructions on how to setup your NPM Token can be found in this previous post.

Release script

The following script perform the following actions:

  1. Checks out master branch and ensures there are no uncommitted changes for safety purposes.
  2. Updates package.json using npm version
  3. Commits and pushes the change into master branch.
  4. Creates a new github release using hub CLI.


#!/usr/bin/env bash

set -e

if [[ $# -eq 0 ]] ; then
    echo 'version not provided: "./release.sh <VERSION>"'
    exit 1


echo " -- preparing master branch"
git fetch
git checkout master
if [ ! -z "$(git status --untracked-files=no --porcelain)" ]; then 
  echo "there are uncommitted changes in tracked files"
  exit 1

echo " -- updating version"
npm version --no-git-tag-version ${NEW_VERSION}
git commit package.json package-lock.json -m "updated version to ${NEW_VERSION}"
git push origin master

echo " -- creating github release"
hub release create ${NEW_VERSION} -m "${NEW_VERSION}"

echo "Done."

Usage example:

$ sh release.sh 0.9.3

 -- preparing master branch
Already on 'master'
Your branch is up to date with 'origin/master'.
 -- updating version
[master f1f3b3c] updated version to 0.9.3
 2 files changed, 2 insertions(+), 2 deletions(-)
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 393 bytes | 393.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/<MY_REPO>
   c3d8cd9..f1f3b3c  master -> master
 -- creating github release

comments powered by Disqus