Skip to content

Instantly share code, notes, and snippets.

@llakewood
Last active January 8, 2019 16:48
Show Gist options
  • Save llakewood/3e8d53cd5683518ab91949effb90a7ec to your computer and use it in GitHub Desktop.
Save llakewood/3e8d53cd5683518ab91949effb90a7ec to your computer and use it in GitHub Desktop.
Deploy Remotely via Git Post Receive Hook

Deploy via Git Once you’ve added your app “appname” under “appuser”, we can create a git repository on the server which will deploy the app once pushed to.

Log in as appuser. Create the git repo:

mkdir -p ~/repo/appname.git
cd ~/repo/appname.git
git init --bare

Next, create a post-receive script

vi hooks/post-receive

Add in the following, editing appname:

#!/bin/bash
export GIT_WORK_TREE=/srv/users/appuser/apps/appname
export SITE="App Name"


while read oldrev newrev ref
    do
        branch=`echo $ref | cut -d/ -f3`

	    if [ "$branch" ] ; then

            git checkout -f $branch

	    fi
    done

echo "Deployed to $GIT_WORK_TREE"

cd $GIT_WORK_TREE

## other post-deploy commands
# composer install
# bin/appdeploy.sh

Make it executable:

chmod a+x hooks/post-receive

To push to live, add a remote locally (on your working copy):

git remote add live ssh://username@remote_address/srv/users/username/repo/appname.git

The push live with:

git push live master

Post deploy scripts should take care of the installing and script building

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment