Permissions with Git Post-Receive

2019-02-19 05:41发布

问题:

I have setup git locally and on my server and created a 'post-receive' so that when I do a push to the live server it should then checkout to the virtual hosts directory of the website.

#!/bin/sh
GIT_WORK_TREE=/var/www/domainname/public_html/ git checkout -f

However, when I try to do my push, it goes out the master successfully but I get permission denied errors when trying to create those files on my server web root.

e.g. remote: error: unable to create file index.php (Permission denied)

These errors actually make sense to me due to the way I have setup my server. I disabled root access and created a new user that is added to the sudoers through visudo - I then set up SSH keys for this user. I use this user for pushing out my git changes to the server.

The issue is, I believe, that this user that I connect to push doesn't have the rights to write to the virtual hosts directory. All my files in these folders are owned by www-data (my nginx user) with the same group.

What is the solution? Create another user with SSH keys to connect with just for git that has higher permissions or is there a way to grant the post-receive sudo like abilities?

Apologies for the very long winded question but wanted to be absolutely clear with what I did.

回答1:

If your repo is created with the --shared option (see "Change GIT repository to shared"), that would allow users from the same group to access and write.

Is there any way you can group those different users in the same group?
A bit as what is mentioned in "Git unable to create file permission denied" same group.
Or sudo access, as explained in the comments of this question, changing the sudoers:

 git ALL = (root) NOPASSWD: /usr/local/sbin/prgetsimpleappscom