Skip to content

Use Travis CI to test the book build and then deploy it to the site, take 2 #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions tools/deploy/update_site_travis.bash
Original file line number Diff line number Diff line change
@@ -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}"
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