我有一个服务器端挂钩(更新后),它将在每次代码推送后运行。即使没有新的推动变化,有没有办法(或者可能是另一个钩子?)来运行它?
示例:
$ git push origin master
remote: Hook running....
$
$ git push origin master
Everything up-to-date
$
我希望它再次运行。这可能吗?
答案 0 :(得分:1)
创建pre-receive
挂钩,将其设为exit 1
,并从客户端git push origin master HEAD:non-existing-branch
创建。这将触发pre-receive
挂钩,即使master
没有更改。
要避免出现错误消息,请成功pre-receive
挂钩退出(exit 0
),然后从non-existing-branch
挂钩中手动删除post-receive
。但是,这会创建一个小时间窗口(当文件non-existing-branch
存在时),而从另一个客户端发起的git push ...
将不会运行挂钩。
答案 1 :(得分:0)
非常感谢@pts获得答案(今天使用我所有的选票,不能立即投票赞成:)
);对于那些(像我一样)在理解它如何正常运行时遇到轻微问题,这里有一个简单的命令行日志,演示如何使用non-existing-branch
来触发pre-receive
:
# create original repo:
$ cd /tmp
$ mkdir repotest_git
$ cd repotest_git/
$ git init
Initialized empty Git repository in /tmp/repotest_git/.git/
$ git config user.name "Your Name"
$ git config user.email you@example.com
# populate repo with 1 commit:
$ echo "test" > test.txt
$ git add test.txt
$ git commit -m "initial commit"
[master (root-commit) 2b60608] initial commit
1 file changed, 1 insertion(+)
create mode 100644 test.txt
$
# create remote "bare" repo; and add pre-receive hook there:
$ cd /tmp
$ git clone --bare repotest_git repotest-remote.git
Cloning into bare repository 'repotest-remote.git'...
done.
$ cd repotest-remote.git
$
$ cat > hooks/pre-receive <<"EOF"
#!/bin/sh
echo from pre-receive: HELLO_VAR [$HELLO_VAR]
exit 1
EOF
$ chmod +x hooks/pre-receive
# go back to original repo, add a remote reference
# to the newly created remote "bare" repo; update with pull:
$ cd /tmp
$ cd repotest_git
$ git remote add origin file:///tmp/repotest-remote.git
$ git pull origin master
From file:///tmp/repotest-remote
* branch master -> FETCH_HEAD
Already up-to-date.
# try testing the hook script;
# since we don't have any changes, Everything is
# up-to-date, and the pre-receive won't trigger:
$ git push
Everything up-to-date
# try testing with HEAD:non-existing-branch
# pre-receive gets triggered - and
# we can see variable is not there:
$ git push origin master HEAD:non-existing-branch
Total 0 (delta 0), reused 0 (delta 0)
remote: from pre-receive: HELLO_VAR []
To file:///tmp/repotest-remote.git
! [remote rejected] HEAD -> non-existing-branch (pre-receive hook declined)
error: failed to push some refs to 'file:///tmp/repotest-remote.git'
# try testing again, this time specify
# env. variable on command line....
# pre-receive gets triggered - and
# we can see variable is there:
$ HELLO_VAR=hello git push origin master HEAD:non-existing-branch
Total 0 (delta 0), reused 0 (delta 0)
remote: from pre-receive: HELLO_VAR [hello]
To file:///tmp/repotest-remote.git
! [remote rejected] HEAD -> non-existing-branch (pre-receive hook declined)
error: failed to push some refs to 'file:///tmp/repotest-remote.git'
在这里,对于本地工作,一切都按预期工作变量;但是,很明显,如果您的远程仓库位于服务器等之后,那么查看变量如何/最终结束可能会出现问题 - 因此仅调试该方面将非常有用,而无需对文件进行更改+ {{1}每次都是/ git add
/ commit
,只是为了触发脚本。