Skip to content

Instantly share code, notes, and snippets.

@gormus
Created October 19, 2021 00:24
Show Gist options
  • Save gormus/16257b152aca788ee0cd98a2d197f275 to your computer and use it in GitHub Desktop.
Save gormus/16257b152aca788ee0cd98a2d197f275 to your computer and use it in GitHub Desktop.
Drupal Deployment Script for Platform.sh Environments
#!/usr/bin/env bash
#
#
# Drupal Deployment Script for Platform.sh Environments
# ==============================================================================
# Authored by osman gormus <osman@gorm.us> on Oct 5, 2021
#
#
# deploy: |
# drush updatedb --no-post-updates --no-cache-clear --yes
# drush config:import --yes
# drush cache:rebuild --yes
#
# post_deploy: |
# drush updatedb --yes
#
#
# ------------------------------------------------------------------------------
# Database Status.
#
# The updatedb command may return updates from following types:
# - hook_update_n
# - entity-update
# - post-update
__updatedb_hook_update_n="hook_update_n"
__updatedb_entity_update="entity-update"
__updatedb_post_update="post-update"
__updatedb_status=$(drush updatedb:status --format=string --field=type)
#
# ------------------------------------------------------------------------------
# Configuration Status.
#
# If there are differences between the filesystem configuration and database
# configuration, the `__config_status` returns 1, and 0 otherwise.
drush config:status 2>&1 | grep "No differences"
# Returns 1 when the configuration is dirty, and should be imported;
# otherwise returned 0 for no differences.
__config_status=$?
#
# ------------------------------------------------------------------------------
# DEPLOY #
#
__need_cache_rebuilding=0
# Run `hook_update_n` updates.
if [[ "$__updatedb_status" =~ .*"$__updatedb_hook_update_n".* ]]; then
__need_cache_rebuilding=1
drush updatedb --no-post-updates --no-cache-clear --yes
fi
# Import config from config directory to database.
if [[ "$__config_status" = 1 ]]; then
__need_cache_rebuilding=1
drush config:import --yes
fi
# Run cache rebuilding only once, and only if needed.
if [[ "$__need_cache_rebuilding" = 1 ]]; then
drush cache:rebuild --yes
fi
# ------------------------------------------------------------------------------
# POST DEPLOY #
#
# Run `post-update` updates.
#
# If there is any pending post-updates, they shall be called in the
# "post_deploy" hook.
# The updatedb already clears caches upon completion. There's no need for
# calling it separately.
if [[ "$__updatedb_status" =~ .*"$__updatedb_post_update".* ]]; then
drush updatedb --yes
fi
# Flush "cache_render" keys from Redis cache.
#
# Note: Use non-blocking commands like SCAN and UNLINK, comparison to KEYS
# and DEL respectively.
redisFlushRenderCache() {
# Extract database credentials from Platform.sh relationships environment variable.
local __json=$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode | json_pp)
local __redis_host=$(echo $__json|php -r 'echo json_decode(fgets(STDIN))->redis[0]->host;')
local __redis_port=$(echo $__json|php -r 'echo json_decode(fgets(STDIN))->redis[0]->port;')
local __redis_cli="redis-cli -h ${__redis_host} -p ${__redis_port}"
$__redis_cli --scan --pattern '*:render:*' | xargs $__redis_cli UNLINK
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment