|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# update_site_travis.bash: Update a GitHub repo with the book build |
| 4 | +# products. |
| 5 | + |
| 6 | +set -o errexit |
| 7 | + |
| 8 | +# Required environment variables: |
| 9 | +# - DOCS_BRANCH_NAME: name of the remote branch serving the book |
| 10 | +# - GH_REPO_NAME: name of the remote repo |
| 11 | +# - GH_TOKEN: [secret] Personal Access Token |
| 12 | +# - GH_USER: name of the remote repo's owner |
| 13 | + |
| 14 | +bold=$(tput bold) |
| 15 | +normal=$(tput sgr0) |
| 16 | + |
| 17 | +if [ -z ${DOCS_BRANCH_NAME+x} ]; then |
| 18 | + echo "${bold}\$DOCS_BRANCH_NAME is not set!${normal}" |
| 19 | + exit 1 |
| 20 | +elif [ -z ${GH_REPO_NAME+x} ]; then |
| 21 | + echo "${bold}\$GH_REPO_NAME is not set!${normal}" |
| 22 | + exit 1 |
| 23 | +elif [ -z ${GH_TOKEN+x} ]; then |
| 24 | + echo "${bold}\$GH_TOKEN is not set!${normal}" |
| 25 | + exit 1 |
| 26 | +elif [ -z ${GH_USER+x} ]; then |
| 27 | + echo "${bold}\$GH_USER is not set!${normal}" |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +git config user.name "Travis CI User" |
| 32 | +git config user.email "travis@travis-ci.org" |
| 33 | + |
| 34 | +GH_REPO_REF="github.com/${GH_USER}/${GH_REPO_NAME}.git" |
| 35 | + |
| 36 | +# Assume the book has already been built, and was moved to `build/` |
| 37 | +# inside the same directory as this script. |
| 38 | + |
| 39 | +if [ -d build ]; then |
| 40 | + echo "${bold}Cloning the website repo...${normal}" |
| 41 | + git clone -b $DOCS_BRANCH_NAME https://git@${GH_REPO_REF} |
| 42 | + rm -rf ./"${GH_REPO_NAME}"/* |
| 43 | + cp -a ./build/* ./"${GH_REPO_NAME}" |
| 44 | + pushd ./"${GH_REPO_NAME}" |
| 45 | + echo "${bold}Adding changes...${normal}" |
| 46 | + git add --all |
| 47 | + echo "${bold}Committing...${normal}" |
| 48 | + # This will return 1 if there are no changes, which should not |
| 49 | + # result in failure. |
| 50 | + git commit \ |
| 51 | + -m "Deploy book to GitHub Pages Travis build: ${TRAVIS_BUILD_NUMBER}" \ |
| 52 | + -m "Commit: ${TRAVIS_COMMIT}" || ret=$? |
| 53 | + git push --force "https://${GH_TOKEN}@${GH_REPO_REF}" > /dev/null 2>&1 |
| 54 | + popd |
| 55 | +else |
| 56 | + echo "" >&2 |
| 57 | + echo "${bold}Warning: The book wasn't found!${normal}" >&2 |
| 58 | + echo "${bold}Warning: Not going to push the book to GitHub!${normal}" >&2 |
| 59 | + exit 1 |
| 60 | +fi |
0 commit comments