Tim Scanlin

Coder, Maker, & Designer

Automated Deploys With Travis

Using Travis CI is a great way to automate repetitive tasks like building and testing your projects. But recently I've been using it to automate deployments to github pages and it's been great. I have read a few tutorials that outlined similar steps, but they didn't quite have the results I wanted so I decided to write my own. To automate deploying to github pages with Travis, here's the steps I followed:

  1. Get a personal access token from github.com Note: You only get to see this key once.

  2. Setup your repository in Travis (at https://travis-ci.org/) I also suggest limiting concurrent builds to 1 in the repo's Travis settings so that builds run sequentially.

  3. npm install -g travis-encrypt (https://www.npmjs.com/package/travis-encrypt)

  4. travis-encrypt -r [repository slug] GITHUB_TOKEN=[personal access token] Note: The repository slug includes your username.

  1. Then setup your .travis.yml file, you need to changes the GITHUB_REPO and secure options.
language: node_js
  - "6.9"
sudo: false
    - master
- npm install
- npm run build
- bash ./travis-deploy.sh
    - GITHUB_REPO: [repository slug]
    - secure: [result from travis-encrypt command]
  1. And include the travis-deploy script:
set -o errexit

# Info.
echo "Running deploy script"
echo "Branch: $TRAVIS_BRANCH"
echo "Is a Pull Request: $TRAVIS_PULL_REQUEST"

# Exit if the branch is not the master branch.
if [ "$TRAVIS_BRANCH" != "master" ]
  echo "This commit was made against the $TRAVIS_BRANCH and not the master. No deploy."
  exit 0

# Exit if the commit is a pull request.
# We only want to build / deploy when pull requests are merged into master.
if [ "$TRAVIS_PULL_REQUEST" != "false" ]
  echo "This commit is a pull request. No deploy."
  exit 0

# Git config.
git config --global user.email "nobody@nobody.org"
git config --global user.name "Travis CI"

# Build steps (taken care of beforehand)

# Deploy.
cd build
git init
git add .
git commit -m "Deploy to Github Pages"
git push --force --quiet "https://${GITHUB_TOKEN}@github.com/${GITHUB_REPO}.git" master:gh-pages > /dev/null 2>&1
  1. Also, make the gh-pages branch on github before running the above script from travis-ci just to be safe.

This will deploy to the gh-pages branch on any commits into master as long as the build finishes successfully [even if not done through a pull request in case of emergencies ;)]. On pull requests, travis will still try and build the branch to make sure that succeeds, but it won't push any of the changes to gh-pages branch.

See this in action in the github repo for this blog at https://github.com/tscanlin/blog.timscanlin.net

28 Oct 2016

© 2022 Tim Scanlin