Prevent Git Push live master from changing file permission and ownership

Viewed 209

I have been battling with this for days now... each time I do git push live master (using git post hook) ... Remote file permissions and ownership are being changed every time back to the default which would make pages on the website throw a 500 error. I usually get around this by running sudo chown -Rf www-data:www-data /var/www/<Website-Root-Folder> but I want this permanently fixed, so I don't have to do this every time.

I have done the following, which I found here: Permissions with Git Post-Receive

sudo usermod -a -G www-data root  // As I'm doing my push using the root user. 

Sorry, I'm new to git, linux, Not sure what else to do. Any help in making this work without me having to run this sudo chown -Rf www-data:www-data /var/www/<Website-Root-Folder> after every push will be highly appreciated.

Thanks

1 Answers

You wrote a script, which, at some point, executes git checkout -- or an equivalent command -- that actually creates and updates files in your target folder.

You should make it so that this command is run with the user you want, for example :

sudo -u www-data git checkout ...

You may need to set the access rights on the repo itself so that user www-data can read the repo :

  • if your repo has r access for all users, you have nothing more to do,

  • you can decide to set the group of all files in that specific repo to www-data :

    # in your post-receive script :
    chrgp www-data -R .
    
  • or set the setgid bit on that repo's directory, so that files and directories created within automatically inherit this directory's group (instead of the user's group) :

    # run once on your server, as root user :
    $ chmod -R g+s /path/to/repo.git
    $ chgrp -R www-data /path/to/repo.git
    
    $ ls -ld /path/to/repo.git
    drwxrwsr-x 9 root www-data 4096 janv. 13 14:20 repo.git
          ^
    # notice the 's' in the access rights
    
Related