Publishing gh-pages with Travis-CI

The solution at

Gergely Nemeth
Jan 21, 2014 · 2 min read

All my latest articles can be found at the blog.

At we faced one recurring task every time a pull request was merged: we had to run our build script (which builds html from markdown), and push the changes back to GitHub.

Now we only have to push the merge button, and the gh-pages branch updates auto-magically. But how?

Using . The entry point of the Travis execution is the .travis.yml file. As our build script is written in Node, we have to enable it. Also we have to specify what script to run:

language: node_js
- '0.10'
script: npm run-script deploy
- secure: RIbIq8hI153J5trRa........

In the env section we have to set our repo url and a secure token. This is how you can create yours:

  1. Go to -> Settings -> Applications -> Personal Access Tokens — > Create new token, and copy it to your clipboard
  2. Let’s generate our secure token, and copy it to the .travis.yml file
npm install travis-encrypt -g
travis-encrypt -r [repository slug] -k GH_TOKEN -v [the token you created before]

Congrats, your .travis.yml is done! ☺

As in the script section of the travis file we wrote npm run-script, it will look it up in our file. Make sure you have a script section in it:

"scripts": {
"deploy": "./"

The only thing we have left is to write our file!

rm -rf out || exit 0;
mkdir out;
node build.js
( cd out
git init
git config "Travis-CI"
git config ""
cp ../countryiso.js ./countryiso.js
git add .
git commit -m "Deployed to Github Pages"
git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:gh-pages > /dev/null 2>&1

This snippet just creates a directory (called out), then runs the build.js, which puts its output into that directory. After that, we initizalize a new git repository in that directory, copy some necessary files from the parent directory, then push all the things to the gh-pages branch. Notice the > /dev/null 2>&1 on the end! It prevents any unwanted sensitive information to be recorded in the travis logs (if push fails for any reason).

I hope it all makes sense, and helps your open-source project to build the documentation/readme easier.

Inspired by and , thank you guys!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store