1#!/bin/bash
2set -e # Exit with nonzero exit code if anything fails
3
4SOURCE_BRANCH="master"
5TARGET_BRANCH="gh-pages"
6OUTPUT_PATH="$TRAVIS_BUILD_DIR/output"
7COMMIT_AUTHOR_EMAIL="${COMMIT_AUTHOR_EMAIL:-build@tarantool.org}"
8
9function doCompile {
10    mkdir benv
11    virtualenv benv
12    source benv/bin/activate
13    pip install sphinx sphinx_rtd_theme
14    cmake .
15    make sphinx-html
16    deactivate
17    rm -rf benv
18}
19
20# Pull requests and commits to other branches shouldn't try to deploy, just
21# build to verify
22if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_BRANCH" != "$SOURCE_BRANCH" ]; then
23    echo "documentation.sh: Skipping deploy; just doing a build."
24    doCompile
25    exit 0
26fi
27
28# Save some useful information
29REPO=`git config remote.origin.url`
30SSH_REPO=${REPO/https:\/\/github.com\//git@github.com:}
31SHA=`git rev-parse --verify HEAD`
32
33# Clone the existing gh-pages for this repo into $OUTPUT_PATH
34# Create a new empty branch if gh-pages doesn't exist yet (should only happen
35# on first deply)
36git clone $REPO $OUTPUT_PATH
37cd $OUTPUT_PATH
38git checkout $TARGET_BRANCH || git checkout --orphan $TARGET_BRANCH
39cd $TRAVIS_BUILD_DIR
40
41# Clean out existing contents
42rm -rf $OUTPUT_PATH/* || exit 0
43
44# Run our compile script
45doCompile
46
47# Now let's go have some fun with the cloned repo
48cd $OUTPUT_PATH
49git config user.name "Travis CI"
50git config user.email "$COMMIT_AUTHOR_EMAIL"
51
52# If there are no changes to the compiled out (e.g. this is a README update)
53# then just bail.
54if [ -z "`git diff --stat --exit-code`" ]; then
55    echo "documentation.sh: No changes to the output on this push; exiting."
56    exit 0
57fi
58
59# Commit the "changes", i.e. the new version.
60# The delta will show diffs between new and old versions.
61git add --all .
62git status
63git commit -m "Deploy to GitHub Pages: ${SHA}"
64
65# Get the deploy key by using Travis's stored variables to decrypt deploy_key.enc
66ENCRYPTED_KEY_VAR="encrypted_${ENCRYPTION_LABEL}_key"
67ENCRYPTED_IV_VAR="encrypted_${ENCRYPTION_LABEL}_iv"
68ENCRYPTED_KEY=${!ENCRYPTED_KEY_VAR}
69ENCRYPTED_IV=${!ENCRYPTED_IV_VAR}
70ENCRYPTED_KEY_PATH="${TRAVIS_BUILD_DIR}/deploy_key.enc"
71DECRYPTED_KEY_PATH="${TRAVIS_BUILD_DIR}/deploy_key"
72openssl aes-256-cbc -K $ENCRYPTED_KEY -iv $ENCRYPTED_IV -in "$ENCRYPTED_KEY_PATH" -out "$DECRYPTED_KEY_PATH" -d
73chmod 600 "$TRAVIS_BUILD_DIR/deploy_key"
74eval `ssh-agent -s`
75ssh-add "$TRAVIS_BUILD_DIR/deploy_key"
76
77# Now that we're all set up, we can push.
78git push $SSH_REPO $TARGET_BRANCH
79