| 1 | #!/bin/bash |
| 2 | |
| 3 | # Auto-deploy sphinx documentation with Github Actions |
| 4 | # Credit: https://gist.github.com/domenic/ec8b0fc8ab45f39403dd |
| 5 | set -o xtrace # Print command traces before executing command |
| 6 | set -e # Exit with nonzero exit code if anything fails |
| 7 | echo $BASH_VERSION # For debugging |
| 8 | |
| 9 | # Setup variables |
| 10 | COMMIT_AUTHOR_NAME="Github Actions" |
| 11 | COMMIT_AUTHOR_EMAIL="[email protected]" |
| 12 | release_re='[0-9]\+\.[0-9]\+\.[0-9]\+' |
| 13 | pre_re='\(\(a\|b\|rc\|alpha\|beta\)[0-9]*\)\?' |
| 14 | VERSION=$(echo $SOURCE_TAG | grep "^v${release_re}${pre_re}$") || VERSION="" |
| 15 | RELEASE_VERSION=$(echo $SOURCE_TAG | grep "^v${release_re}$") || RELEASE_VERSION="" |
| 16 | DEST_DIR="" |
| 17 | NEW_RELEASE="No" |
| 18 | |
| 19 | # dev, latest, stable, v1.0.0 |
| 20 | if [[ "$SOURCE_BRANCH" == "main" ]]; then |
| 21 | DEST_DIR="latest" |
| 22 | elif [[ "$SOURCE_BRANCH" == "dev" ]]; then |
| 23 | DEST_DIR="dev" |
| 24 | elif [[ "${SOURCE_BRANCH:0:11}" == "refs/tags/v" ]]; then |
| 25 | if [[ "$RELEASE_VERSION" ]]; then |
| 26 | DEST_DIR="$RELEASE_VERSION" |
| 27 | fi |
| 28 | fi |
| 29 | |
| 30 | # A release (tag) without a corresponding directory entry means we |
| 31 | # are seeing it for the first time. |
| 32 | if [[ "$DEST_DIR" == "$RELEASE_VERSION" ]] && [[ ! -d "$DEST_DIR" ]]; then |
| 33 | NEW_RELEASE="Yes" |
| 34 | fi |
| 35 | |
| 36 | # Do not deploy if destination directory has been created at this point. |
| 37 | # If the destination directory exists clear it out |
| 38 | # Otherwise we create it |
| 39 | if [[ -z "$DEST_DIR" ]]; then |
| 40 | echo "Nothing to deploy."; exit 0 |
| 41 | elif [[ -d "$DEST_DIR" ]]; then |
| 42 | rm -f "$DEST_DIR/*" |
| 43 | else |
| 44 | mkdir -p $DEST_DIR |
| 45 | fi |
| 46 | |
| 47 | # Copy documentation |
| 48 | touch .nojekyll |
| 49 | cp -a "$HTML_DIR/." $DEST_DIR |
| 50 | |
| 51 | # A new release becomes the stable version |
| 52 | # and also becomes the latest |
| 53 | if [[ "$NEW_RELEASE" == "Yes" ]]; then |
| 54 | rm stable |
| 55 | |
| 56 | ln -s "$DEST_DIR" stable |
| 57 | |
| 58 | rm -rf latest |
| 59 | ln -sf $DEST_DIR latest |
| 60 | |
| 61 | # For debugging |
| 62 | ls -la |
| 63 | fi |
| 64 | |
| 65 | COMMIT_MSG="Documentation: ${DEST_DIR}" |
| 66 | |
| 67 | # Configure commit information |
| 68 | git config user.name "$COMMIT_AUTHOR_NAME" |
| 69 | git config user.email "$COMMIT_AUTHOR_EMAIL" |
| 70 | |
| 71 | # Commit the "changes", i.e. the new version. |
| 72 | # The delta will show diffs between new and old versions. |
| 73 | git add "$DEST_DIR" |
| 74 | git add stable |
| 75 | git add latest |
| 76 | |
| 77 | changes=$(git diff --cached --shortstat) |
| 78 | if [[ -z "$changes" ]]; then |
| 79 | echo "No changes to the output on this push; exiting." |
| 80 | exit 0 |
| 81 | fi |
| 82 | |
| 83 | git commit -m "$COMMIT_MSG" |
| 84 | git push |