#!/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
2条答案
按热度按时间huus2vyu1#
要启用
post-receive
钩子脚本,请在.git目录的hooks文件夹中放置一个同名文件(没有任何扩展名),并使其可执行:字符串
欲了解更多信息,请查看此文档:https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
示例
看看这个例子(* 一个简单的部署 *)
GIT_PATH/hooks/post-receive
:型
来源:https://gist.github.com/noelboss/3fe13927025b89757f8fb12e9066f2fa#file-
8ftvxx2r2#
它需要被称为
post-receive
(没有扩展名,例如没有post-receive.sh
)。如果它被放置在.git/hooks文件夹中(就像OP所做的那样),并使其成为可执行文件,那么当您推送到该repo时,它将被调用(因为它是一个服务器钩子)。
如果你将它安装在你自己的本地仓库上,它将不会被调用(除非你以某种方式推到你自己的仓库,这似乎不太可能)。
对于像GitHub这样的远程Git托管服务器,您需要将该钩子实现为**webhook**(GitHub推送事件的侦听器)。