Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git hooks and how they work

Tags:

git

hook

So I'm trying to get hudson to build with a post-receive hook. In my local git repo I set post-receive.sample to just post-receive, chmod 755 and added in the line:

/usr/bin/curl -u user:secret http://localhost:8080/hudson/job/MyJob/build?token=secondsecret

If I force a build, hudson updates the code, but here's what I don't understand, the hooks in that repo DON't have the .sample after them like they do locally, and the post-receive in the hudson repo doesn't have that line of code above. What's going on here and how are hooks integrated into the whole git process? Do I need to be changing this hook on the remote repo? I would have thought it was enough to do it locally and push so anyone fetching from that repo get the new hooks. I can't understand how another user's repo would have different hooks.

like image 333
brad Avatar asked Nov 11 '09 18:11

brad


People also ask

How do git hooks work?

Git hooks are shell scripts found in the hidden . git/hooks directory of a Git repository. These scripts trigger actions in response to specific events, so they can help you automate your development lifecycle. Although you may never have noticed them, every Git repository includes 12 sample scripts.

How many groups of git hooks are there?

Like many other Version Control Systems, Git has a way to fire off custom scripts when certain important actions occur. There are two groups of these hooks: client-side and server-side.

Where do I put my git hooks?

Create Your Managed Hooks Directory githooks at the root where all these hooks live. You can choose whatever makes the most sense for your project. Remember that when adding hooks they must be executable, so make sure you chmod +x each of them to make that happen.

How do pre-commit hooks work?

pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. Confusingly, there is also a Python package called pre-commit which allows you to create and use pre-commit hooks with a way simpler interface.


1 Answers

You basically have two options:

  1. Place the post-receive hook on the server and let the server run curl.
  2. Place a post-commit hook on your local repo and let your local box run curl.

As your build job will probably fetch the code to build from the repo on the server, only option 1. makes sense. In case 2., the build job would probably have to fetch the code from your local box, and that is probably not what you want.

You cannot place hooks onto the server using git push. You (or someone with the appropriate permissions) needs to do that by manually logging into server and modifying the hook script files locally.

like image 160
ndim Avatar answered Sep 20 '22 12:09

ndim