Last active
October 7, 2019 05:54
-
-
Save pghalliday/240fe740d523dad21d3f to your computer and use it in GitHub Desktop.
Auto build and deploy GitHub pages with Travis-CI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Deploy built site to this branch | |
TARGET_BRANCH=master | |
# Sync the contents of this directory where the site should have been built | |
SOURCE_DIR=_site | |
if [ ! -d "$SOURCE_DIR" ]; then | |
echo "SOURCE_DIR ($SOURCE_DIR) does not exist, build the source directory before deploying" | |
exit 1 | |
fi | |
REPO=$(git config remote.origin.url) | |
if [ -n "$TRAVIS_BUILD_ID" ]; then | |
# When running on Travis we need to use SSH to deploy to GitHub | |
# | |
# The following converts the repo URL to an SSH location, | |
# decrypts the SSH key and sets up the Git config with | |
# the correct user name and email (globally as this is a | |
# temporary travis environment) | |
# | |
# Set the following environment variables in the travis configuration (.travis.yml) | |
# | |
# DEPLOY_BRANCH - The only branch that Travis should deploy from | |
# ENCRYPTION_LABEL - The label assigned when encrypting the SSH key using travis encrypt-file | |
# GIT_NAME - The Git user name | |
# GIT_EMAIL - The Git user email | |
# | |
echo DEPLOY_BRANCH: $DEPLOY_BRANCH | |
echo ENCRYPTION_LABEL: $ENCRYPTION_LABEL | |
echo GIT_NAME: $GIT_NAME | |
echo GIT_EMAIL: $GIT_EMAIL | |
if [ "$TRAVIS_BRANCH" != "$DEPLOY_BRANCH" ]; then | |
echo "Travis should only deploy from the DEPLOY_BRANCH ($DEPLOY_BRANCH) branch" | |
exit 0 | |
else | |
if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then | |
echo "Travis should not deploy from pull requests" | |
exit 0 | |
else | |
ENCRYPTED_KEY_VAR=encrypted_${ENCRYPTION_LABEL}_key | |
ENCRYPTED_IV_VAR=encrypted_${ENCRYPTION_LABEL}_iv | |
ENCRYPTED_KEY=${!ENCRYPTED_KEY_VAR} | |
ENCRYPTED_IV=${!ENCRYPTED_IV_VAR} | |
REPO=${REPO/git:\/\/github.com\//[email protected]:} | |
# The `deploy_key.enc` file should have been added to the repo and should | |
# have been created from the deploy private key using `travis encrypt-file` | |
openssl aes-256-cbc -K $ENCRYPTED_KEY -iv $ENCRYPTED_IV -in deploy_key.enc -out deploy_key -d | |
chmod 600 deploy_key | |
eval `ssh-agent -s` | |
ssh-add deploy_key | |
git config --global user.name "$GIT_NAME" | |
git config --global user.email "$GIT_EMAIL" | |
fi | |
fi | |
fi | |
REPO_NAME=$(basename $REPO) | |
TARGET_DIR=$(mktemp -d /tmp/$REPO_NAME.XXXX) | |
REV=$(git rev-parse HEAD) | |
git clone --branch ${TARGET_BRANCH} ${REPO} ${TARGET_DIR} | |
rsync -rt --delete --exclude=".git" --exclude=".nojekyll" --exclude=".travis.yml" $SOURCE_DIR/ $TARGET_DIR/ | |
cd $TARGET_DIR | |
git add -A . | |
git commit --allow-empty -m "Built from commit $REV" | |
git push $REPO $TARGET_BRANCH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
language: ruby | |
rvm: | |
- 1.9.3 | |
# The build script should also call `jekyll build` | |
script: | |
- bundle exec rake build | |
after_success: | |
- ./deploy.sh | |
# The encryption label is the unique key used in the environment | |
# variables set up when calling `travis encrypt-file` on the SSH key | |
env: | |
global: | |
- ENCRYPTION_LABEL="YOUR_ENCRYPTION_LABEL" | |
- GIT_NAME="YOUR_NAME" | |
- GIT_EMAIL="YOUR_EMAIL" | |
- DEPLOY_BRANCH="deploy" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# make sure you ignore the SSH keys and only commit the | |
# encrypted version. You may also want to back up the original | |
# key files elsewhere | |
deploy_key | |
deploy_key.pub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set up the `master` branch so that Travis-CI | |
# doesn't try to build it | |
branches: | |
except: | |
- master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment