Currently I deploy a project to production with git, using git push production master to a repository with the following post-receive hook:
#!/bin/bash
GIT_WORK_TREE=/home/username/www.example.com/myproject/ git checkout -f
production is a remote, added via git remote add production ssh://server/home/username/projects/myproject.git.
But now I need to deploy a separate branch to a separate path on the server. I did come up with a solution, but I suppose there’s a better way to do it.
What I did was create a new repository on the server, myproject-alternate.git, with a similar post-receive hook (replacing /myproject/ with /myproject-alternate/), added this new repository with git remote add alternate ssh://server/home/username/projects/myproject-alternate.git. Now I can deploy to the alternate path with git push alternate branchname:master.
This works, but I have some issues:
- The command to deploy to the alternate server is not what I was expecting—more than once I forgot the
:masterat the end and the server’s repository received a new branch and the post-receive hook wasn’t triggered. - I’m not sure if creating a new repository on the server was the best solution, and I wonder what would happen with a larger project.
Are there other ways to accomplish this deploy flow without the mentioned issues? Maybe a better post-receive hook that uses the received branchname to deploy to the right path? (is this even possible?)
I’ve written a blog post about a setup I use to deploy my website to a staging server and the live server. You could do something similar. The key is to configure which branches you’re going to be pushing in the
.git/configfile of your local repository, something like this:This will set it up so that whenever you type
it will automatically push the local
branchnamebranch to the remotemasterbranch in the alternate repository.However, since your alternate and production work trees are on the same computer, you can probably get away with only creating one repository and just checking it out to two different places. To do that, ignore the previous paragraph, and instead put something like this in your post-receive hook:
(obviously you don’t have to use an
ifstatement). If you do this, I suggest making the repository bare so that it doesn’t store its own working copy, just for simplicity. This way, you only need to have one remote, and whenever you push themasterbranch, it will update the production work tree, and whenever you push thebranchnamebranch, it will update the alternate work tree.Disclaimer: I haven’t actually tested this, so try it out in a test folder first. If I find any errors I’ll come back and edit.