Publishing gh-pages with Travis-CI

The solution at

All my latest articles can be found at the RisingStack blog.

At node-meatspace 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 Travis. 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:

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

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 package.json file. Make sure you have a script section in it:

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

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 @madbence and @oroce, thank you guys!



Engineering Manager | Built @RisingStack, @GodaddyOSS @BaseWebReact | Find me at

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