git logs not matching when setting --work-tree with push-to-deploy post-receive hook
Asked Answered
R

1

1

I have setup push-to-deploy with my production server by setting up a --bare directory at /home/ubuntu/push-to-deploy/test.git, using that as my remote and adding a hooks/post-receive inside the --bare looking like this:

#!/bin/bash

while read oldrev newrev ref
do
  branch=`echo $ref | cut -d/ -f3`
  if [ "production" == "$branch" -o "master" == "$branch" ]; then

    git --work-tree=/var/www/test/ checkout -f $branch
    sudo chown -R ubuntu:www-data /var/www/test

    echo 'Changes pushed to Amazon EC2 PROD.'
  fi
done

This works great when pushing to this new remote from my localhost. The post-receive script executes as it should and the content updates are reflected in the /var/www/test directory as they should be. The only thing is that my git log inside /var/www/test is not matching my localhost at all. Is this normal behavior of --work-tree? If so, what can I do to retain this push-to-deploy functionality and still have my git log copied over to the production directory as well as the content?

Also

When my content copies into the production directory (/var/www/test) all the file ownerships are overrode to ubuntu:ubuntu which makes the www-data not be able to do its thing. I put a line in my post-receive to update the ownership after each receive but is there another way (a better way) to do this?

UPDATE

The way to ensure that www-data is retained as the group is to set the directory's guid like this:

chmod -R g+s /var/www/test

This will set it to whatever the current group of the directory is, so if you want it to be www-data then make sure you set the group to www-data before you issue that command.

Thanks

Rattler answered 13/4, 2014 at 20:37 Comment(6)
git log --git-dir=/home/ubuntu/push-to-deploy/test.git should work just fine when executed from /var/www/test.Fan
fatal: unrecognized argument: --git-dir=/home/ubuntu/push-to-deploy/test.gitRattler
Sorry, I meant git --git-dir=/home/ubuntu/push-to-deploy/test.git logFan
also, should I then add an option in the /var/www/test/.git/config to change the git-dir to /home/ubuntu/push-to-deploy/test.git so I don't have to specify that --git-dir option for each command like that?Rattler
I propose in my answer to set the environment variable GIT_DIR.Fan
Note that simply splitting up the ref-name with cut -d/ is not quite right: if there's a ref of the form refs/tags/master/v2, for instance, cut will extract field 3 which will be master. It's probably fine in this particular case, but in general you should just test "$ref" == refs/heads/master for instance.Vachon
F
1

You can set the environment variable GIT_DIR to /home/ubuntu/push-to-deploy/test.git and:

  • do your git --work-tree=/var/www/test/ checkout -f $branch
  • or do your git log in /var/www/test/

In both case, the right index will be taken into account.


The OP sadmicrowave confirms in the comments:

just did a chmod -R g+s /var/www/test and it is working now.

Fan answered 13/4, 2014 at 20:46 Comment(4)
that sounds like it would work in this case but what if I had more than one git repo on my production server?Rattler
@Rattler then you need to have some script/wrapper which encapsulate the git commands you want to run while setting the GIT_DIR to the right repo.Fan
any recommendations for the second part of my op?Rattler
nvm - just did a chmod -R g+s /var/www/test and it is working nowRattler

© 2022 - 2024 — McMap. All rights reserved.