Skip to content

Instantly share code, notes, and snippets.

@ryansechrest
Last active February 12, 2017 12:41
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ryansechrest/8011725 to your computer and use it in GitHub Desktop.
Save ryansechrest/8011725 to your computer and use it in GitHub Desktop.
Git post-receive hook to deploy WordPress and plugins as submodules. It can also install Node.js modules with npm and vendor packages with Composer.
#!/bin/bash
# Created on 7/17/13 by Ryan Sechrest
# Deploys pushed branch from the origin repository to the web directory
if [[ (-n $1) && (-n $2) && (-n $3) ]]; then
# Set path to project directory
project_path="/var/www/domains/$2/$3"
# Set web directory name
web_dir="htdocs"
# Set path to web directory
web_path="$project_path/$web_dir"
# For each branch that was pushed
while read oldrev newrev refname
do
# Get branch name
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
echo "> Received $branch branch"
# Assume no target branch pushed
target_branch=false
# Determine whether current branch is target branch for environment
if [[ "$FH_ENVIRONMENT" == "LOCALHOST" ]]; then
echo '> Determined environment is LOCALHOST'
if [[ "$branch" == "develop" || "$branch" == "feature/"* ]]; then
target_branch=true
echo "> Determined $branch branch is target branch"
fi
elif [[ "$FH_ENVIRONMENT" == "DEVELOPMENT" ]]; then
echo '> Determined environment is DEVELOPMENT'
if [[ "$branch" == "develop" || "$branch" == "release/"* ]]; then
target_branch=true
echo "> Determined $branch branch is target branch"
fi
elif [[ "$FH_ENVIRONMENT" == "STAGING" ]]; then
echo "> Determined environment is STAGING"
if [[ "$branch" == "master" || "$branch" == "hotfix/"* ]]; then
target_branch=true
echo "> Determined $branch branch is target branch"
fi
else
echo "> Determined environment is PRODUCTION"
if [[ "$branch" == "master" ]]; then
target_branch=true
echo "> Determined $branch branch is target branch"
fi
fi
# If branch is target branch
if $target_branch ; then
echo "> Processing $branch branch"
# Unset global GIT_DIR so script can leave repository
unset GIT_DIR
echo "> Unset GIT_DIR"
# Move into web directory
cd $web_path
echo "> Moved into $web_path directory"
# If web directory is empty
if find . -maxdepth 0 -empty | read; then
echo "> Determined web directory is empty"
# Clone branch from origin repository into web directory
git clone /opt/repositories/$1.git -b $branch .
echo "> Cloned origin/$branch branch from $1.git repository into web directory"
# Create empty file to remember created date
touch $web_path/.created
echo "> Created .created file in web directory"
# If web directory is not empty
else
echo "> Determined web directory contains files"
# Get HEAD of working directory
current_branch=$(git rev-parse --abbrev-ref HEAD)
echo "> Determined working directory is on $current_branch branch"
# If branch matches HEAD of working directory
if [ "$branch" == "$current_branch" ]; then
echo "> Determined updates affect current branch"
# Fetch and merge changes into web directory
git pull origin $branch
echo "> Pulled origin/$branch into $branch branch in web directory"
# If branch does not match HEAD of working directory
else
echo "> Determined updates belong to new branch"
# Fetch changes from origin
git fetch origin
echo "> Fetched changes from origin"
# Checkout new branch
git checkout $branch
echo "> Checked out $branch branch in web directory"
fi
# Create or update empty file to remember last updated date
touch $web_path/.updated
echo "> Updated .updated file in web directory"
fi
# Fetch commits and tags for each submodule
git submodule foreach git fetch --tags
echo "> Fetched commits and tags for each submodule"
# Update all submodules
git submodule update --init --recursive
echo "> Initialized and updated all submodules"
# If Node.js package file exists in web directory
if [ -f "$web_path/nodejs/package.json" ]; then
echo "> Determined $web_dir/nodejs/package.json file exists"
# If Node.js directory does not exist in project directory
if [ ! -d "$project_path/nodejs" ]; then
echo "> Determined $project_path/nodejs directory does not exist"
# Create Node.js directory in project directory
mkdir $project_path/nodejs
echo "> Created $project_path/nodejs directory"
fi
# Copy package.json from web to project directory
cp $web_path/nodejs/package.json $project_path/nodejs/package.json
echo "> Copied package.js from web to project directory"
# Move into Node.js directory in project directory
cd $project_path/nodejs
echo "> Moved into $project_path/nodejs directory"
# Install Node.js modules
npm install $project_path/nodejs
echo "> Installed Node.js modules in $project_path/nodejs directory"
# Move into web directory
cd $web_path
echo "> Moved into $web_path directory"
fi
# If website is WordPress powered
if [ -d "$web_path/wordpress" ]; then
echo "> Determined website runs WordPress"
# If nodejs/product directory exists
if [ -d "$web_path/nodejs/product" ]; then
echo "> Determined $web_dir/nodejs/product directory exists"
# If product/sources directory does not exist
if [ ! -d "$web_path/wp-content/product/sources" ]; then
echo "> Determined $web_dir/wp-content/product/sources directory does not exist"
# Create sources directory
mkdir -p $web_path/wp-content/product/sources
echo "> Created $web_dir/wp-content/product/sources directory"
# Give Apache permissions to write to sources
chmod g+w $web_path/wp-content/product/sources
echo "> Added write permissions to $web_dir/wp-content/product/sources directory"
fi
fi
# If uploads directory does not exist
if [ ! -d "$web_path/wp-content/uploads" ]; then
echo "> Determined $web_dir/wp-content/uploads does not exist"
# Create uploads directory
mkdir $web_path/wp-content/uploads
echo "> Created $web_dir/wp-content/uploads directory"
# Give Apache permissions to write to uploads
chmod g+w $web_path/wp-content/uploads
echo "> Added write permissions to $web_dir/wp-content/uploads"
fi
# If w3-total-cache directory exists
if [ -d "$web_path/wp-content/plugins/w3-total-cache" ]; then
echo "> Determined WordPress has W3 Total Cache installed"
# If cache directory does not exist
if [ ! -d "$web_path/wp-content/cache" ]; then
echo "> Determined $web_dir/wp-content/cache does not exist"
# Create cache directory for cache files
mkdir $web_path/wp-content/cache
echo "> Created $web_dir/wp-content/cache directory"
# Allow Apache to write to cache
chmod g+w $web_path/wp-content/cache
echo "> Added write permissions to $web_dir/wp-content/cache"
fi
# If w3tc-config directory does not exist
if [ ! -d "$web_path/wp-content/w3tc-config" ]; then
echo "> Determined $web_dir/wp-content/w3tc-config does not exist"
# Create w3tc-config directory for configuration files
mkdir $web_path/wp-content/w3tc-config
echo "> Created $web_dir/wp-content/w3tc-config directory"
# Allow Apache to write to w3tc-config
chmod g+w $web_path/wp-content/w3tc-config
echo "> Added write permissions to $web_dir/wp-content/w3tc-config"
fi
fi
# If external-media-integration directory exists
if [ -d "$web_path/wp-content/plugins/external-media-integration" ]; then
echo "> Determined WordPress has External Media Integration installed"
# Move into plugin directory
cd $web_path/wp-content/plugins/external-media-integration
echo "> Moved into $web_path/wp-content/plugins/external-media-integration directory"
# Install required vendor files
composer install --verbose
echo "> Installed vendor packages with Composer"
# Move into web directory
cd $web_path
echo "> Moved into $web_path directory"
fi
fi
fi
done
# Print arguments to debug
else
echo "Not all required variables have values:"
echo "> FULL_DOMAIN: $1"
echo "> ROOT_DOMAIN: $2"
echo "> SUB_DOMAIN: $3"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment