1

I'm aware that there are numerous questions about that topic already, but I never really got it working the way I want.

I'm trying to setup an easy deploy workflow for a small site. I'm using this Tutorial for reference: https://halfthetruth.de/2011/09/13/using-git-to-deploy-a-website/

I setup everything like explained in the tutorial but I'm running into this problem:

This is when I try to push from my local repository:

example.com|master ⇒ git push production +master:refs/heads/master
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (12/12), 967 bytes | 0 bytes/s, done.
Total 12 (delta 3), reused 0 (delta 0)
remote: ********************
remote: Post receive hook: Updating website
remote: ********************
remote: fatal: No remote repository specified.  Please, specify either a URL or a
remote: remote name from which new revisions should be fetched.
remote: fatal: Unable to create '/home/user/git/example.com.git/index.lock': Permission denied
To ssh://[email protected]/~/git/example.com.git
 * [new branch]      master -> master

My post-receive hook looks like this:

#!/bin/sh
echo "********************"
echo "Post receive hook: Updating website"
echo "********************"

export GIT_WORK_TREE=/usr/share/nginx/www/example.com
cd $GIT_WORK_TREE
sudo -u www-data git --work-tree=/usr/share/nginx/www/example.com --git-dir=$HOME/git/example.com.git pull
sudo -u www-data git --work-tree=/usr/share/nginx/www/example.com --git-dir=$HOME/git/example.com.git checkout master -f

My bare repository on the remote server is located in /home/user/git/example.com.git and the document root for nginx where the site should be served from is located in /usr/share/nginx/www/example.com.

It looks like there's some problem with running the git pull command in a non-git directory but I don't know how to resolve this. Anyone who could point me in the right direction?

Thanks!

4
  • According to that error message, ~/www/example.com.git isn't actually a git repo. Can you look in there? sudo -u www-data ls -la ~/www/example.com.git. Maybe try using the full path without the ~ shortcut? (/usr/share/nginx/www/example.com.git) Commented Jan 10, 2014 at 20:13
  • The directory is definitely a repository. It's showing up as my master in zsh and git status shows me the usual "fatal: This operation must be run in a work tree" for a bare repository. Commented Jan 10, 2014 at 20:18
  • 1
    AFAIK ~ is only expanded to $HOME by the shell if it's at the beginning of an argument, which it isn't in this case. Replace it with $HOME. Commented Jan 10, 2014 at 20:19
  • Thanks, I updated the question with all the changes I made in the meantime, including the one you suggested. The new one seems to be some kind of permission problem where it's trying to write to the git directory but while still acting under the sudo'ed www-data user. I'm wondering why none of these git deploy tutorials is talking about permissions at all. Commented Jan 10, 2014 at 20:34

2 Answers 2

1

if you are not in a git repo, you can still point to a git repo with options on the git command itself:

git --work-tree=path/to/worktree --git-dir=path/to/.git pull
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I updated the original question after adding your suggestion. Unfortunately it's not resolved by this.
1

I "fixed" this by adding my user to the www-data group.

sudo chown www-data /usr/share/nginx/www/example.com
sudo usermod -a -G www-data user
sudo chgrp -R www-data /usr/share/nginx/www/example.com
sudo chmod -R g+w /usr/share/nginx/www/example.com

Once this was done I removed the custom sudoers file mentioned in the tutorial linked in the original question and removed the sudo -u www-data part the post-receive file.

If there's a better solution to this problem I'll gladly accept your answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.