Created
June 16, 2019 17:18
-
-
Save yuri1969/fd3273bf9b18286e131579e8edb4abda to your computer and use it in GitHub Desktop.
Deploy
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
# Deployment with zero downtime | |
# By default keeps 2 last deployments in KEEP_DEPLOYMENTS_DIR and current deployment | |
BUILD_ID="$1" | |
ROOT_DIR="$(pwd)" | |
# Project domain | |
PROJECT_NAME="test.com" | |
# Project directory | |
TARGET_DIR="${ROOT_DIR}/${PROJECT_NAME}" | |
# Deployments directory | |
KEEP_DEPLOYMENTS_DIR="${ROOT_DIR}/deploy" | |
KEEP_DEPLOYMENTS=2 | |
CURRENT_DEPLOY_DIR="${BUILD_ID}_$(date +'%d%m%Y_%H%M%S')" | |
DEPLOY_DIR_PROJECT=${KEEP_DEPLOYMENTS_DIR}/${PROJECT_NAME} | |
DEPLOY_DIR=${DEPLOY_DIR_PROJECT}/${CURRENT_DEPLOY_DIR} | |
echo "Initialize deployment directory '"${DEPLOY_DIR}"'" | |
[ -d "${DEPLOY_DIR}" ] || mkdir -p "${DEPLOY_DIR}" | |
cd "${DEPLOY_DIR}" | |
touch foo | |
echo "Copying '"${TARGET_DIR}/"' to '"${DEPLOY_DIR}"'" | |
rsync -a "${TARGET_DIR}/" "${DEPLOY_DIR}" | |
echo "Execute commands in deployment directory" | |
cd "${DEPLOY_DIR}" | |
# Atomic, zero downtime | |
echo "Update symlink '"${DEPLOY_DIR}"' to '"${TARGET_DIR}.tmp"'" | |
ln -s "${DEPLOY_DIR}" "${TARGET_DIR}.tmp" | |
# Remove current project directory if not symlink | |
if [ ! -h "${TARGET_DIR}" ]; then | |
rm -rf "${TARGET_DIR}" | |
fi | |
echo "Update symlink '"${DEPLOY_DIR}.tmp"' to '"${TARGET_DIR}"'" | |
mv -Tf "${TARGET_DIR}.tmp" "${TARGET_DIR}" | |
echo "Clear old deployments in '"${DEPLOY_DIR_PROJECT}" keep last '"${KEEP_DEPLOYMENTS}"'" | |
cd "${DEPLOY_DIR_PROJECT}" | |
rm -rf "$(ls -t ${DEPLOY_DIR_PROJECT} | grep -v ${CURRENT_DEPLOY_DIR} | tail -n +$((KEEP_DEPLOYMENTS+1)))" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment