了解 Git Hook - post-receive hook

问题描述 投票:0回答:2

我编写了简单的 shell 脚本来抛出“成功和失败消息”,并将其放置在具有所有适当权限的 .git/hooks/ 下。我想将此脚本称为后接收。但脚本不起作用,运行脚本只是起作用,但作为接收后挂钩它不起作用。

他们是否遗漏了某些东西,或者我错误地理解了接收后挂钩。有人可以解释一下客户端和服务器端钩子以及如何执行它们吗?

我查过但没能理解。

git githooks git-post-receive
2个回答
12
投票

要启用

post-receive
挂钩脚本,请将文件放入 .git 目录的 hooks 子目录中,该文件同名(不带任何扩展名)并使其可执行:

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

有关更多信息,请查看此文档:https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

示例

检查这个示例(一个简单的部署

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

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


2
投票

它需要被称为

post-receive
(没有扩展名,例如没有
post-receive.sh
)。

如果它被放置(就像OP所做的那样)在.git/hooks文件夹中,并使其可执行,那么当您推送到该存储库时,它将被调用(因为它是服务器钩子)。
如果您要将它安装在您自己的本地存储库上,则不会调用它(除非您以某种方式推送到您自己的存储库,这似乎不太可能)。

对于像 GitHub 这样的远程 Git 托管服务器,您需要将该挂钩实现为 webhookGitHub 推送事件的监听器)。

© www.soinside.com 2019 - 2024. All rights reserved.