Aero Wang
Aero Wang

Reputation: 9247

How to create a git hook on the server side to pull (in a path that requires root permission to modify) after it receives a push?

I've tried to use post-receive and post-update to:

cd "/home/servers/a"
git pull
exit

but it doesn't seem to work. I think this might be because either post-receive and post-update are not triggered after the server received a push, or it might be that for git pull to run successfully on the server I still need to type in the password (in this case how can I save password in the script file then).

Upvotes: 2

Views: 290

Answers (1)

VonC
VonC

Reputation: 1327784

Make sure your script is executable (in your remote_repo.git/hooks)... and executed: for that, a simple echo "test" would be enough.

Then, when doing a git pull, specify the work-tree and git-dir.

#!/bin/bash
cd "/home/servers/a"
echo "pulling in $(pwd)"
git --work-tree=/home/servers/a --git-dir=/home/servers/a/.git pull

No need for exit.

The pull should not need a password, since the remote origin for that repo should be a relative path to the repo you just pushed to:

cd "/home/servers/a"
git remote -v

You should see /path/to/remote_repo.git.


For paths managed by root, a possible solution is, in the hooks file, to use sudo git --work-tree=/home/servers/a --git-dir=/home/servers/a/.git pull, after modifying /etc/sudoers with:

dev ALL=(ALL) NOPASSWD: git

(as described in "Allow certain guests to execute certain commands")
If git is in root $PATH, that will work.

Upvotes: 2

Related Questions