4

I'm trying to use this post-recieve hook to update my live server

GIT_WORK_TREE=/var/www/www.example.org git checkout -f

This hook is on the remote bare repo and I would like the live work tree to be on a different server.

How do I set this up so that when the hook fires it checks out the files to the live server, does it ssh from the remote server to the live server? Where do I define this?

Here's what I'm trying to setup

http://www.dejaaugustine.com/2011/05/leveraging-git-as-a-full-fledged-web-development-tool/

but live and test sit on separate servers to the bare repo, but instead of using git pull I was going to use git checkout -f.

Stedy
  • 7,359
  • 14
  • 57
  • 77
user1039523
  • 111
  • 2
  • 8

3 Answers3

3

The following solution updates a live server code base with code from a bare repo on another server. This solution does not use scp or copy files to overwrite on the live server, because we want to avoid overwriting a whole directory (with git we can choose what we want to update).

Assumptions:

  • You are pushing your code to a bare-repository on a testing/staging server (or it might only be a server to host your "centralized" repo)
  • You want to update your live server with the code in this bare repo
  • Your live server is on a different server than you bare repo

On the live server:

  • Set up a non-bare git repo that hosts the live files: cd /var/www/www.yoursite.com && git init
  • From this live server, make sure you have ssh-access to the server where your bare repository is, using ssh-keys (not covered here)
  • Add the "centralized" repo: git remote add origin git@testserverip:/path/to/repo.git
  • Now, every time you want to update the live server code base, you can run git fetch origin followed by git merge

Since this is a live server, you typically do not want any merge-conflicts to cause trouble. If you don't care about loosing changes on the live server (because you probably never change anything important on the live server), you can use git merge -m 'Overwriting live server' -s recursive -X theirs origin/active-branch-on-live-server

A typical scenario is that you have other files (temp files, user-changed files etc) on the live server that you do not want to overwrite). Make sure to add all these files/directories to your .gitignore-file and make sure they are not added by git add. That way, they will not be affected when code is pulled from you centralized repo.

If you want this setup to me more automatic, make a bash script on the live server:

git fetch origin
git merge -m 'Overwriting live server' -s recursive -X theirs origin/active-branch-on-live-server

Make this script as an executable bash script (not covered here). Now, you can call this script from a hook-script on the "centralized" server, so that the live server gets updated every time you push your code.

On the "centralized" repo/test/staging server:

(You should already have a bare repo set up here, if not create it).

In your bare-repo.git/hooks/ create/edit the post-receive file, so that the live server runs the script created above when code is pushed to the bare repo:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)

    # Use this if-sentence to only update live server if branch is the wanted branch, e.g. master or stable
    if [[ "stable" == "$branch" ]]; then
            # Fetch this branch from live-server
            ssh root@ip-to-live-server '/path/to/script-created-above'
    fi
done

Make sure that the git user on you the server that hosts your bare repo has access to your live server via ssh-keys, so that the ssh in the script above works.

This is a schematic overview. Details can be found other places:

Community
  • 1
  • 1
igr
  • 4,529
  • 2
  • 15
  • 20
0

You could setup a bare repository on live/test servers and push to them with a post-receive hook from the main origin repository. Then again use post-receive hook to checkout to the web directory.

Or use rsync for the main origin repository to live/test servers. (Be careful with time lag.)

Luan Nguyen
  • 111
  • 2
  • 4
0

You will have to update your post-receive hook to get the files from the checkout folder in your git server and scp it to your live server.

GIT_WORK_TREE=/home/temp git checkout -f
scp -r /home/temp user@liveserver:/var/www/www.example.org
manojlds
  • 290,304
  • 63
  • 469
  • 417