如何在 git push 后运行 bash 脚本

13

我想知道如何在将代码推送到Github之后在本地仓库中执行bash脚本。

该bash脚本位于文件夹的根目录中,根据调用位置的不同可能会被移动。我已经研究了Git钩子,但没有后置钩子,而且我对其他钩子也不是很了解。

我正在尝试在推送后调用Jenkins构建。我已经研究了通过Github上的post-receive网址等方式在推送后通知Jenkins,但似乎对我都不起作用。

这是我正在尝试运行的脚本:

#!/bin/bash/
java -jar jenkins-cli.jar -s http://localhost:8080/ build Test

谢谢! 瓦伦


1
没有这样的git钩子。请参考以下解决方法:https://dev59.com/0XI-5IYBdhLWcg3wj5BG - Ivan Zuzak
1
这是一篇关于 Git Hooks 的好文章: https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks - Rimian
可能是Local executing hook after a git push?的重复问题。 - KyleMit
1个回答

25

这相当容易。已经有一个脚本准备好运行了。您需要修改.git/hooks/post-commit以找到需要运行的脚本。

mv .git/hooks/post-commit.sample .git/hooks/post-commit
vim .git/hooks/post-commit

我在这里找到了:git-scm.com:Git Hooks

如果没有.git/hooks/post-commit.sample文件,没关系,只需从头开始创建.git/hooks/post-commit文件(记得使用chmod +x命令使脚本可执行),例如:

#!/bin/sh
echo look at me I am the post-commit script
pwd
# call the script you want

进行一次测试提交,然后您应该在正常的提交输出之前看到脚本的输出。


17
这不是为了提交推送,而是针对提交记录的 :| - Saifur Rahman Mohsin
如何在 post-commit 中调用我要运行的脚本? - munmunbb
1
OP 需要发布推送脚本,但这个答案是提交后执行脚本。 - ollydbg23
2
这个回答是针对提交后运行脚本,而不是推送后运行。请点踩。 - Vinicius Dias

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接