Skip to content

Instantly share code, notes, and snippets.

@martin-klima
Created February 1, 2020 10:12
Show Gist options
  • Save martin-klima/670105b3dab3f6fed98fc049d993c3ef to your computer and use it in GitHub Desktop.
Save martin-klima/670105b3dab3f6fed98fc049d993c3ef to your computer and use it in GitHub Desktop.
Drupal 8 automatic deployment git hook with changed config protection
#!/bin/bash
# Script for automatic Drupal 8 deployment with checking current site configuration state.
# If site has any unexported configuration changes, the deployment is not executed.
GIT_DIR="path_to_repo.git"
TARGET="path_to_destination_dir"
BRANCH="production"
while read oldrev newrev ref
do
# only checking out the specified branch you would like to deploy
if [[ $ref = refs/heads/$BRANCH ]]; then
# Check current config state.
cd ${TARGET}
CEX_EXPORT=$(drush cex --no |& grep identical)
# Continue only if site config has no uncommitted changes.
if [[ $CEX_EXPORT =~ "The active configuration is identical" ]]; then
# Deploy files.
echo "Ref $ref received. Deploying ${BRANCH} branch to ${TARGET}..."
git --git-dir=$GIT_DIR --work-tree=$TARGET checkout ${BRANCH} -f
# Deployment command block starts HERE...
cd ${TARGET}
composer install --no-dev
# cd ${TARGET}/web
drush cr
drush updb -y
drush cim -y
drush cr
# Deployment command block ends here.
echo "Finished."
else
echo "WARNING! Site configuration has been changed. Deployment is stopped due to protect the changes."
echo " Export and commit configuration first or reset it with 'drush cim' manually."
fi
else
echo "Ref $ref received. Only the ${BRANCH} branch may be deployed on this server."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment