Skip to content

Instantly share code, notes, and snippets.

@jameswilson
Created December 4, 2024 23:34
Show Gist options
  • Save jameswilson/1a8e21f8afda86fbbcfd474e7c717c69 to your computer and use it in GitHub Desktop.
Save jameswilson/1a8e21f8afda86fbbcfd474e7c717c69 to your computer and use it in GitHub Desktop.
Make platform.sh Deploy hook idempotent
# This is an adaptation and simplication from the Platform.sh Drupal 10 templates:
# https://github.com/platformsh-templates/drupal10/blob/master/.platform.app.yaml
# https://github.com/platformsh-templates/drupal10/blob/master/drush/platformsh_deploy_drupal.sh
mounts:
# Create a persistent filesystem location to house the PREVIOUS_PLATFORM_TREE_ID
# value in a file that must be available for each build.
'/build_info':
source: local
source_path: 'build_info'
hooks:
# Make the deploy hook idempotent. Only run `config:import` if we're certain the
# deployment is part of a code release. Note: this hook can run even when code
# changes are not deployed, despite being documented otherwise in Platform Docs,
# leading to production data loss if configurations are changed manually via
# Drupal's UI before it has had a chance to be captured and exported to code.
deploy: |
set -e
cd /app/web
logger "[deploy]: Code deployed to $PLATFORM_BRANCH."
logger "[deploy]: Updating database."
drush -y updatedb --uri=platform.sh
PREVIOUS_PLATFORM_TREE_ID=$(</app/build_info/PREVIOUS_PLATFORM_TREE_ID)
logger "[deploy]: Previous ID: $PREVIOUS_PLATFORM_TREE_ID"
logger "[deploy]: Current ID: $PLATFORM_TREE_ID"
if [ "$PLATFORM_TREE_ID" != "$PREVIOUS_PLATFORM_TREE_ID" ]; then
logger "[deploy]: Code changes detected. Importing configuration."
drush -y config:import --uri=platform.sh
else
logger "[deploy]: No code changes detected. Skipping config import to prevent data loss."
drush config:status
drush config:export --no --diff || true
fi
logger "[deploy]: Clearing cache."
drush cache:rebuild --uri=platform.sh
echo "$PLATFORM_TREE_ID" > /app/build_info/PREVIOUS_PLATFORM_TREE_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment