git - 如何配置 Git post commit 钩子(Hook)

标签 git jenkins hook commit

如何从 Jenkins 远程触发构建?
如何配置 Git post commit 钩子(Hook)?

我的要求是,每当在 Git 存储库中为特定项目进行更改时,它都会自动启动 Jenkins 为该项目构建。

在 Jenkins 触发器构建部分,我选择了远程触发器构建。
.git 目录下,hooks 目录是我们必须配置提交后文件的地方。
我很困惑如何从那里触发构建(我知道某些部分我们应该使用 curl 命令)。


curl cmbuild.aln.com/jenkins/view/project name/job/myproject/buildwithparameters?Branch=feat-con

我已将此命令放在我的 git 服务器 Hook 目录中(提交后 Hook )。
每当存储库中发生更改时,它都会运行自动构建。

我想检查变更集中是否至少有一个 java 文件应该开始构建。
假设开发人员仅更改了不应启动构建的 xml 文件或属性文件。
xml 一起,假设 .java 文件存在,构建应该开始。

最佳答案

如“Polling must die: triggering Jenkins builds from a git hook”中所述,您可以通知 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.

作为 mentioned here,请确保为您的 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

为了强调最后一点,ptha 添加了 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 url parameter should match exactly what you have in Repository URL of your Jenkins job.
When copying examples I left out the protocol, in our case ssh://, and it didn't work.


你也可以像“Push based builds using Jenkins and GIT”一样使用一个简单的接收后钩子(Hook)

#!/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).

然而,这是一个项目特定的脚本,作者提到了一种泛化它的方法。
第一个解决方案更简单,因为它不依赖于身份验证或特定项目。


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.

基本上,您的构建脚本可以:

  • 在第一次通话时添加“构建”注释(参见 git notes)
  • 在随后的调用中,获取构建候选分支的 HEADgit notes 'build' ( git show refs/notes/build): git diff --name-only SHA_build HEAD.
  • 您的脚本可以解析该列表并决定是否需要继续构建。
  • 在任何情况下,创建/移动你的 git notes 'build' 到 HEAD

2016 年 5 月:cwhsu 指出 in the comments 以下可能的 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

http://i.imgur.com/IolrOOj.png


2016 年 6 月,polaretto 指出 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 name

repository=${PWD%/hooks}; 
repository=${repository##*/} 

and then use it like:

curl $JENKINS_URL/git/notifyCommit?url=$GIT_URL/$repository

关于git - 如何配置 Git post commit 钩子(Hook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794568/

相关文章:

Jenkins 按标签锁定

git - 查找在提交之间发生更改的目录

Cordova Hook 顺序

c# SpecFlow BeforeScenario 钩子(Hook)

java - 我可以用 Jython/Python 扩展 Jenkins 吗

c++ - DirectX 3(三)如何将信息呈现给显示器?

git 识别两个分支最近的交集

git:如何在多台 PC 之间正确共享本地副本

git - 查看 Git 存储库的大小

git - 有没有办法在 git rebase 中自动丢弃修改后的提交?