2

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).

Aero Wang
  • 8,382
  • 14
  • 63
  • 99

1 Answers1

2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Does this mean within the same machine, git can do push and pull without needing a password? Sweet. I actually set up my deployment on my server as `git@127.0.0.1:/home/gitrepo/a.git` – Aero Wang Jul 26 '18 at 05:01
  • @AeroWang check the remote used in the folder a (where you pull): if this is a path, there is no authentication needed. – VonC Jul 26 '18 at 05:02
  • It works! Okay now should I use `post-receive` or `post-update` if I want to trigger a pull after a push because neither seems to be triggered... – Aero Wang Jul 26 '18 at 05:04
  • @AeroWang post-receive (for instance; https://stackoverflow.com/a/49838994/6309), but make sure the name of the script is post-receive without extension, and that it is executable. – VonC Jul 26 '18 at 05:15
  • Thanks! I realized the process folder /data/server requires root permission to modify files, which means `git pull` in /data/server will require password. What can I do now? – Aero Wang Aug 14 '18 at 05:54
  • @AeroWang Execute your command with a sudo, after modifying first the sudoer (https://unix.stackexchange.com/q/215412/7490) That way, no password required! – VonC Aug 14 '18 at 06:26
  • Sorry I am a little confused. Do you mean in my hooks file I wrote `git --work-tree=/home/servers/a --git-dir=/home/servers/a/.git pull` after modifying `/etc/sudoers` in which I add `dev ALL=(ALL) NOPASSWD: git` (do I need to create command alias for git btw)? – Aero Wang Aug 14 '18 at 06:40
  • @AeroWang Yes: if the `git` command is executed as root, it will be able to write in a root-managed path). No need for an alias, just make sure git is in root `$PATH`. – VonC Aug 14 '18 at 06:44
  • Okay just to make sure...which would I write, 'sudo git' or 'git', in my hooks file? – Aero Wang Aug 14 '18 at 06:48
  • @AeroWang `sudo git` – VonC Aug 14 '18 at 06:50
  • @AeroWang I have edited the answer to reflect this discussion in the comments. – VonC Aug 14 '18 at 06:54