Publishing to Npm on Git commit using Github Hooks and Travis

Travis-CI can auto “npm publish” your git repo to npmjs, this means that whenever you commit to your github repository your software is always updated and available to users via npm update. This should also work with pull requests so no need to jump onto CLI After a merge, just hit merge and after a few minutes / hours you should see your application updated on npmjs.

Step 1.

Create Travis-CI account, ideally signing in with your github account.

Step 2.

Find the repository you want to auto publish on the Travis Web interface (under your profile) and click Enable.

Step 3.

Install Ruby Gems

sudo apt-get install rubygems

Step 4.

Install travis CLI gem and json gem

sudo gem install json
gem install travis

Step 5.

Create a .travis.yml file in the root of your repository that looks like this (replacing your email)

language: node_js
deploy:
  api_key:
  email: your@emailhere.com
  provider: npm

Step 6.

Get your npmjs api key and copy it into the clip board (Copy the bit after the _auth =)

cat ~/.npmrc | grep _auth

Step 7.

Run the Travis CI secret key generator

travis encrypt --add deploy.api_key

When prompted paste your APIKey then hit Control D, don’t add an additional line break or enter.

Step 8.

Add, Commit and Push the new .travis.yml file, this will trigger a build on Travis

git add .travis.yml && git commit -m "Travis auto publish config" && git push

Step 9.

Head over to your Travis page and wait for your build to begin, if it doesn’t it’s likely you didn’t push correctly up to github.

Doing lots of packages?

You will only need to complete steps 5 to 9 on future packages (assuming you enabled them on travis)

2 thoughts on “Publishing to Npm on Git commit using Github Hooks and Travis

  1. I am getting the following on step #7

    $ travis encrypt –add deploy.api_key
    not logged in, please run travis login –pro

    The suggestion it gives isn’t working, but I don’t see why I would need to log in anyway.

    Any suggestions?

Leave a Reply

Your email address will not be published. Required fields are marked *