Skip to content

Instantly share code, notes, and snippets.

@sodacrackers
Created January 3, 2020 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sodacrackers/d81e04ec7573251ce0e60ede9643246e to your computer and use it in GitHub Desktop.
Save sodacrackers/d81e04ec7573251ce0e60ede9643246e to your computer and use it in GitHub Desktop.
Git post commit deploy
#!/bin/sh
# set -x
# set -v
#
#
# This script deploys your code to the production/ LIVE site.
#
# To make use of this git hook, it can be copied to `.git/post-commit`
# in the folder root. It's then triggered after each git commit.
# Git offers hooks on many different git activities and, this custom
# hook checks if we are on the "master" or "develop" branch and, if so
# and the user responds "Yes" to the prompt, will copy the current code
# to a git "current-deploy-release" branch.
#
# Note that our MediaTemple instance listens for commits to that branch.
# After MediaTemple receives a update notification, it's able to download
# latest code, run configuration import updates, clear caches and run cron.
#
# No files need to be rearranged to run the hook successfully.
# It's important your current directory has vendor files and a working
# drupal instance.
#
#
# How to get webhook URL for remote Git repository in Plesk
# @see https://support.plesk.com/hc/en-us/articles/115004356274-How-to-configure-webhook-for-remote-Git-repository-in-Plesk
#
# GitHub - Webhooks | GitHub Developer Guide
# @see https://developer.github.com/webhooks/
#
# Git - Customizing Git - Git Hooks
# https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
#
TIME=`date +%s`
RESEVOIR='git@github.com:uwmweb/uwmcms-reservoir-db.git'
GIT_CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ "$GIT_CURRENT_BRANCH" != "master" && "$GIT_CURRENT_BRANCH" != "develop" ]]; then
echo ''
echo 'UWM: Starting project .git/hooks/post-commit'
echo 'UWM: Not on "develop" or "master" so exiting early.'
echo ''
exit 1
fi
echo ''
echo ''
echo 'UWM: Starting project .git/hooks/post-commit'
echo ''
echo 'UWM: About to prompt you (the current user)'
echo 'UWM: Would you like the configurations (yml and database) '
echo 'UWM: Currently on the LIVE/ PRODUCTION site replaced with '
echo 'UWM: What is currently in your working folder. '
echo ''
echo ''
exec < /dev/tty
while true; do
read -p "UWM: Make this deploy branch? Does it include all ./vendor/ files?? (y/n) " answer
if [[ $answer =~ ^[Yy]$ ]];
then
echo
echo 'UWM: Preparing to deploy to production. '
echo 'UWM: What is the name of your deployment? For example "plesk-develop-deployment" '
read -p 'or "plesk-prod-deployment"? ' GIT_DEPLOY_BRANCH
GIT_DEPLOY_TAG="$GIT_DEPLOY_BRANCH-$TIME"
if [[ "$GIT_DEPLOY_BRANCH" == *"deploy"* ]];
then
if [[ "$GIT_DEPLOY_BRANCH" == *"prod"* && "$GIT_CURRENT_BRANCH" != "master" ]];
then
echo
echo 'UWM: Creating a "production" deploy branch commit (is there a webhook listening) '
echo 'but, not from master. Exiting early.'
echo
exit 1
fi
if [[ "$GIT_DEPLOY_BRANCH" == *"develop"* && "$GIT_CURRENT_BRANCH" != "develop" ]];
then
echo
echo 'UWM: Creating a "stage" deploy branch commit (is there a webhook listening) '
echo 'but, not from develop. Exiting early.'
echo
exit 1
fi
echo
echo
echo $GIT_DEPLOY_BRANCH
echo $GIT_DEPLOY_TAG
echo
echo 'UWM: Deleting previous deploy branch. Some error messages are normal.'
echo
# We are not tracking history for these deploys. Let's add a tag called "deploy" and
# having the current date. Since git offers more options and management options for
# branches, let's create a "deploy" branch too.
git tag $GIT_DEPLOY_TAG
git push origin $GIT_DEPLOY_TAG
git push --delete origin $GIT_DEPLOY_BRANCH
git branch -D $GIT_DEPLOY_BRANCH
git checkout -b $GIT_DEPLOY_BRANCH
git push --set-upstream origin $GIT_DEPLOY_BRANCH
# We want to add all files to the branch, so it can be checked out on the
# production server as a working site. Remove any git ignore entries and
# add all project root files.
find . -type f -name '.gitignore' -exec rm -f {} \;
find docroot -type d -name '.git' -exec rm -rf {} \;
echo '' > .gitignore
echo 'private_files' >> .gitignore
echo 'private_files/*' >> .gitignore
echo 'docroot/sites/default/files/*' >> .gitignore
echo 'docroot/sites/default/default.settings.php' >> .gitignore
rm docroot/sites/default/default.settings.php >> .gitignore
# Add the tag name so it can be verified on production.
echo $GIT_DEPLOY_TAG > docroot/uwm_version
git add .
git commit -m'UWM: The project .git/post-commit created this branch.'
git push --set-upstream origin $GIT_DEPLOY_BRANCH
git checkout $GIT_CURRENT_BRANCH
git checkout -- .gitignore
echo
echo
echo
echo
echo 'UWM: Added tag.....'
echo $GIT_DEPLOY_TAG
echo
echo
fi
else
echo
echo 'UWM: Exiting early.'
echo
exit 1
fi
break
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment