Skip to content

Instantly share code, notes, and snippets.

@abkiran
Forked from ryansechrest/post-receive-laravel.php
Created September 1, 2018 17:26
Show Gist options
  • Save abkiran/6d47dec0f434e47da17cfccacb33095b to your computer and use it in GitHub Desktop.
Save abkiran/6d47dec0f434e47da17cfccacb33095b to your computer and use it in GitHub Desktop.
Git post-receive hook to deploy a Laravel application.
#!/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="public"
# 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
# Assume environment group is production
env_group="PRODUCTION"
# Determine whether current branch is target branch for environment
if [[ "$FH_ENVIRONMENT" == "LOCALHOST" ]]; then
echo '> Determined environment is LOCALHOST'
env_group="DEVELOPMENT"
echo '> Determine environment group is DEVELOPMENT'
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'
env_group="DEVELOPMENT"
echo '> Determine environment group is DEVELOPMENT'
if [[ "$branch" == "develop" ]]; 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" ]]; 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 project directory
cd $project_path
echo "> Moved into $project_path directory"
# If project directory is empty
if find . -maxdepth 0 -empty | read; then
echo "> Determined project directory is empty"
# Clone branch from origin repository into project directory
git clone /opt/repositories/$1.git -b $branch .
echo "> Cloned origin/$branch branch from $1.git repository into project directory"
# Create empty file to remember created date
touch $project_path/.created
echo "> Created .created file in project directory"
# If project directory is not empty
else
echo "> Determined project 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 changes from origin
git fetch origin
echo "> Fetched changes from origin"
# Fetch and merge changes into project directory
git pull origin $branch
echo "> Pulled origin/$branch into $branch branch in project 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 project directory"
fi
# Create or update empty file to remember last updated date
touch $project_path/.updated
echo "> Updated .updated file in project directory"
fi
# Install PHP dependencies
if [[ "$env_group" == "DEVELOPMENT" ]]; then
composer install
else
composer install --no-dev
fi
echo "> Installed dependencies with composer"
# Install Node dependencies
npm install
echo "> Installed node modules"
# Run Gulp tasks
if [[ "$env_group" == "DEVELOPMENT" ]]; then
gulp
else
gulp --production
fi
echo "> Executed gulp tasks"
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