Post Receive Hooks on Webfaction
Published: October 31, 2012
Although the title looks scary, if you’re looking for this information it can be kind of hard to find, especially for designer types hacking on personal projects. The basic idea is that you want to commit changes to a Git repository, and deploy those changes to your production box. To do this, we can set up what’s called a post-receive hook in your production repository. This post-receive hook will run after changes are pushed. I use Webfaction, so I have SSH access, Git and all that good stuff.
Create the Post-receive Hook
To do this, you need to SSH into your server, cd into the repo, and run some Git commands to create the hook. This will use the cat command to create the post-receive hook and make it executable.
$ cat > hooks/post-receive #!/bin/sh GIT_WORK_TREE=/home/username/webapps/reponame git checkout -f GIT_WORK_TREE=/home/username/webapps/reponame git reset --hard $ chmod +x hooks/post-receive
Obviously, you’re going to want to change the various directory names and such to what works for your setup.
Add the Production Repo as a Remote
Next you might want to add that repo as a remote. That’s simple.
git remote add production ssh://email@example.com/~/webapps/reponame/
Make Git Like It
Now Git is going to freak out on us when you try to push to production because it’s already checked out on the server. To get around this, we have to set the configuration option receive.denyCurrentBranch to ignore.
git config receive.denyCurrentBranch ignore
That’ll get Git to shut up and you should be able to push to your new production environment.
Update: You might be wondering how you can push your changes to both Github and your live production server at the same time, instead of doing each one individually. What you can do is create a remote that has both of the repos you want to push to in it. For example, you
$ git remote add all origin-ssh://firstname.lastname@example.org/username/reponame.git $ git remote set-url --add all production-ssh://email@example.com/~/webapps/reponame/
You should then be able to run git push all --all and have your changes be pushed to both your Github repo and your production one.
About David Hemphill
Builder of web things. Family man. I used to fight. I used to music. I'm an introvert, but I still like you.