Git post-receive hook not working

2019-02-06 22:30发布

问题:

My setup is a windows XAMPP server, with cURL enabled, and Git and Hudson installed. Hudson polls Git every minute to look for changes, and if it finds them, it creates a build. I use this build as my testing server. This works fine.

I would like to set up a post-receive hook on my central remote repository that runs the force build function of Hudson.

I've created a post-receive file called "post-receive" in the hooks directory in my central Git repository, the one that is pushed to from the developers' local branches. They each push to their own branch on the central repository. I want to run the post-receive build immediately after every push, instead of having Hudson poll Git every minute.

When I open a shell to the remote server and run "post-receive" in the hooks folder, it runs. It just isn't being called when people push to it from their local repository copies to the central one.

Maybe I'm not explaining this right, but it's how I understand Git.

The post-receive code is two lines:

#!/bin/sh
curl http://myserver.com:8080/hudson/job/myjobname/build?token=mytoken

Again, when I open a shell and run this, it works, but when someone pushes to it, nothing happens, until a minute or less goes by, Hudson realizes that Git was changed, and then it builds.

I am happy to clarify if need be. Any help is greatly appreciated.

EDIT: After playing around with it, I feel like maybe post-receive isn't executing because refs aren't being updated of something? The git documentation says

It executes on the remote repository once after all the refs have been updated.

Does this mean if nothing updates, it won't execute? And if so, I'm pretty sure things are updating anyway so it shouldn't apply.

Here's my process: Make edits locally. Commit edits. Push from my HEAD to the remote branch called 'mybranch' (not the master branch, which is checked out) This is the point at which I want my hook to execute.

回答1:

Please check whether your file has executable rights. Otherwise it can not be executed. Something like rwxr-xr-x should be sufficient.

You can add the missing bit with

$ chmod +x /path/to/post-receive


回答2:

What transport method are you using to push to the repo?

Hooks are only executed when using 'smart' transport protocols like ssh and the cgi implementation of http. For other 'dumb' transport protocols like ftp, rsync, and the older http implementation, hooks will never be executed. I assume the git protocol would execute hooks but pushing over that protocol has never been considered a good idea.



回答3:

All of these answers are useful, but it turns out I needed the "bin" directory of git in my PATH variable. I had the "cmd" directory only. Added c:\my_path_to_git\git\bin to PATH and it works fine. I found this answer by trial and error and looking at the apache error log. Thank you all for your help!



回答4:

Try using full path to curl in post-receive file, as PATH can be different than when running script by hand.

If you want to see what is the environment of running post-receive script:

#!/bin/sh
export > file

or just (it will show env. variables after pushing into repo)

#!/bin/sh
export

Also, I'm not sure how executing shell scrips works on windows, so #!/path/to/installed/sh instead of #!/bin/sh might help



回答5:

I think the simple answer is you need to move the contents of the post-receive hook server side instead of client side