Skip to content

Instantly share code, notes, and snippets.

@sheharyarn
Last active February 20, 2018 14:38
Show Gist options
  • Save sheharyarn/37a4d530f748ce65ffe560e41dbed4db to your computer and use it in GitHub Desktop.
Save sheharyarn/37a4d530f748ce65ffe560e41dbed4db to your computer and use it in GitHub Desktop.
Static Website Bash Deployment Script
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
#set -o xtrace
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)"
APP_NAME="my-app-name"
APP_DIR="${__root}/app/"
TMP_DIR="${__root}/tmp/"
SERVER_IP="xxx.xxx.xxx.xxx"
SERVER_USER="deploy"
SERVER_KEY="${HOME}/.ssh/${APP_NAME}.pem"
SERVER_PATH="/home/${SERVER_USER}/${APP_NAME}"
SERVER="${SERVER_USER}@${SERVER_IP}"
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`
GIT_HASH=`git log -n1 --pretty=format:'%h'`
TIMESTAMP=`date +%F-%H%M`
RELEASE_NAME="${APP_NAME}-${TIMESTAMP}-${GIT_BRANCH}-${GIT_HASH}"
RELEASE_CURRENT="${SERVER_PATH}/current"
RELEASE_PATH="${SERVER_PATH}/releases/${RELEASE_NAME}"
RELEASE_ARCHIVES="${SERVER_PATH}/archives/"
RELEASE_ARCHIVE="${RELEASE_ARCHIVES}${RELEASE_NAME}.tgz"
RELEASE_LOCAL="${TMP_DIR}${RELEASE_NAME}.tgz"
step() {
printf "\n\n\n$1\n\n"
}
printf "\n\n\n"
printf "STARTING DEPLOYMENT\n"
printf "===================\n"
step "1. Archiving application directory"
tar -zcvf ${RELEASE_LOCAL} -C ${APP_DIR} .
step "2. Uploading archive"
ssh -i ${SERVER_KEY} ${SERVER} <<- SSHCOMMANDS
mkdir -p ${RELEASE_ARCHIVES}
SSHCOMMANDS
scp -i ${SERVER_KEY} ${RELEASE_LOCAL} "${SERVER}:${RELEASE_ARCHIVE}"
step "3. Extracting archive and building new release"
ssh -i ${SERVER_KEY} ${SERVER} <<- SSHCOMMANDS
mkdir -p ${RELEASE_PATH}
tar -xvf ${RELEASE_ARCHIVE} -C ${RELEASE_PATH}
rm -rf ${RELEASE_CURRENT}
ln -s ${RELEASE_PATH} ${RELEASE_CURRENT}
SSHCOMMANDS
step "4. Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment