|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Assumes being called from the Biopython repository's root folder, |
| 4 | +# (i.e. a clone of https://github.com/biopython/biopython) as part |
| 5 | +# of our continuous integration testing to save the compiled docs |
| 6 | +# to https://github.com/biopython/docs |
| 7 | +# |
| 8 | +# In order to have write permissions, we put a private key into the |
| 9 | +# TravisCI settings as a secure environment variable, and put the |
| 10 | +# matching public key into the GitHub documentation repository's |
| 11 | +# settings as a deploy key with write permissions. |
| 12 | +# |
| 13 | +# Key creation, |
| 14 | +# |
| 15 | +# $ ssh-keygen -t rsa -b 4096 -C "biopython documentation deploy key" -f biopython_doc_key -N "" |
| 16 | +# Generating public/private rsa key pair. |
| 17 | +# Your identification has been saved in biopython_doc_key. |
| 18 | +# Your public key has been saved in biopython_doc_key.pub. |
| 19 | +# The key fingerprint is: |
| 20 | +# SHA256:nFfhbwryDLDz8eDEHa4sjdH0gOgwyXGGDUBGfDi5luQ biopython documentation deploy key |
| 21 | +# The key's randomart image is: |
| 22 | +# +---[RSA 4096]----+ |
| 23 | +# |===+o . | |
| 24 | +# |.B.*.. . . . | |
| 25 | +# |o X . o o . o | |
| 26 | +# | E + B * o . | |
| 27 | +# |. . + S * o | |
| 28 | +# | X @ . o | |
| 29 | +# | o * + . | |
| 30 | +# | . | |
| 31 | +# | | |
| 32 | +# +----[SHA256]-----+ |
| 33 | +# |
| 34 | +# Next, we add the public key to https://github.com/biopython/docs as |
| 35 | +# a deployment key with write permission, |
| 36 | +# |
| 37 | +# $ cat biopython_doc_key.pub |
| 38 | +# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDpQ3I6ZpL9cqUpqkHMPALTQg9ya3sL1MVXYjbTnuWnDoRml5UYXVgD8hgOJxwaxDo1BV+fKn68LXPEwlZ5FC6eRSCJz20SvWPkMDhAwChJJ+nE7f/vvK18R3Ge9ksWra8LFSR3EL7joQTN+c1VyaJH22qj1OED3G68Ix+bgvnUpZgeurV8vDV06FVx7H1Q5a5MoTWFdMa9wzJn5o6m7khditOTDKqznFULoOONpw7CsTiJD6drQPk1pwftDxEBMEAG7cKwux/dRWJtzsRQ7IO0d/AhzsqnLJJIgkHzQwmvpGpffWfoomNwF4bWJuWzu6tRcGcX16fLMyGK8kFJaL1zY6gQFkAbfsIdA2G28S79mIC4jT1JtiNYBOV9wIjxyZUyvzSeQGVC7yBafHE5eEb267dgGnDl654XzyIImLSKv/nx8No16UK/e5F+ds3hp0DPTknzeVOGBUEt1k8pEp47J9JVKoeceph0cJbfzFNv9pgOgyaHb1mhs9pI4kIQ3R+ibeAZbPWT709n26Y99Q2MSSZyPuZvX8VBA1NfoENmuTrEn/qqGlvZez3Blh4MIvYg24DFv/rHN92Edk5S7xY0eB7E6D6X/N80ThuBSqxlJpxSQlA+LICcq/EPd37/WT7exiheXysN5oIOvwNgUNNFftDWv2gPBu2bf/foHfAQKQ== biopython documentation deploy key |
| 39 | +# |
| 40 | +# Finally, we add the private key to TravisCI by going to |
| 41 | +# https://travis-ci.org/biopython/biopython/settings or any authorised |
| 42 | +# fork like https://travis-ci.org/peterjc/biopython/settings and |
| 43 | +# setting DOC_KEY to the following (secret) value: |
| 44 | +# |
| 45 | +# $ python -c "print(open('biopython_doc_key').read().strip().replace(' ', r'\ ').replace('\n', r'\\\n'))" |
| 46 | +# ... |
| 47 | +# |
| 48 | +# TravisCI requires we escape spaces as '\ ' and newlines as '\\n', and |
| 49 | +# we explicitly strip the trailing new line so that we don't get an extra |
| 50 | +# one when rebuilding the key later. |
| 51 | +# |
| 52 | +# Make sure "DISPLAY VALUE IN BUILD LOG" is off (the default). |
| 53 | +# |
| 54 | +# For testing locally, set local environment $DOC_KEY to this value. |
| 55 | +# Thereafter, when ever this script gets run on TravisCI it should |
| 56 | +# be able to deplop the HTML documentation to our documentation |
| 57 | +# repository (which will dispaly on biopython.org via GitHub pages). |
| 58 | + |
| 59 | +set -e |
| 60 | + |
| 61 | +if [ -z "$DOC_KEY" ]; then |
| 62 | + echo "Missing (secret) environment variable DOC_KEY," |
| 63 | + echo "which should hold the private SSH deployment key." |
| 64 | + false |
| 65 | +fi |
| 66 | + |
| 67 | +set -euo pipefail |
| 68 | + |
| 69 | +DEST_SLUG=biopython/docs |
| 70 | +# Could look at $TRAVIS_TAG, e.g. DEST_DIR=${TRAVIS_TAG:-dev} |
| 71 | +# However, tags lack the dots in the version number. Since |
| 72 | +# Biopython was installed to run Sphinx and build the docs, |
| 73 | +# can use this: |
| 74 | +DEST_DIR=`python -c "import Bio; v=Bio.__version__; print('dev' if 'dev' in v else v)"` |
| 75 | +SOURCE_DIR=${TRAVIS_BUILD_DIR:-$PWD}/Doc/api/_build/html |
| 76 | +WORKING_DIR=/tmp/deploy_biopython_docs |
| 77 | + |
| 78 | +if [ -z "$DEST_DIR" ]; then |
| 79 | + echo "ERROR: Failed to get Biopython version, is it not installed?" |
| 80 | + python -c "import Bio; print(Bio.__version__)" |
| 81 | + false |
| 82 | +fi |
| 83 | +DEST_DIR=$DEST_DIR/api |
| 84 | +echo "Aiming to deploy $SOURCE_DIR to $DEST_SLUG branch gh-pages as $DEST_DIR" |
| 85 | + |
| 86 | +# On TravisCI, must create the variable using '\ ' and '\n', so |
| 87 | +# here we must unescape the whitespace to recover the SSH deploy key: |
| 88 | +python -c "import os; print(os.environ['DOC_KEY'].strip().replace(r'\ ', ' ').replace(r'\n', '\n'))" > $HOME/.biopython_doc_deploy.key |
| 89 | +# Check we have a sane looking line structure: |
| 90 | +if [ `grep -c "^\-\-\-\-\-" $HOME/.biopython_doc_deploy.key` -ne 2 ]; then |
| 91 | + echo "ERROR: Failed to rebuild the SSH key," |
| 92 | + wc -l $HOME/.biopython_doc_deploy.key |
| 93 | + md5sum $HOME/.biopython_doc_deploy.key |
| 94 | + false |
| 95 | +fi |
| 96 | +chmod 600 $HOME/.biopython_doc_deploy.key |
| 97 | +export GIT_SSH=${TRAVIS_BUILD_DIR:-$PWD}/.github/ssh_via_deploy_key.sh |
| 98 | + |
| 99 | +if ! [[ -f "$GIT_SSH" ]]; then |
| 100 | + echo "Error, set GIT_SSH="$GIT_SSH" but does not exist" |
| 101 | + false |
| 102 | +elif ! [[ -x "$GIT_SSH" ]]; then |
| 103 | + echo "Error, set GIT_SSH="$GIT_SSH" but not executable" |
| 104 | + false |
| 105 | +fi; |
| 106 | + |
| 107 | +echo "Setting up clone of $DEST_SLUG locally at $WORKING_DIR" |
| 108 | + |
| 109 | +# Clone the destination under /tmp (public URL, no key needed) |
| 110 | +rm -rf $WORKING_DIR |
| 111 | +git clone https://github.com/$DEST_SLUG.git $WORKING_DIR |
| 112 | +pushd $WORKING_DIR |
| 113 | +git checkout gh-pages |
| 114 | +# Switch the git protocol to SSH based so we can use our key |
| 115 | +git remote set-url origin --push [email protected]: $DEST_SLUG.git |
| 116 | +popd |
| 117 | + |
| 118 | +echo "Copying $SOURCE_DIR/* to $WORKING_DIR/$DEST_DIR/ next" |
| 119 | +if [ ! -d $SOURCE_DIR ]; then |
| 120 | + echo "ERROR: Directory $SOURCE_DIR/ does not exist." |
| 121 | + false |
| 122 | +fi |
| 123 | + |
| 124 | +# Remove any old files |
| 125 | +pushd $WORKING_DIR |
| 126 | +if [ -d $DEST_DIR ]; then |
| 127 | + echo "Removing old files" |
| 128 | + git rm -r $DEST_DIR/ |
| 129 | +fi |
| 130 | +mkdir -p $DEST_DIR |
| 131 | +echo "Copying files" |
| 132 | +cp -R $SOURCE_DIR/* $DEST_DIR/ |
| 133 | +echo "Staging files in git" |
| 134 | +git add $DEST_DIR/ |
| 135 | + |
| 136 | +if [[ -z $(git status --porcelain) ]]; then |
| 137 | + echo "Nothing has changed, nothing needs pushing." |
| 138 | +else |
| 139 | + echo "Making commit of new files" |
| 140 | + git commit -m "Automated update ${TRAVIS_COMMIT:-}" --author "TravisCI <[email protected]>" |
| 141 | + echo "Finally, pushing to $DEST_SLUG gh-pages branch" |
| 142 | + git push origin gh-pages |
| 143 | + echo "Documentation deployed!" |
| 144 | +fi |
| 145 | + |
| 146 | +popd |
0 commit comments