13

Background: Developing a Facebook app using PHP Laravel framework & MySQL for database.

I have setup Gitlab on our development server and created a repository on it where the team has been added and is committing & pushing code.

What I would like to do is, when I push code to a particular branch on GitLab (for example Master) I would like it to be available at /var/www/productname so that I can test it within the Facebook canvas as well (certain things happen there that can't be tested on your local machine).

However, I don't want the hook to copy all the files over every time a push happens on master, just the files that were modified.

Can anyone help out with such a hook?

Thank you.

1615903
  • 32,635
  • 12
  • 70
  • 99
Kopty
  • 538
  • 5
  • 20
  • 2
    You had selected the right answer before. It is best to leave git do the checkout, respecting the .gitignore and other git attributes, rather than rsynch which copies/sync everything. – VonC May 25 '17 at 06:43

3 Answers3

13

You would need to add to the bare repo (managed by GitLab) a post-receive hook which would:

  • maintain a working tree (git checkout -f master)
  • copy the files you want from that working

That would be:

cd ~git/repositories/yourRepo.git/hooks
touch post-receive
chmod +x post-receive

You can make sure that hook will only be active if someone pushes on branch master:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        # Do something
    fi
done

For the checkout done by that hook, see "GIT post-receive checkout without root folder", that is:
make sure you specify --git-dir and --git-work-tree:

git --git-dir=/path/to/project_root.git --work-tree=/path/to/your/workingtree checkout -f

Again, /path/to/your/workingtree can be:

  • only an intermediate working tree for you to extract from it the relevant files you want to copy.
  • or the path to your app, if you want all the files from master to be updated in the destination directory of your wab app.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Can we use the sha key as a new sub directory name? – dipi evil Jul 28 '15 at 12:56
  • @dipievil I am not sure how a sha key plays any role in this question or this answer. – VonC Jul 28 '15 at 12:58
  • For example: git --git-dir=/path/to/project_root.git --work-tree=/path/to/your/DYNAMIC_NAME_COMMIT_SHAKEY checkout -f – dipi evil Jul 28 '15 at 13:10
  • @dipievil why not, indeed. Provided you know beforehand the SHA1 you do checkout. – VonC Jul 28 '15 at 13:12
  • But the post-applypatch hook is invoked AFTER the commit. In this case do we have the SHA1 value in any variable? Or the TAG value... Anyway, some value that help me to create a custom directory name. – dipi evil Jul 28 '15 at 15:43
  • @dipievil that is what I meant by " Provided you know beforehand the SHA1 you do checkout". I am not sure it is possible. As for a value to create a custome directory name, `git describe` could help (as in http://stackoverflow.com/a/19736453/6309) – VonC Jul 28 '15 at 16:56
4

This post on Seb Duggan blog describe it nicely. You simply create post-receive hook that contain:

#!/bin/sh
GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

Add it +x flag and it will checkout your repo in given folder.

Hauleth
  • 22,873
  • 4
  • 61
  • 112
  • You can also see my [gitdeploy](https://github.com/hauleth/puppet-gitdeploy) Puppet manifest. It is still in early development but as I tested so far it works :) – Hauleth Apr 18 '13 at 08:30
  • Sorry but what do you mean by +x flag? Also, is there a reason my post-commit hook runs but not my post-receive? Thanks! – andli Jun 11 '13 at 12:35
  • `chmod +x .git/hooks/post-receive` it means that you need to add executable permission for you to this file. – Hauleth Jun 11 '13 at 17:41
4

Use rsync rather than cp for this operation. It does diff checking automatically. that way your post-commit hook doesn't become needlessly complicated.

  • This is excellent advice and I have implemented it into my workflow. Thank you so much. – Kopty Jun 25 '13 at 07:59