Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git Deployments with post-receive.sh.
#!/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
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
You can’t perform that action at this time.