Triggering a Travis Build Programmatically

Published on Sunday, March 29, 2015

For Exalibur.js we wanted to be able to keep our master branch documentation up-to-date on the website. The website is built using and GitHub pages and after successfully automating my blog, naturally I turned to Travis CI to set up automated documentation generation.

Travis normally sets up a hook in your GitHub repository to automatically trigger a build after you push a commit (or pull request). This is real nice but unfortunately that's where it stops. If you want to trigger a build for another repository, you have to do it manually.

I asked Travis about this and their response is promising (Twitter can be nice sometimes):

Until that time, though, something has to be done.

One approach I saw was a small Ruby script to forge a webhook POST message. This is actually a nice idea but since Excalibur uses Node to build, I needed something else and didn't want to port it over.

Instead I borrowed some code from @patrickketner that uses the node-travis-ci npm package to submit a build through the public API. I modified Patrick's code since I need to use GitHub personal access tokens like I did previously.

You still need to follow the same steps to set up the GH_TOKEN environment variable but once you do, all you have to do is execute this Node.js script (changing the repo).

So for example, in your .travis.yml file, you just need:

  - npm install travis-ci
  - node trigger-build.js

All set. Now when you commit to one repository, you can trigger a build for another one. This allows us to automatically keep our docs up-to-date with whatever master has.

You can reference Excalibur to see how we execute the script. Additionally, you can reference's Travis configuration to see how we use TypeDoc to generate documentation for the latest version of master.

Don't miss any updates!

No spam and I usually send a newsletter once a quarter with content you won't see on the blog.

About Kamran

I'm a technologist, speaker, and Pluralsight author and I specialize in building full-stack solutions with a focus on modern web technology and cloud native architecture.

Kamran's Pluralsight courses

comments powered by Disqus
comments powered by Disqus