Understanding Git Hook - post-receive hook
Asked Answered
E

2

6

I have written simple shell script to throw "success and failure message" and placed it under .git/hooks/ with all proper permissions. I want to call this script as a post-receive. But the script is not working, running the script simply works but as post-receive hook it doesn't work.

Is their something being missed or have i wrongly understood the post-receive hook. Can some one explain client-side and server-side hooks and how to execute them.

I have searched over it but not able to understand.

Earring answered 23/1, 2015 at 8:44 Comment(2)
I'd suggest showing us a little more, like the script you are using and how you are trying to invoke it. Also, the Git Hooks section of Pro Git is helpful, as well as the githooks man page.Lamented
Did you perhaps forget to mark it executable? An explicit sh ./post-receive will work regardless, but almost nothing else (and not this).Punctual
P
13

To enable the post-receive hook script, put a file in the hooks subdirectory of your .git directory that is same named (without any extension) and make it executable:

touch GIT_PATH/hooks/post-receive
chmod u+x GIT_PATH/hooks/post-receive

For more info check this doc: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

Example

Check this example (a simple deploy) GIT_PATH/hooks/post-receive:

#!/usr/bin/env bash
TARGET="/home/webuser/deploy-folder"
GIT_DIR="/home/webuser/www.git"
BRANCH="master"

while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref = refs/heads/$BRANCH ]];
    then
        echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
    else
        echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
done

Source: https://gist.github.com/noelboss/3fe13927025b89757f8fb12e9066f2fa#file-

Plush answered 31/10, 2018 at 13:42 Comment(1)
I did not understand what is the difference between TARGET and GIT_DIR...Coinage
P
3

It needs to be called post-receive (no extension, no post-receive.sh for instance).

If it is placed (as the OP did) in .git/hooks folder, and made executable, it will be called when you are pushing to that repo (since it is a server hook).
If you were to install it on your own local repo, it would not be called (unless you somehow push to your own repo, which seems unlikely).

For a remote Git hosting server like GitHub, you would need to implement that hook as a webhook (a listener to GitHub push event).

Parks answered 23/1, 2015 at 10:33 Comment(6)
How do I install the hook "on server" ? I have just placed it in the .git/hooks folder and made it executable. is there something else that needs to be done?Tarkany
@Tarkany what git remote -v returns? What is your server?Parks
that returns the following: origin github.<corp>.com/sumit/webhooks.git (fetch) origin github.<corp>.com/sumit/webhooks.git (push) where <corp> is my org name.Tarkany
@Tarkany If this is managed by GitHub (meaning it is not on premise, managed by your company), then you cannot write a post-receive hook, since it is a server-side hook, as described in git-scm.com/book/en/v2/…. You might have a look to developer.github.com/webhooks.Parks
oh I think I understood. Webhooks is probably what I need. Thanks for pointing me in the right direction!Tarkany
I have a similar question here where I am trying to use git hook to merge branch. I wanted to see if you can help me out.Dactylo

© 2022 - 2024 — McMap. All rights reserved.