Search code examples
gitgithooks

Git hooks do not run


I want to run post-receive hook to notify Jenkins to start a new build, but I cannot make Git hooks to run automatically.

  1. I tried example hooks in .git/hooks repository, by removing .sample but none of them run after the actions they are supposed to do so.
  2. I have setup hooks path to .git/hooks using git config --local core.hooksPath .git/hooks command, but it did not help.
  3. I made sure hooks scripts are executable (chmod ug+x <script>).

Is there some special setup required to run Git hooks automatically?


Solution

  • The post-receive hook is a server-side hook. It is invoked when data is pushed to the repository. You can simulate the process with two local repositories, one as the client and the other as the server.

    git init --bare server
    # copy post-receive to server/hooks and make sure it's executable
    
    git init client
    cd client
    touch a.txt
    git add a.txt
    git commit
    git push ../server master
    

    In practice, the server-side repository is usually at another machine, and we use http/https-protocol or ssh-protocol remote to communicate with it. The hook should be installed in the server-side repository.

    Then there is another issue. In most cases, the server-side repositories are managed by a hosting service like Github, Gitlab, Gitee or other services. They may support the standard git hooks like post-receive and may not. They have their own hooks, like Github webhooks, different from git hooks. For these hooks, reference the documentation to find how to configure and use them.