Git post-receive hook not working
Asked Answered
I

5

8

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.

Iq answered 9/2, 2011 at 21:38 Comment(5)
The user your git daemon is running has permission to execute that script?Janellejanene
Not sure. It executes it when I run it through Git Bash, but it doesn't when I push to it. How would I check those permissions?Iq
the correct permission depends on the way you're sharing your repository, and the actual configurations. However, msquared's tells you the way to give executable permission to all users/groups, so it should work if the problem is indeed that.Janellejanene
The file is executable, it's just not being executed, or executed properly, when I git-push. Any other ideas?Iq
Put an echo statement in your post-receive hook to see if it's actually running, e.g., "echo hook ran!" -- git will send it to the caller.Medievalism
I
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!

Iq answered 17/2, 2011 at 0:8 Comment(0)
B
11

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
Barkeeper answered 9/2, 2011 at 21:52 Comment(1)
Did that. It's now rwxr-xr-x, and remember it was executing from the Git Bash shell, but still not executing on its own via push.Iq
U
6

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.

Unflinching answered 10/2, 2011 at 3:25 Comment(5)
Oh man. I'm using http. Fail. Any advice on switching to a different version without ruining whatever I currently have in the repo? I'll look into myself as well.Iq
You can still use HTTP if you use kernel.org/pub/software/scm/git/docs/git-http-backend.html but I don't know anything about trying to set it up on Windows. You can also setup an SSH server using either a native Windows server or with cygwin, which I also don't have any experience with. As you've found out, Windows isn't exactly the friendliest OS to host git repos on.Unflinching
@Unflinching Your comment proved useful for me to set up a toy repository over HTTP and I needed the hooks to be executed as well. Thanks!Heteropterous
@Unflinching - how can I check what transport method I am using?Moussaka
If in git extentions remote repositories I see url starting with [email protected] then it means its ssh transport method, right? Then what else can be the reason its not firing?Moussaka
I
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!

Iq answered 17/2, 2011 at 0:8 Comment(0)
F
1

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

Fecundity answered 9/2, 2011 at 22:1 Comment(2)
Not sure what the full path to curl is. I am just totally lost here. Is the "sh" path supposed to be the sh that came with Git? Windows is confusing. When I run the export, that's all fine, when I manually run the post-receive script, but the script isn't running at all when I push, so that doesn't really help me there. CURL path would be helpful. I have c:\xampp\php in my PATH, what more do I need?Iq
You need to figure out where curl is installed and use the full path to curl in your script. Run the shell (git's shell) manually and type "which curl" -- it will print something like /usr/bin/curl. Replace "curl" in your script with whatever it prints.Medievalism
C
1

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

Chaperone answered 13/10, 2016 at 14:33 Comment(1)
Welcome to Stack Overflow! This is really a comment, not an answer. With a bit more rep, you will be able to post comments.Culbert

© 2022 - 2024 — McMap. All rights reserved.