Skip to content

Instantly share code, notes, and snippets.

@ptsurko
Last active March 11, 2016 20:06
Show Gist options
  • Save ptsurko/6e1cb3108c6b96f5a5ac to your computer and use it in GitHub Desktop.
Save ptsurko/6e1cb3108c6b96f5a5ac to your computer and use it in GitHub Desktop.
Github pages autodeploy using Travis CI

Github pages autodeploy using Travis CI

Create an OAuth token

Use https://github.com/settings/tokens/new

Install Travis CLI

npm install pwmckenna/node-travis-encrypt -g

Encrypt OAuth token

travis-encrypt -r <username>/<repo name> GH_TOKEN=<OAUTH TOKEN>

Configure Travis CI

language: node_js
node_js:
- 4.1
env:
  global:
  - GH_REF: github.com/<username>/<repo>.git
  - GIT_NAME: Travis-CI
  - GIT_EMAIL: travis@nodemeatspace.com
  - secure: <encrypted oAuth token>
before_install:
- chmod +x deploy_ghpages.sh
before_script:
- npm install
script: bash ./deploy_ghpages.sh
branches:
  only:
  - master

Configure deployer script

#!/bin/bash

# This makes sure, that we don't deploy pull requests to gh-pages :-)
if [ $TRAVIS_PULL_REQUEST != false ];
  then
  echo "Not deploying test-run for a pull request"
  exit 0
fi
(
  echo "Pushing build to ${GH_REF} gh-pages branch."
  git checkout -b gh-pages

  # inside this git repo we'll pretend to be a new user
  git config user.name "${GIT_NAME}"
  git config user.email "${GIT_EMAIL}"

  # The first and only commit to this new Git repo contains all the
  # files present with the commit message "Deploy to GitHub Pages".
  git add .
  git commit -m "Deployed to Github Pages" > /dev/null

  # Force push from the current repo's master branch to the remote
  # repo's gh-pages branch. (All previous history on the gh-pages branch
  # will be lost, since we are overwriting it.) We redirect any output to
  # /dev/null to hide any sensitive credential data that might otherwise be exposed.
  git push --force "https://${GH_TOKEN}@${GH_REF}" master:gh-pages > /dev/null 2>&1

  echo "New gh-pages branch has been pushed to ${GH_REF}."
)

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment