|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Rollback script for the Craft CMS deployment script on |
| 4 | +# staging/production servers. |
| 5 | +# @see https://github.com/elfacht/craft-deploy |
| 6 | +# |
| 7 | +# - Rollback to second newest release folder. |
| 8 | +# - This script Will not to any database work! |
| 9 | +# |
| 10 | +# @author |
| 11 | +# Martin Szymanski <martin@elfacht.com> |
| 12 | +# https://www.elfacht.com |
| 13 | +# https://github.com/elfacht |
| 14 | +# |
| 15 | +# @license MIT |
| 16 | + |
| 17 | +####################################### |
| 18 | +# Exit if any command fails |
| 19 | +####################################### |
| 20 | +set -e |
| 21 | + |
| 22 | +####################################### |
| 23 | +# Get constants from .env file: |
| 24 | +# - Server root path to project |
| 25 | +# - Restart PHP task (if symlinks are cached) |
| 26 | +####################################### |
| 27 | +read_var() { |
| 28 | + VAR=$(grep $1 $2 | xargs) |
| 29 | + IFS="=" read -ra VAR <<< "$VAR" |
| 30 | + echo ${VAR[1]} |
| 31 | +} |
| 32 | + |
| 33 | +ROOT_PATH=$(read_var DEPLOY_ROOT .env) |
| 34 | +RESTART_PHP=$(read_var DEPLOY_RESTART_PHP .env) |
| 35 | + |
| 36 | +####################################### |
| 37 | +# Get the current release folder |
| 38 | +####################################### |
| 39 | +CURRENT_RELEASE=$(ls -td $ROOT_PATH/releases/* | head -1 | tail -n 1) |
| 40 | + |
| 41 | +####################################### |
| 42 | +# Get the second newest folder |
| 43 | +####################################### |
| 44 | +LAST_STABLE=$(ls -td $ROOT_PATH/releases/* | head -2 | tail -n 1) |
| 45 | + |
| 46 | +####################################### |
| 47 | +# Symlink current release |
| 48 | +####################################### |
| 49 | +cd $ROOT_PATH |
| 50 | +printf -- "Symlink current release to $LAST_STABLE .." |
| 51 | +DONE=0; |
| 52 | +while [ $DONE -eq 0 ]; do |
| 53 | + ln -sfn $LAST_STABLE current |
| 54 | + |
| 55 | + if [ "$?" = "0" ]; then DONE=1; fi; |
| 56 | + printf -- '.'; |
| 57 | + sleep 1; |
| 58 | +done |
| 59 | +printf -- ' DONE!\n'; |
| 60 | + |
| 61 | +####################################### |
| 62 | +# Delete former release folder |
| 63 | +####################################### |
| 64 | +cd $ROOT_PATH |
| 65 | +printf -- "Delete former release folder $CURRENT_RELEASE .." |
| 66 | +DONE=0; |
| 67 | +while [ $DONE -eq 0 ]; do |
| 68 | + rm -rf $CURRENT_RELEASE |
| 69 | + |
| 70 | + if [ "$?" = "0" ]; then DONE=1; fi; |
| 71 | + printf -- '.'; |
| 72 | + sleep 1; |
| 73 | +done |
| 74 | +printf -- ' DONE!\n'; |
| 75 | + |
| 76 | +####################################### |
| 77 | +# Run composer and Craft scripts |
| 78 | +####################################### |
| 79 | +cd $ROOT_PATH/current |
| 80 | +if composer install --no-interaction --prefer-dist --optimize-autoloader; then |
| 81 | + php craft migrate/all |
| 82 | + php craft project-config/sync |
| 83 | + cd $ROOT_PATH |
| 84 | +fi |
| 85 | + |
| 86 | +####################################### |
| 87 | +# Restart PHP |
| 88 | +####################################### |
| 89 | +if ${RESTART_PHP}; then |
| 90 | + printf -- "- Restart PHP .." |
| 91 | + |
| 92 | + DONE=0; |
| 93 | + while [ $DONE -eq 0 ]; do |
| 94 | + ${RESTART_PHP} |
| 95 | + |
| 96 | + if [ "$?" = "0" ]; then DONE=1; fi; |
| 97 | + printf -- '.'; |
| 98 | + sleep 1; |
| 99 | + done |
| 100 | + |
| 101 | + printf -- ' DONE!\n'; |
| 102 | +fi |
0 commit comments