Skip to content

Instantly share code, notes, and snippets.

@actionm
Created February 28, 2017 19:01
Show Gist options
  • Save actionm/63b927a9b933e54414ae2857e5712162 to your computer and use it in GitHub Desktop.
Save actionm/63b927a9b933e54414ae2857e5712162 to your computer and use it in GitHub Desktop.
Zero downtime deployment script
# Deployment with zero downtime
# By default keeps 2 last deployments in KEEP_DEPLOYMENTS_DIR and current deployment
# Project domain
PROJECT_NAME=test.com
# Project directory
PROJECT_DIR=/home/forge/test.com
# Deployments directory
KEEP_DEPLOYMENTS_DIR=/home/forge/deploy
KEEP_DEPLOYMENTS=2
DEPLOY_DIR_NAME=$(date +'%d%m%Y_%H%M%S')
DEPLOY_DIR_PROJECT=${KEEP_DEPLOYMENTS_DIR}/${PROJECT_NAME}
DEPLOY_DIR=${DEPLOY_DIR_PROJECT}/${DEPLOY_DIR_NAME}
echo "Initialize deployment directory '"${DEPLOY_DIR}"'"
[ -d ${DEPLOY_DIR} ] || mkdir -p ${DEPLOY_DIR}
echo "Copying '"${PROJECT_DIR}/"' to '"${DEPLOY_DIR}"'"
rsync -a $PROJECT_DIR/ $DEPLOY_DIR
echo "Execute commands in deployment directory"
cd ${DEPLOY_DIR}
git pull origin master
composer install --no-interaction --prefer-dist --optimize-autoloader
if [ -f artisan ]
then
php artisan storage:link
php artisan migrate --force
php artisan queue:restart
fi
# Atomic, zero downtime
echo "Update symlink '"${DEPLOY_DIR}"' to '"${PROJECT_DIR}.tmp"'"
ln -s $DEPLOY_DIR ${PROJECT_DIR}.tmp
# Remove current project directory if not symlink
if [ ! -h $PROJECT_DIR ]; then
rm -rf $PROJECT_DIR
fi
echo "Update symlink '"${DEPLOY_DIR}.tmp"' to '"${PROJECT_DIR}"'"
mv -Tf $PROJECT_DIR.tmp $PROJECT_DIR
echo "Clear old deployments in '"${DEPLOY_DIR_PROJECT}" keep last '"${KEEP_DEPLOYMENTS}"'"
cd ${DEPLOY_DIR_PROJECT}
rm -rf $(ls ${DEPLOY_DIR_PROJECT} -t | grep -v ${DEPLOY_DIR_NAME} | tail -n +$((KEEP_DEPLOYMENTS+1)))
@lam0819
Copy link

lam0819 commented Sep 12, 2022

should be tr
since we want to delete oldest one
rm -rf $(ls ${DEPLOY_DIR_PROJECT} -tr | grep -v ${DEPLOY_DIR_NAME} | tail -n +$((KEEP_DEPLOYMENTS+1)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment