如何配置 Git 提交后钩子 [英] How to configure Git post commit hook

查看:94
本文介绍了如何配置 Git 提交后钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Jenkins 远程触发构建?
如何配置 Git 提交后钩子?

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

在 Jenkins 触发器构建部分,我选择了远程触发器构建.
.git目录下,hooks目录在那里,我们需要配置post commit文件.
我很困惑如何从那里触发构建(我知道我们应该使用 curl 命令的某些部分).

<小时>

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

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

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

解决方案

<小时>

2016 年 6 月,polaretto 指出 在评论中:

<块引用>

我想补充一点,只需编写少量 shell 脚本,您就可以避免手动 url 配置,特别是如果您在一个公共目录下有许多存储库.
例如我使用这些参数扩展来获取 repo 名称

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

然后像这样使用它:

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

How to trigger a build remotely from Jenkins?
How to configure Git post commit hook?

My requirement is whenever changes are made in the Git repository for a particular project it will automatically start Jenkins build for that project.

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).


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.

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.

解决方案

As mentioned in "Polling must die: triggering Jenkins builds from a git hook", you can notify Jenkins of a new commit:

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.

As mentioned here, make sure to use the right address for your Jenkins server:

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

To reinforce that last point, ptha adds 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.


You can also use a simple post-receive hook like in "Push based builds using Jenkins and 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).

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.

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 the git 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' to HEAD.

May 2016: cwhsu points out in the comments the 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


June 2016, polaretto points 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 name

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

and then use it like:

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

这篇关于如何配置 Git 提交后钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆