Skip to main content

Using Git to deploy and update a website Pt. 2

Lovell Felix 1 min read

Archive note: the tools and versions have moved on. I have kept this entry because the debugging path and the underlying constraint may still be useful.

Previously, in Tutorial: How to use Git to deploy and update a website Pt. 1, I set up a single command that pushes changes from my local machine straight to the git server and the web server.

It worked well and cut my deployment time significantly, so now I'm taking it a step further: pushing two different branches to two different locations on the remote server. There are two branches on my local machine, Master and Beta.

With this setup, the Master branch deploys to http://example.com, and the Beta branch deploys to http://beta.example.com. Start by creating the Beta branch from your existing local directory:

git branch checkout -b beta

Then SSH to the Git server and navigate to the repository folder, for example /home/project/website.git/.

Create a post-receive hook that checks out the Master and Beta branches into their respective web server DocumentRoots:

cat > hooks/post-receive
#!/bin/sh
while read oldrev newrev ref
do
  branch=`echo $ref | cut -d/ -f3`

  if [ "master" == "$branch" ]; then
    git --work-tree=/path/under/root/dir/live-site/ checkout -f $branch
    echo 'Changes pushed live.'
  fi

  if [ "beta" == "$branch" ]; then
    git --work-tree=/path/under/root/dir/beta-site/ checkout -f $branch
    echo 'Changes pushed to beta.'
  fi
done
chmod +x hooks/post-receive

And that's it.

About the author

Lovell Felix

Infrastructure and reliability engineer working on Linux platforms, configuration delivery, and deployment safety at fleet scale.

@lovellfelix

More notes