-
-
Save cvladan/05e2b2c13e35cccc7929d6800f3ab319 to your computer and use it in GitHub Desktop.
Script used to deploy my personal notes repository, placed in `/opt/webhook/deploy-treasury.sh`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# Note the '-e' in the line above. This is required for error trapping implemented below. | |
# required when inside <webhook> service as $HOME variable is not set | |
if [ -z "${HOME:-}" ]; then export HOME="$(cd ~ && pwd)"; fi | |
# hugo bin | |
HUGO_BIN="/home/linuxbrew/.linuxbrew/bin/hugo" | |
# Repo name on GitHub | |
REMOTE_REPO="git@github.com:user/repo.git" | |
REMOTE_REPO_SSH_KEY="$HOME/.ssh/id_rsa.github.repo" | |
REMOTE_SUBMODULE_SSH_KEY="$HOME/.ssh/id_rsa.github.submodule" | |
# Domain name so Hugo can generate links correctly | |
MY_DOMAIN="example.com" | |
# Location (server block) where Nginx looks for content to serve | |
PUBLIC_WWW="/var/www/example/web" | |
# A place to clone the remote repo so Hugo can build from it | |
WORKING_DIRECTORY="$HOME/hugo-staging_area" | |
# Backup folder in case something goes wrong during this script | |
BACKUP_WWW="$HOME/hugo-backup_html" | |
# For future notifications on Telegram (payload from GitHub) | |
commit_message="$1" | |
pusher_name="$2" | |
commit_id="$3" | |
# If something goes wrong, put the previous version back in place | |
function cleanup { | |
echo "A problem occurred. Reverting to backup." | |
rsync -aqz --del "$BACKUP_WWW/" "$PUBLIC_WWW" | |
rm -rf "$WORKING_DIRECTORY" | |
# !!Placeholder for Telegram notification | |
} | |
# Call the cleanup function if this script exits abnormally. The -e flag | |
# in the shebang line ensures an immediate abnormal exit on any error | |
trap cleanup EXIT | |
# Clear out the working directory | |
# rm -rf $WORKING_DIRECTORY | |
# Make a backup copy of current website version | |
rsync -aqz "$PUBLIC_WWW/" "$BACKUP_WWW" | |
# Pull from GitHub | |
# | |
if [[ ! -d "$WORKING_DIRECTORY" ]]; then | |
export GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i $REMOTE_REPO_SSH_KEY -F /dev/null" | |
git clone "$REMOTE_REPO" "$WORKING_DIRECTORY" | |
cd "$WORKING_DIRECTORY" | |
export GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i $REMOTE_SUBMODULE_SSH_KEY -F /dev/null" | |
git submodule update --init | |
else | |
cd "$WORKING_DIRECTORY" | |
export GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i $REMOTE_REPO_SSH_KEY -F /dev/null" | |
git pull | |
fi | |
export GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i $REMOTE_SUBMODULE_SSH_KEY -F /dev/null" | |
git submodule update --remote | |
# Placeholder for future Telegram notification | |
# Delete old version; Be careful as double quotes prevent the wildcard expansion | |
rm -rf "$PUBLIC_WWW/"* | |
# Have Hugo generate the new static HTML directly into the public WWW folder | |
$HUGO_BIN -s "$WORKING_DIRECTORY" -d "$PUBLIC_WWW" -b "https://${MY_DOMAIN}" | |
# !!Placeholder for Telegram notification | |
# Clear out working directory | |
# rm -rf "$WORKING_DIRECTORY" | |
# Exit without trapping, since everything went well | |
# | |
echo "* Updated repos as '$(whoami)' in '$PWD'" | |
# touch "$HOME/hugo_deployed_$(date +"%Y-%m-%d_%H-%M-%S").done" | |
trap - EXIT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment