2

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:

  1. The command to deploy to the alternate server is not what I was expecting—more than once I forgot the :master at the end and the server's repository received a new branch and the post-receive hook wasn't triggered.
  2. 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?)

dipnlik
  • 378
  • 4
  • 13

2 Answers2

2

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/config file of your local repository, something like this:

[remote "alternate"]
    url = ssh://server/home/username/projects/myproject-alternate.git
    fetch = +refs/heads/master:refs/remotes/alternate/master
    pushurl = ssh://server/home/username/projects/myproject-alternate.git
    push = refs/heads/branchname:refs/heads/master
[remote "production"]
    url = ssh://server/home/username/projects/myproject.git
    fetch = +refs/heads/master:refs/remotes/production/master
    pushurl = ssh://server/home/username/projects/myproject.git
    push = refs/heads/master:refs/heads/master

This will set it up so that whenever you type

git push alternate

it will automatically push the local branchname branch to the remote master branch 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:

#!/bin/bash
checkout_alt=
checkout_prod=
while read oldrev newrev refname; do
    case "$refname" in
        ( "refs/heads/branchname" )
            export checkout_alt=1 ;;
        ( "refs/heads/master" )
            export checkout_prod=1 ;;
    esac
done
test -n "$checkout_alt" && GIT_WORK_TREE=/home/diazona/tmp/gittest/alt/ git checkout -f branchname
test -n "$checkout_prod" && GIT_WORK_TREE=/home/diazona/tmp/gittest/prod/ git checkout -f master

(obviously you don't have to use an if statement). 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 the master branch, it will update the production work tree, and whenever you push the branchname branch, 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.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • Can you please explain the `branch=${3##*/}` line before I test it? – dipnlik Sep 22 '11 at 11:15
  • It removes the longest leading substring matching the pattern `*/`. – David Z Sep 22 '11 at 16:11
  • ... from which string? (sorry for asking again, I really want to understand the full line of code, and I can't Google something with so many symbols on it) – dipnlik Sep 22 '11 at 18:53
  • Ah, OK: it strips the prefix matching `*/` from the third argument to the bash script (`$3`), which is going to be something like `refs/heads/master`. The last component should normally be the branch name. You can read more about this in the bash man page, under the section "Parameter Expansion." – David Z Sep 22 '11 at 19:21
  • oooh, wait, actually I messed up. I forgot that the hook script gets its "arguments" through stdin, not as actual positional parameters. So you'll need to do something a little different. I'll edit my answer accordingly. – David Z Sep 22 '11 at 19:27
  • OK, fixed. I left out the parameter expansion (prefix-stripping) this time because it's probably a better idea to check the whole string. Everything in the new script is explained in the bash man page. – David Z Sep 22 '11 at 19:42
0

I've also written a blog post on the set up I use, which allows me to git push each branch in my project up to my remote, deploying each into their own respective directories.

For example:

Branch      | Directory on remote
--------------------------------------
master      | /www/foo.com/master
staging     | /www/foo.com/staging
production  | /www/foo.com/produciton

So, to do this, add the following function to your server dotfiles:

function gitorigin() {
    mkdir -p "$1"
    cd "$1"
    git init --bare
    git config core.bare false
    git config receive.denycurrentbranch ignore
    cat <<EOF >hooks/post-receive
#!/bin/bash

while read oldrev newrev ref
do
    branch=\`echo \$ref | cut -d/ -f3\`
    mkdir -p ../\$branch
    git --work-tree=../\$branch checkout -f \$branch
    echo Changes pushed to \$branch
done
EOF
    chmod +x hooks/post-receive
    cd ..
}

On Remote, create a repository:

$ cd /www/foo.com
$ gitrepo foo.git

On Local, push a branch

$ cd ~/Sites/foo.com
$ git remote add origin ssh:///<USER>@<HOST>/www/foo.com/foo.git
$ git push origin <BRANCH>

Will push your master branch up to remote, into /www/foo.com/<BRANCH>/.

AzzaMeyt
  • 1
  • 1