Skip to content

Instantly share code, notes, and snippets.

@tacone
Created April 10, 2024 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tacone/230d5c305a9c5eff7f58ea2744f2028c to your computer and use it in GitHub Desktop.
Save tacone/230d5c305a9c5eff7f58ea2744f2028c to your computer and use it in GitHub Desktop.
deploy
#!/bin/bash
# ------------------------------------------------------------------
# This script deploys the application to production
#
# example:
#
# scripts/deploy
# ------------------------------------------------------------------
set -e
trap 'catch_err' ERR
catch_err() { echo -e "\n[ERROR] Deployment failed"; exit 1; }
err() { local ERR=$?; echo "$@"; return 1; }
git_switch_branch() {
echo -n " && echo -e '\n-- switching to branch: ""$1""\n'"
echo -n " && git checkout -b $1 origin/$1"
echo -n " || git switch $1"
}
git_get_current_branch() {
echo "git branch --show-current 2>/dev/null || git symbolic-ref --short HEAD"
}
SCRIPTS=$(realpath $(dirname "$0"))
ROOT=$(realpath $SCRIPTS/..)
cd $ROOT && source $ROOT/.env
# check DEPLOY_SSH and DEPLOY_PATH
[ "$DEPLOY_SSH" ] || err "DEPLOY_SSH variable is not set"
[ "$DEPLOY_PATH" ] || err "DEPLOY_PATH variable is not set"
start_time=$(date +%s) # save the start time
echo -e "\nDeploying the code"
echo -e "\n-- connecting\n"
DEPLOY_SCRIPT=""
DEPLOY_SCRIPT+=" cd $DEPLOY_PATH"
[ $DEPLOY_BRANCH ] && DEPLOY_SCRIPT="$DEPLOY_SCRIPT $(git_switch_branch $DEPLOY_BRANCH)"
DEPLOY_SCRIPT+=' && echo -e "\n-- current branch: $('$(git_get_current_branch)')\n"'
DEPLOY_SCRIPT+=' && echo -e "-- pulling the code.."'
DEPLOY_SCRIPT+=' && git pull'
DEPLOY_SCRIPT+=' && echo -e "\n-- building docker images..\n"'
DEPLOY_SCRIPT+=' && ./scripts/live --parallel 4 build'
DEPLOY_SCRIPT+=' && echo -e "\n\n------------------- Restarting Containers -------------------\n"'
DEPLOY_SCRIPT+=' && echo -e "Do NOT interrupt the script now.\n"'
DEPLOY_SCRIPT+=' && echo -e "Interruping will result in a broken application. Wait until"'
DEPLOY_SCRIPT+=' && echo -e "you see the logs. \n"'
DEPLOY_SCRIPT+=' && echo -e "-------------------------------------------------------------\n"'
DEPLOY_SCRIPT+=' && ./scripts/live up -d --force-recreate'
DEPLOY_SCRIPT+=' && echo -e "\n-- build completed, containers restarted\n"'
$DEPLOY_SSH $DEPLOY_SCRIPT
end_time=$(date +%s)
execution_time=$((end_time - start_time))
echo ""
echo "---------------- Deployment finished -----------------"
echo
echo " Application deployed in: $execution_time seconds"
echo
echo "------------------------------------------------------"
echo ""
$DEPLOY_SSH "cd $DEPLOY_PATH && ./scripts/live logs -n100 -f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment