99 lines
3.9 KiB
Bash
Executable File
99 lines
3.9 KiB
Bash
Executable File
#!/bin/bash
|
|
# https://chromatichq.com/blog/drupal-8-deployments-jenkins-github-slack
|
|
|
|
CYAN='\033[1;36m'
|
|
GREEN='\033[1;32m'
|
|
RESET='\033[0m'
|
|
|
|
step() {
|
|
echo -e "\n${CYAN}==================================================${RESET}"
|
|
echo -e "${CYAN} $1${RESET}"
|
|
echo -e "${CYAN}==================================================${RESET}\n"
|
|
}
|
|
|
|
step "Updating Drupal 8"
|
|
echo "Switching to project docroot."
|
|
cd ./public_html
|
|
|
|
# Snapshot of the lock as currently deployed, taken BEFORE the pull.
|
|
# Used below to detect modules removed from the lock by this deploy.
|
|
git show HEAD:composer.lock > /tmp/deploy-lock-old 2>/dev/null || echo "WARN: no previous composer.lock found"
|
|
|
|
step "Pulling down latest code"
|
|
git pull --ff-only origin prod
|
|
git submodule update --init --recursive
|
|
cp composer.lock /tmp/deploy-lock-new
|
|
|
|
step "Clearing drush caches"
|
|
vendor/bin/drush cache-clear drush --ansi
|
|
|
|
step "Uninstalling extensions removed from the lock"
|
|
# Must run BEFORE composer install: the obsolete code is still on disk,
|
|
# so drush can uninstall cleanly. Without this, an extension still enabled
|
|
# in the DB whose package disappears from the lock crashes the bootstrap.
|
|
if [ -s /tmp/deploy-lock-old ] && [ -s /tmp/deploy-lock-new ]; then
|
|
grep -o '"name": "drupal/[^"]*"' /tmp/deploy-lock-old | sed 's/.*"drupal\///;s/"//' | sort -u > /tmp/deploy-pkgs-old
|
|
grep -o '"name": "drupal/[^"]*"' /tmp/deploy-lock-new | sed 's/.*"drupal\///;s/"//' | sort -u > /tmp/deploy-pkgs-new
|
|
ENABLED_MODS=$(vendor/bin/drush php:eval 'echo implode(" ", array_keys(\Drupal::config("core.extension")->get("module")));' 2>/dev/null)
|
|
ENABLED_THEMES=$(vendor/bin/drush php:eval 'echo implode(" ", array_keys(\Drupal::config("core.extension")->get("theme")));' 2>/dev/null)
|
|
TO_UNINSTALL_MODS=""
|
|
TO_UNINSTALL_THEMES=""
|
|
for pkg in $(comm -23 /tmp/deploy-pkgs-old /tmp/deploy-pkgs-new); do
|
|
for dir in web/modules/contrib/$pkg web/themes/contrib/$pkg; do
|
|
[ -d "$dir" ] || continue
|
|
case "$dir" in
|
|
web/modules/*)
|
|
for mod in $ENABLED_MODS; do
|
|
find "$dir" -name "$mod.info.yml" 2>/dev/null | grep -q . && TO_UNINSTALL_MODS="$TO_UNINSTALL_MODS $mod"
|
|
done
|
|
;;
|
|
web/themes/*)
|
|
for th in $ENABLED_THEMES; do
|
|
find "$dir" -name "$th.info.yml" 2>/dev/null | grep -q . && TO_UNINSTALL_THEMES="$TO_UNINSTALL_THEMES $th"
|
|
done
|
|
;;
|
|
esac
|
|
done
|
|
done
|
|
if [ -n "$TO_UNINSTALL_MODS" ]; then
|
|
TO_UNINSTALL_MODS=$(echo $TO_UNINSTALL_MODS | tr ' ' '\n' | sort -u | tr '\n' ' ')
|
|
echo "pm:uninstall$TO_UNINSTALL_MODS"
|
|
vendor/bin/drush pmu $TO_UNINSTALL_MODS -y --ansi || echo "WARN: module uninstall incomplete"
|
|
fi
|
|
if [ -n "$TO_UNINSTALL_THEMES" ]; then
|
|
TO_UNINSTALL_THEMES=$(echo $TO_UNINSTALL_THEMES | tr ' ' '\n' | sort -u | tr '\n' ' ')
|
|
echo "theme:uninstall$TO_UNINSTALL_THEMES"
|
|
vendor/bin/drush theme:uninstall $TO_UNINSTALL_THEMES -y --ansi || echo "WARN: theme uninstall incomplete"
|
|
fi
|
|
else
|
|
echo "Lock diff unavailable, skipping obsolete extension detection."
|
|
fi
|
|
|
|
step "Composer install"
|
|
composer install --no-dev -n --ansi
|
|
|
|
step "Running database updates"
|
|
vendor/bin/drush updb -y --ansi
|
|
|
|
step "Importing configuration"
|
|
vendor/bin/drush config-import -y --ansi
|
|
#drush csim prod -y
|
|
|
|
step "Importing content and blocks (structure_sync)"
|
|
# Only when structure_sync is enabled on this environment (config_split aware).
|
|
if vendor/bin/drush php:eval 'echo \Drupal::moduleHandler()->moduleExists("structure_sync") ? "yes" : "no";' 2>/dev/null | grep -q "yes"; then
|
|
vendor/bin/drush im --choice safe --ansi
|
|
vendor/bin/drush ib --choice full --ansi
|
|
else
|
|
echo "structure_sync is not enabled on this environment, skipping im/ib."
|
|
fi
|
|
|
|
step "Regen path aliases"
|
|
#drush pag all canonical_entities:node
|
|
#drush pag all canonical_entities:taxonomy_term
|
|
|
|
step "Clearing caches"
|
|
vendor/bin/drush cr --ansi
|
|
|
|
echo -e "\n${GREEN}Deployment complete.${RESET}\n"
|