What user runs the git hook?

2020-02-24 13:01发布

I have a post-update hook on my server, such that when I

git push

it does a pull on the live web directory. However, while the push always succeeds, the post-update hook sometimes fails.

The hook is pretty simple:

#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
cd /var/www
env -i git pull

I'm pushing updates from a variety of places, but sometimes I have to login as root on the server and manuall do a

env -i git pull

I only have to do it 20% of the time though. Any ideas why it would fail randomly? Also, how would I get it to log error messages, since it might be running as someone who can't write to the file system?

标签: git hook
2条回答
Melony?
2楼-- · 2020-02-24 13:23

The hooks are run as the user doing the push. If you have some kind of pre-made setup, that may be a user like git or gitosis, or it may be you. Just look at how you have the remote configured. (git remote show <remote-name> or just examine .git/config if you don't know) Presumably you're pushing via SSH, and there's a username@hostname in the URL.

P.S. It's pretty quick to demonstrate this - just clone a repo locally, throw a post-update hook in with an echo $USER or something similar, and try pushing as yourself or a different user (directly or through ssh).

查看更多
该账号已被封号
3楼-- · 2020-02-24 13:48

I decided to test this on my gitlab 6 server by creating a pre-receive hook and echoing out the user information

$ cat /home/git/repositories/foo/foo.git/hooks/pre-recieve
#!/bin/bash
set -x
echo -e "The user the hook is run as is $USER"
echo -e "Just to doublecheck, the user is $(whoami)"
exit 1

It looks like it is run as the git user

$ git push 
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: + echo -e 'The user the hook is run as is'
remote: The user the hook is run as is
remote: ++ whoami
remote: + echo -e 'Just to doublecheck, the user is git'
remote: Just to doublecheck, the user is git
remote: + exit 1
查看更多
登录 后发表回答