如何配置 Git 提交后钩子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12794568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Git post commit hook
提问by phanikumar Raja
How to trigger a build remotely from Jenkins?
How to configure Git post commit hook?
如何从 Jenkins 远程触发构建?
如何配置 Git 提交后钩子?
My requirement is whenever changes are made in the Git repository for a particular project it will automatically start Jenkins build for that project.
我的要求是每当在 Git 存储库中为特定项目进行更改时,它将自动启动该项目的 Jenkins 构建。
In Jenkins trigger build section I selected trigger build remotely.
In .git
directory, hooks directory is there in that we have to configure post commit file.
I am confusing how to trigger a build from there (I know some part we should use curl command).
在 Jenkins 触发器构建部分,我选择了远程触发器构建。
在.git
目录中,hooks 目录在那里,我们必须配置提交后文件。
我很困惑如何从那里触发构建(我知道我们应该使用 curl 命令的某些部分)。
curl cmbuild.aln.com/jenkins/view/project name/job/myproject/buildwithparameters?Branch=feat-con
I have placed this command in my git server hooks directory (post commit hook).
Whenever the changes happen in repository it is running automate build.
我已将此命令放在我的 git 服务器钩子目录中(提交后钩子)。
每当存储库中发生更改时,它都会运行自动构建。
I want to check in changeset whether in at least one java file is there the build should start.
Suppose the developers changed only xml files or property files the build should not start.
Along with xml
, suppose the .java
files is there the build should start.
我想检查变更集是否应该在至少一个 java 文件中开始构建。
假设开发人员仅更改了不应启动构建的 xml 文件或属性文件。
随着xml
,假设.java
文件在那里,构建应该开始。
回答by VonC
As mentioned in "Polling must die: triggering Jenkins builds from a git hook", you can notify Jenkins of a new commit:
正如“轮询必须死:从 git hook 触发 Jenkins 构建”中所述,您可以通知 Jenkins 新提交:
With the latest Git plugin 1.1.14(that I just release now), you can now do this more >easily by simply executing the following command:
curl http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>
This will scan all the jobs that's configured to check out the specified URL, and if they are also configured with polling, it'll immediately trigger the polling (and if that finds a change worth a build, a build will be triggered in turn.)
This allows a script to remain the same when jobs come and go in Jenkins.
Or if you have multiple repositories under a single repository host application (such as Gitosis), you can share a single post-receive hook script with all the repositories. Finally, this URL doesn't require authentication even for secured Jenkins, because the server doesn't directly use anything that the client is sending. It runs polling to verify that there is a change, before it actually starts a build.
使用最新的Git 插件 1.1.14(我现在刚刚发布),您现在可以通过简单地执行以下命令更轻松地做到这一点:
curl http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>
这将扫描所有配置为检查指定 URL 的作业,如果它们也配置了轮询,它将立即触发轮询(如果发现值得构建的更改,将依次触发构建。 )
这允许脚本在 Jenkins 中来来去去时保持不变。
或者,如果您在单个存储库宿主应用程序(例如 Gitosis)下有多个存储库,您可以与所有存储库共享一个 post-receive hook 脚本。最后,即使对于受保护的 Jenkins,此 URL 也不需要身份验证,因为服务器不直接使用客户端发送的任何内容。在实际开始构建之前,它运行轮询以验证是否有更改。
As mentioned here, make sure to use the right address for your Jenkins server:
如此处所述,请确保为您的 Jenkins 服务器使用正确的地址:
since we're running Jenkins as standalone Webserver on port 8080 the URL should have been without the
/jenkins
, like this:http://jenkins:8080/git/notifyCommit?url=git@gitserver:tools/common.git
由于我们在端口 8080 上将 Jenkins 作为独立 Web 服务器运行,因此 URL 应该没有
/jenkins
,如下所示:http://jenkins:8080/git/notifyCommit?url=git@gitserver:tools/common.git
To reinforce that last point, pthaadds in the comments:
It may be obvious, but I had issues with:
curl http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>.
The urlparameter should match exactly what you have in Repository URLof your Jenkins job.
When copying examples I left out the protocol, in our casessh://
, and it didn't work.
这可能很明显,但我遇到了以下问题:
curl http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>.
该URL参数应该完全匹配你所拥有的资源库网址您的詹金斯工作。
在复制示例时,我遗漏了协议,在我们的例子中ssh://
,它不起作用。
You can also use a simple post-receive hook like in "Push based builds using Jenkins and GIT"
您还可以使用一个简单的 post-receive 钩子,如“使用 Jenkins 和 GIT 的基于推送的构建”
#!/bin/bash
/usr/bin/curl --user USERNAME:PASS -s \
http://jenkinsci/job/PROJECTNAME/build?token=1qaz2wsx
Configure your Jenkins job to be able to “Trigger builds remotely” and use an authentication token (
1qaz2wsx
in this example).
将您的 Jenkins 作业配置为能够“远程触发构建”并使用身份验证令牌(
1qaz2wsx
在本例中)。
However, this is a project-specific script, and the author mentions a way to generalize it.
The first solution is easier as it doesn't depend on authentication or a specific project.
然而,这是一个项目特定的脚本,作者提到了一种概括它的方法。
第一个解决方案更简单,因为它不依赖于身份验证或特定项目。
I want to check in change set whether at least one java file is there the build should start.
Suppose the developers changed only XML files or property files, then the build should not start.
我想检查更改集是否至少有一个 java 文件应该开始构建。
假设开发人员只更改了 XML 文件或属性文件,那么构建不应开始。
Basically, your build script can:
基本上,您的构建脚本可以:
- put a 'build' notes (see
git notes
) on the first call - on the subsequent calls, grab the list of commits between
HEAD
of your branch candidate for build and the commit referenced by thegit notes
'build' (git show refs/notes/build
):git diff --name-only SHA_build HEAD
. - your script can parse that list and decide if it needs to go on with the build.
- in any case, create/move your
git notes
'build
' toHEAD
.
git notes
在第一次调用时添加“构建”注释(请参阅)- 在随后的调用,抢之间提交的名单
HEAD
你的分支候补为构建和提交由引用的git notes
“构建”( )。git show refs/notes/build
git diff --name-only SHA_build HEAD
- 您的脚本可以解析该列表并决定是否需要继续构建。
- 在任何情况下,创建/移动您的
git notes
'build
' 到HEAD
.
May 2016: cwhsupoints out in the commentsthe following possible url:
you could just use
curl --user USER:PWD http://JENKINS_SERVER/job/JOB_NAME/build?token=YOUR_TOKEN
if you set trigger config in your item
curl --user USER:PWD http://JENKINS_SERVER/job/JOB_NAME/build?token=YOUR_TOKEN
如果您在项目中设置触发器配置,则可以使用
June 2016, polarettopoints out in the comments:
I wanted to add that with just a little of shell scripting you can avoid manual url configuration, especially if you have many repositories under a common directory.
For example I used these parameter expansions to get the repo namerepository=${PWD%/hooks}; repository=${repository##*/}
and then use it like:
curl $JENKINS_URL/git/notifyCommit?url=$GIT_URL/$repository
我想补充一点,只需编写一点 shell 脚本,您就可以避免手动 url 配置,特别是如果您在一个公共目录下有许多存储库。
例如,我使用这些参数扩展来获取 repo 名称repository=${PWD%/hooks}; repository=${repository##*/}
然后像这样使用它:
curl $JENKINS_URL/git/notifyCommit?url=$GIT_URL/$repository
回答by Nav
Hope this helps: http://nrecursions.blogspot.in/2014/02/how-to-trigger-jenkins-build-on-git.html
希望这有帮助:http: //nrecursions.blogspot.in/2014/02/how-to-trigger-jenkins-build-on-git.html
It's just a matter of using curl
to trigger a Jenkins job using the git hooks provided by git.
The command
这只是curl
使用 git 提供的 git hooks 来触发 Jenkins 作业的问题。
命令
curl http://localhost:8080/job/someJob/build?delay=0sec
can run a Jenkins job, where someJob
is the name of the Jenkins job.
可以运行 Jenkins 作业,Jenkins 作业someJob
的名称在哪里。
Search for the hooks
folder in your hidden .git folder. Rename the post-commit.sample
file to post-commit
. Open it with Notepad, remove the : Nothing
line and paste the above command into it.
hooks
在隐藏的 .git 文件夹中搜索该文件夹。将post-commit.sample
文件重命名为post-commit
. 用记事本打开它,删除该: Nothing
行并将上述命令粘贴到其中。
That's it. Whenever you do a commit, Git will trigger the post-commit commands defined in the file.
就是这样。每当您进行提交时,Git 都会触发文件中定义的提交后命令。
回答by anhoppe
I want to add to the answers above that it becomes a little more difficult if Jenkins authorization is enabled.
我想补充上面的答案,如果启用 Jenkins 授权,它会变得更加困难。
After enabling it I got an error message that anonymous user needs read permission.
启用它后,我收到一条错误消息,指出匿名用户需要读取权限。
I saw two possible solutions:
我看到了两种可能的解决方案:
1: Changing my hook to:
1:将我的钩子改为:
curl --user name:passwd -s http://domain?token=whatevertokenuhave
2: setting project based authorization.
2:设置基于项目的授权。
The former solutions has the disadvantage that I had to expose my passwd in the hook file. Unacceptable in my case.
前一种解决方案的缺点是我必须在钩子文件中公开我的密码。在我的情况下是不可接受的。
The second works for me. In the global auth settings I had to enable Overall>Read for Anonymous user. In the project I wanted to trigger I had to enable Job>Build and Job>Read for Anonymous.
第二个对我有用。在全局身份验证设置中,我必须为匿名用户启用整体>阅读。在我想要触发的项目中,我必须启用 Job>Build 和 Job>Read for Anonymous。
This is still not a perfect solution because now you can see the project in Jenkins without login. There might be an even better solution using the former approach with http login but I haven't figured it out.
这仍然不是一个完美的解决方案,因为现在您无需登录即可在 Jenkins 中查看该项目。使用 http 登录的前一种方法可能有更好的解决方案,但我还没有弄清楚。
回答by Zitrax
As the previous answer did show an example of how the full hook might look like here is the code of my working post-receive hook:
正如前面的答案确实显示了完整挂钩的示例,这里是我的工作后接收挂钩的代码:
#!/usr/bin/python
import sys
from subprocess import call
if __name__ == '__main__':
for line in sys.stdin.xreadlines():
old, new, ref = line.strip().split(' ')
if ref == 'refs/heads/master':
print "=============================================="
print "Pushing to master. Triggering jenkins. "
print "=============================================="
sys.stdout.flush()
call(["curl", "-sS", "http://jenkinsserver/git/notifyCommit?url=ssh://user@gitserver/var/git/repo.git"])
In this case I trigger jenkins jobs only when pushing to master and not other branches.
在这种情况下,我仅在推送到 master 而不是其他分支时触发 jenkins 作业。