JoshMock
JoshMock

Reputation: 1301

How do I use a post-receive hook to conditionally run commands depending on the branch updated?

I have the following post-receive hook I wrote on a virtual server such that it copies the dev and production versions of the repository into dev and production directories where each version of the site is run.

The following is the post-receive hook I wrote. How do I conditionally refresh just one or the other depending on what branch just got pushed?

#!/bin/sh

if [ -n $GIT_DIR ]; then
    unset GIT_DIR
    cd ..
fi

echo "Deploying to dev"
git checkout develop
umask 002 && git reset --hard
cp -r /path/to/git/files/* /path/to/dev/site/

echo "Deploying to prod"
git checkout master
umask 002 && git reset --hard
cp -r /path/to/git/files/* /path/to/prod/site/

Upvotes: 2

Views: 1119

Answers (2)

ellotheth
ellotheth

Reputation: 4523

You want to loop through updated branches and deploy any updates to develop to the dev site, and any updates to master to the production site, correct? Try this: http://blog.ekynoxe.com/2011/10/22/git-post-receive-for-multiple-remote-branches-and-work-trees/

Upvotes: 0

Nazin
Nazin

Reputation: 827

Post-receive hooks receives on standard input 3 variables: old revision, new revision and ref name - which is exactly what you need. You can improve your script with:

read oldrev newrev refname

And do some ifs depending on what refname contains (i.e: refs/heads/master).

Upvotes: 1

Related Questions