简体   繁体   English

理解 Git Hook - post-receive hook

[英]Understanding Git Hook - post-receive hook

I have written simple shell script to throw "success and failure message" and placed it under .git/hooks/ with all proper permissions.我编写了简单的 shell 脚本来抛出“成功和失败消息”,并将它放在 .git/hooks/ 下,并具有所有适当的权限。 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.我已经搜索过了,但无法理解。

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:要启用post-receive hook 脚本,请将一个文件放在 .git 目录的 hooks 子目录中,该文件同名(没有任何扩展名)并使其可执行:

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有关更多信息,请查看此文档: https : //git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

Example例子

Check this example ( a simple deploy ) GIT_PATH/hooks/post-receive :检查这个例子(一个简单的部署GIT_PATH/hooks/post-receive

#!/bin/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-来源: https : //gist.github.com/noelboss/3fe13927025b89757f8fb12e9066f2fa#file-

It needs to be called post-receive (no extension, no post-receive.sh for instance).它需要被称为post-receive (例如没有扩展名,没有post-receive.sh )。

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 ).如果它被放置(就像 OP 所做的那样)在 .git/hooks 文件夹中,并使其可执行,那么当你推送到那个 repo 时它会被调用(因为它是一个服务器钩子)。
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 ).对于像 GitHub 这样的远程 Git 托管服务器,您需要将该钩子实现为webhookGitHub 推送事件的侦听器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM