Created
October 21, 2020 18:49
-
-
Save Renrhaf/ceb2da90bb18d659e08679a21063b3a2 to your computer and use it in GitHub Desktop.
Classy shell script for building a Drupal 8 website
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
#!/usr/bin/env bash | |
# For references see the following links. | |
# Error handling : https://stackoverflow.com/a/50265513/1756667 | |
# Shebang : https://stackoverflow.com/a/10383546/1756667 | |
info_message () { | |
printf "%s\n" "$1" 2>&1 | tee -a $LOG | |
} | |
step_message () { | |
printf "\n\e[42m\e[1m%s\e[0m\n" "$1" 2>&1 | tee -a $LOG | |
} | |
end_message () { | |
step_message "-------------------------------" | |
printf "\e[42m\e[5m โ ๏ธ๐ Deployment finished ๐โ๏ธ \e[0m" | |
step_message "-------------------------------" | |
notify-send --urgency=CRITICAL "Deployment finished" | |
} | |
exec_command () { | |
local command="$1" | |
local error=$2 | |
# Execute command and redirect all output to this log file | |
printf '\e[1m>> %s\e[0m\n' "${command}" 2>&1 | tee -a $LOG | |
if [[ $command == *"import_dump"* ]]; then | |
$command | |
else | |
$command 2>&1 | tee -a $LOG | |
fi | |
local exit_code=${PIPESTATUS[0]} | |
# Log errors | |
[[ $exit_code ]] && # do nothing if no error code passed | |
((exit_code != 0)) && { # do nothing if error code is 0 | |
[[ $error ]] && printf '\e[41m\e[1m\n ERROR: %s\e[49m' "$error" 2>&1 | tee -a $LOG | |
printf '\e[5m\e[41m\e[1m\n DEPLOYMENT FAILED \e[0m\n' 2>&1 | tee -a $LOG | |
notify-send --urgency=CRITICAL "Deployment error" | |
exit "$exit_code" # we could also check to make sure | |
# error code is numeric when passed | |
} | |
} | |
import_dump() { | |
local dump="$1" | |
pv ${dump} | ${DRUSH} sql-cli | |
} | |
# ================= # | |
# Prepare variables # | |
# ================= # | |
PROJET="project_name" | |
PWD=/path/to/base | |
DRUSH=${PWD}/${PROJET}/vendor/drush/drush/drush | |
DATABASE_DUMP=${PWD}/${PROJET}/$1 | |
d=$(date +%y-%m-%d) | |
MONTH=$(date +%B) | |
YEAR=$(date +%Y) | |
MYBASEDIR=/var/log/${PROJET} | |
MYBACKUPDIR=${MYBASEDIR}/${YEAR}/${MONTH} | |
mkdir -p ${MYBACKUPDIR} | |
LOG="${MYBACKUPDIR}/deploy-$d.log" | |
[ -e $LOG ] && rm $LOG | |
touch -am $LOG | |
# ================================== # | |
# Print variables for debug purposes # | |
# ================================== # | |
info_message "-------------------------------------" | |
info_message "DEPLOYMENT DATE : "$(date +%Y-%m-%d-%T) | |
info_message "-------------------------------------" | |
info_message "USER=$(whoami)" | |
info_message "LOGS=$LOG" | |
# ====================== # | |
# Set Drupal permissions # | |
# ====================== # | |
step_message "Preparing Drupal folders and permissions..." | |
exec_command "cd ${PWD}/${PROJET}" | |
exec_command "chmod -R ug+rw ${PWD}/${PROJET}/config/sync" | |
exec_command "chmod -R g+w ${PWD}/${PROJET}/web/sites/default/files" | |
exec_command "chmod u+x ${PWD}/${PROJET}/vendor/drush/drush/drush" | |
# ========================== # | |
# Deployment script beneath. # | |
# ========================== # | |
step_message "Deployment start..." | |
exec_command "composer install --optimize-autoloader" "Error while installing dependencies." | |
# Check that database dump exists. | |
if [ -f "$DATABASE_DUMP" ]; then | |
step_message "Database import." | |
exec_command "${DRUSH} sql-drop -y" "Error while dropping the database." | |
exec_command "import_dump ${DATABASE_DUMP}" "Error while importing the database : ${DATABASE_DUMP}." | |
step_message "Database import finished." | |
fi | |
exec_command "${DRUSH} cache-rebuild" "First cache clear issue." | |
exec_command "${DRUSH} sset system.maintenance_mode 1 " "Impossible to set maintenance mode." | |
exec_command "${DRUSH} updb -y" "Database update hooks failed." | |
step_message "Database and entity updates ran and cache rebuild done." | |
exec_command "${DRUSH} pipeline:run-step pre-config-import -y" "Error while running pre-config-import pipelines." | |
exec_command "${DRUSH} cache-rebuild" "Second cache clear issue." | |
step_message "Pipeline ran for pre-config-import step." | |
exec_command "${DRUSH} cim sync -y" "Error while importing configurations." | |
step_message "Configuration imported from config/sync." | |
exec_command "${DRUSH} pipeline:run-step post-config-import -y" "Error while running post-config-import pipelines." | |
step_message "Pipeline ran for post-config-import step." | |
exec_command "${DRUSH} vdm-translation:clean -y" "Error while cleaning translations." | |
exec_command "${DRUSH} language-import --langcode=fr" "Error while importing translations." | |
step_message "Translations cleaned and synchronized." | |
exec_command "${DRUSH} vdm-ct:sync" "Error while synchronizing continuation token." | |
step_message "Continuation token data synchronized." | |
exec_command "${DRUSH} sset system.maintenance_mode 0" "Impossible to exit maintenance mode." | |
exec_command "${DRUSH} cache-rebuild" "Last cache clear issue." | |
step_message "Last cache rebuild done after updating and importing the config done." | |
exec_command "${DRUSH} vdm-account:add-test-users -y --env=local" "Error creating test user accounts." | |
exec_command "${DRUSH} vdm-content:import --env=local" "Error creating test contents." | |
end_message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment