From a5bb8b8a69c102c5e11eba1924c6e47e5057e565 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Fri, 20 Jul 2018 22:13:14 -0400 Subject: [PATCH 1/3] Use Travis CI to test the book build and then deploy it to the site. --- .travis.yml | 28 +++++++++++++ tools/deploy/update_site_travis.bash | 61 ++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 .travis.yml create mode 100644 tools/deploy/update_site_travis.bash diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..1f82e24bb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,28 @@ +dist: trusty +sudo: false + +language: node_js +node_js: + - "4" + +cache: + directories: + - node_modules + +install: + - npm install gitbook-cli -g + - gitbook install + +before_script: + - mkdir -p "${TRAVIS_BUILD_DIR}"/build + +script: + - gitbook build . "${TRAVIS_BUILD_DIR}"/build + - env | sort + - | + if [[ "${TRAVIS_BRANCH}" == master && "${TRAVIS_PULL_REQUEST}" == false ]]; then + # Commits to master that are not pull requests, that is, only + # actual addition of code to master, should deploy the book to + # the site. + bash "${TRAVIS_BUILD_DIR}"/tools/deploy/update_site_travis.bash + fi diff --git a/tools/deploy/update_site_travis.bash b/tools/deploy/update_site_travis.bash new file mode 100644 index 000000000..767c9e0f4 --- /dev/null +++ b/tools/deploy/update_site_travis.bash @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +# update_site_travis.bash: Update a GitHub repo with the book build +# products. + +set -o errexit + +# Required environment variables: +# - DOCS_BRANCH_NAME: name of the remote branch serving the book +# - DOCS_REPO_NAME: name of the remote repo +# - DOCS_REPO_OWNER: name of the remote repo's owner +# - GH_TOKEN: [secret] Personal Access Token + +bold=$(tput bold) +normal=$(tput sgr0) + +if [ -z ${DOCS_BRANCH_NAME+x} ]; then + echo "${bold}\$DOCS_BRANCH_NAME is not set!${normal}" + exit 1 +elif [ -z ${DOCS_REPO_NAME+x} ]; then + echo "${bold}\$DOCS_REPO_NAME is not set!${normal}" + exit 1 +elif [ -z ${DOCS_REPO_OWNER+x} ]; then + echo "${bold}\$DOCS_REPO_OWNER is not set!${normal}" + exit 1 +elif [ -z ${GH_TOKEN+x} ]; then + echo "${bold}\$GH_TOKEN is not set!${normal}" + exit 1 +fi + +git config user.name "Travis CI User" +git config user.email "travis@travis-ci.org" + +GH_REPO_REF="github.com/${DOCS_REPO_OWNER}/${DOCS_REPO_NAME}.git" + +# Assume the book has already been built, and was moved to `build/` +# inside the same directory as this script. + +if [ -d build ]; then + echo "${bold}Cloning the website repo...${normal}" + git clone -b $DOCS_BRANCH_NAME https://git@${GH_REPO_REF} + rm -rf ./"${DOCS_REPO_NAME}"/* + cp -a ./build/* ./"${DOCS_REPO_NAME}" + pushd ./"${DOCS_REPO_NAME}" + # echo "www.algorithm-archive.org" > CNAME + echo "${bold}Adding changes...${normal}" + git add --all + echo "${bold}Committing...${normal}" + # This will return 1 if there are no changes, which should not + # result in failure. + git commit \ + -m "Deploy book to GitHub Pages Travis build: ${TRAVIS_BUILD_NUMBER}" \ + -m "Commit: ${TRAVIS_COMMIT}" || ret=$? + git push "https://${GH_TOKEN}@${GH_REPO_REF}" > /dev/null 2>&1 + popd +else + echo "" >&2 + echo "${bold}Warning: The book wasn't found!${normal}" >&2 + echo "${bold}Warning: Not going to push the book to GitHub!${normal}" >&2 + exit 1 +fi From 799faea3549dbf81241739f9b8ab185c89712c33 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Sun, 23 Sep 2018 17:33:36 -0400 Subject: [PATCH 2/3] A CNAME file is needed for the correct URL redirect. --- tools/deploy/update_site_travis.bash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/deploy/update_site_travis.bash b/tools/deploy/update_site_travis.bash index 767c9e0f4..cd923d985 100644 --- a/tools/deploy/update_site_travis.bash +++ b/tools/deploy/update_site_travis.bash @@ -38,11 +38,11 @@ GH_REPO_REF="github.com/${DOCS_REPO_OWNER}/${DOCS_REPO_NAME}.git" if [ -d build ]; then echo "${bold}Cloning the website repo...${normal}" - git clone -b $DOCS_BRANCH_NAME https://git@${GH_REPO_REF} + git clone -b "${DOCS_BRANCH_NAME}" https://git@"${GH_REPO_REF}" rm -rf ./"${DOCS_REPO_NAME}"/* cp -a ./build/* ./"${DOCS_REPO_NAME}" pushd ./"${DOCS_REPO_NAME}" - # echo "www.algorithm-archive.org" > CNAME + echo "www.algorithm-archive.org" > CNAME echo "${bold}Adding changes...${normal}" git add --all echo "${bold}Committing...${normal}" @@ -51,7 +51,7 @@ if [ -d build ]; then git commit \ -m "Deploy book to GitHub Pages Travis build: ${TRAVIS_BUILD_NUMBER}" \ -m "Commit: ${TRAVIS_COMMIT}" || ret=$? - git push "https://${GH_TOKEN}@${GH_REPO_REF}" > /dev/null 2>&1 + git push "https://${GH_TOKEN}@${GH_REPO_REF}" popd else echo "" >&2 From 8cae8b1e5b1fbff8bf98c12286029babfae0f5c9 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Sun, 23 Sep 2018 19:59:54 -0400 Subject: [PATCH 3/3] Disable caching Node modules for Travis --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1f82e24bb..c212e7863 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,9 @@ language: node_js node_js: - "4" -cache: - directories: - - node_modules +# cache: +# directories: +# - node_modules install: - npm install gitbook-cli -g