|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Many aspsects of this came from https://gist.github.com/domenic/ec8b0fc8ab45f39403dd |
| 4 | +# Significant alterations |
| 5 | +# - Avoid pulling all history for cloned repo |
| 6 | +# - Support for multiple copies of documenation, |
| 7 | +# - only clearing out latest |
| 8 | +# - doc-history.md logging doc history |
| 9 | +# - Wrapped calls with actual encrypt keys with > /dev/null 2> /dev/null4 |
| 10 | + |
| 11 | +# How to run: |
| 12 | +# - From repository root .travis/push_docs_to_gh_pages.sh |
| 13 | + |
| 14 | +# Required files / directories (relative from repo root) |
| 15 | +# - Folder : "site" with that contains latest docs |
| 16 | +# - File : ".travis/deploy_key.enc" SSH deployment key encrypted by Travis command line tool |
| 17 | + |
| 18 | +# Required ENV Variables |
| 19 | +# ENCRYPTION_LABEL - Manually Set in travis web settings. Value can be displayed in LOG |
| 20 | +# encrypted_${ENCRYPTION_LABEL}_key - Set in web settings using travis cmdline encryption |
| 21 | +# encrypted_${ENCRYPTION_LABEL}_iv - Set in web settings using travis cmdline encryption |
| 22 | +# PAGES_TARGET_BRANCH="gh-pages" - Set in .travis.yml, branch were pages will be deployed |
| 23 | +# PAGES_VERSION_BASE="version3" -Set in .travis.yml, directory for pages deployment |
| 24 | +# TRAVIS_* variables are set by travis directly and only need to be if testing externally |
| 25 | + |
| 26 | +# Pull requests are special... |
| 27 | +# They won't have acceess to the encrypted variables. |
| 28 | +# This prevent access to the Encrypted SSH Key |
| 29 | +# Regardless we don't want a pull request automatically updating the repository |
| 30 | +if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then |
| 31 | + |
| 32 | + # ENV Variable checks are to help with configuration troubleshooting |
| 33 | + # they silently exit with unique message. Anyone one of them not set |
| 34 | + # can be used to turn off this functionality. |
| 35 | + # For example a feature branch in the master repository that we don't want docs produced for yet. |
| 36 | + [[ -n "$PAGES_VERSION_BASE" ]] || { echo "PAGES_VERSION_BASE Missing"; exit 0; } |
| 37 | + [[ -n "$PAGES_TARGET_BRANCH" ]] || { echo "PAGES_TARGET_BRANCH Missing"; exit 0; } |
| 38 | + [[ -n "$ENCRYPTION_LABEL" ]] || { echo "ENCRYPTION_LABEL Missing"; exit 0; } |
| 39 | + |
| 40 | + # Fail because required script to generate documenation must have not been run, or was changed. |
| 41 | + [[ -d ./site ]] || { echo "site directory not found"; exit 1; } |
| 42 | + |
| 43 | + # Save some useful information |
| 44 | + REPO=`git config remote.origin.url` |
| 45 | + SSH_REPO=${REPO/https:\/\/github.com\//git@github.com:} |
| 46 | + SHA=`git rev-parse --verify HEAD` |
| 47 | + |
| 48 | + # shallow clone the repostoriy and switch to PAGES_TARGET_BRANCH branch |
| 49 | + mkdir pages |
| 50 | + cd pages |
| 51 | + git clone --depth 1 $REPO . |
| 52 | + PAGES_BRANCH_EXISTS=$(git ls-remote --heads $REPO $PAGES_TARGET_BRANCH) |
| 53 | + |
| 54 | + if [ -n "$PAGES_BRANCH_EXISTS" ] ; then |
| 55 | + echo "Pages Branch Found" |
| 56 | + echo "-$PAGES_BRANCH_EXISTS-" |
| 57 | + git remote set-branches origin $PAGES_TARGET_BRANCH |
| 58 | + git fetch --depth 1 origin $PAGES_TARGET_BRANCH |
| 59 | + git checkout $PAGES_TARGET_BRANCH |
| 60 | + else |
| 61 | + echo "Creating Pages Branch" |
| 62 | + git checkout --orphan $PAGES_TARGET_BRANCH |
| 63 | + git rm -rf . |
| 64 | + fi |
| 65 | + |
| 66 | + #clear out latest and copy site contents to it. |
| 67 | + echo "updating $VERSION_BASE/latest" |
| 68 | + mkdir -p $PAGES_VERSION_BASE/latest |
| 69 | + rm -rf $PAGES_VERSION_BASE/latest/**./* || exit 0 |
| 70 | + cp -a ../site/. $PAGES_VERSION_BASE/latest |
| 71 | + |
| 72 | + # If a Tagged Build then copy to it's own directory as well. |
| 73 | + if [ -n "$TRAVIS_TAG" ]; then |
| 74 | + echo "Creating $PAGES_VERSION_BASE/$TRAVIS_TAG" |
| 75 | + mkdir -p $PAGES_VERSION_BASE/$TRAVIS_TAG || exit 0 |
| 76 | + cp -a ../site/. $PAGES_VERSION_BASE/$TRAVIS_TAG |
| 77 | + fi |
| 78 | + |
| 79 | + #Check if there are doc changes, if none exit the script |
| 80 | + if [[ -z `git diff --exit-code` ]] && [ -n "$PAGES_BRANCH_EXISTS" ] ; then |
| 81 | + echo "No changes to docs detected." |
| 82 | + exit 0 |
| 83 | + fi |
| 84 | + |
| 85 | + #Chganges where detected, so it's safe to write to log. |
| 86 | + now=$(date +"%d %b %Y - %r") |
| 87 | + export latest=" - [Latest Doc Change]($PAGES_VERSION_BASE/latest/) - Created $now" |
| 88 | + if [ ! -f doc-history.md ]; then |
| 89 | + echo "<!-- Auto generated in .travis/push_docs_to_gh_pages.sh -->" >doc-history.md |
| 90 | + echo "#Doc Generation Log" >>doc-history.md |
| 91 | + echo "" >>doc-history.md |
| 92 | + echo "- 4th line placeholder see below" >>doc-history.md |
| 93 | + echo "" >>doc-history.md |
| 94 | + echo "##Released Version Doc History" >>doc-history.md |
| 95 | + echo "" >>doc-history.md |
| 96 | + fi |
| 97 | + if [ -n "$TRAVIS_TAG" ]; then |
| 98 | + echo " - [$TRAVIS_TAG]($PAGES_VERSION_BASE/$TRAVIS_TAG/) - Created $now" >>doc-history.md |
| 99 | + fi |
| 100 | + |
| 101 | + #replace 4th line in log |
| 102 | + sed -i '4s@.*@'"$latest"'@' doc-history.md |
| 103 | + |
| 104 | + #Setup Git with Commiter info |
| 105 | + git config user.name "Travis CI" |
| 106 | + git config user.email "utplsql@users.noreply.github.com" |
| 107 | + |
| 108 | + #Add and Commit the changes back to pages repo. |
| 109 | + git add . |
| 110 | + git commit -m "Deploy to GitHub Pages: ${SHA}" |
| 111 | + |
| 112 | + #unencrypt and configure deployment key |
| 113 | + [[ -e ../.travis/deploy_key.enc ]] || { echo ".travis/deploy_key.enc file not found"; exit 1; } |
| 114 | + ENCRYPTED_KEY_VAR="encrypted_${ENCRYPTION_LABEL}_key" |
| 115 | + ENCRYPTED_IV_VAR="encrypted_${ENCRYPTION_LABEL}_iv" |
| 116 | + ENCRYPTED_KEY=${!ENCRYPTED_KEY_VAR} > /dev/null 2> /dev/null |
| 117 | + ENCRYPTED_IV=${!ENCRYPTED_IV_VAR} > /dev/null 2> /dev/null |
| 118 | + openssl aes-256-cbc -K $ENCRYPTED_KEY -iv $ENCRYPTED_IV -in ../.travis/deploy_key.enc -out ../.travis/deploy_key -d > /dev/null 2> /dev/null |
| 119 | + chmod 600 ../.travis/deploy_key |
| 120 | + eval `ssh-agent -s` |
| 121 | + ssh-add ../.travis/deploy_key |
| 122 | + |
| 123 | + # Now that we're all set up, we can push. |
| 124 | + echo "git push $SSH_REPO $PAGES_TARGET_BRANCH" |
| 125 | + git push $SSH_REPO $PAGES_TARGET_BRANCH |
| 126 | +fi |
0 commit comments