后提交钩子触发自动Jenkins Build [英] Post-Commit Hook to trigger automatic Jenkins Build

查看:3859
本文介绍了后提交钩子触发自动Jenkins Build的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多类似的帖子,但我还没有找到一个解决方案,其他帖子中提出的建议和解决方案并不像我所看到的那样。

I know that there are many similar postings, but I have not found a solution, and the advice and solutions presented in the other posts don't quite jive with what I'm seeing.

这个场景非常简单:我在Eclipse中有一个项目,当我从该项目更改到我们的Subversion服务器(即VisualSVN Server 2.5.3)时,我想让Jenkins连续集成服务器(即Jenkins 1.546)来接收此更改并启动新构建。我不想从Jenkins投票。

The scenario is pretty darn simple: I have a project in Eclipse, and when I check-in changes from that project to our Subversion server (i.e., VisualSVN Server 2.5.3), I want our Jenkins continuous integration server (i.e., Jenkins 1.546) to pick up this change and kick off a new build. I do not want to poll from Jenkins.

我一直主要是按照本文。这是我的后提交钩子脚本:

I've been mostly following the steps in this article. Here's my post-commit hook script:

repos   = WScript.Arguments.Item(0)
rev     = WScript.Arguments.Item(1)
svnlook = WScript.Arguments.Item(2)
jenkins = WScript.Arguments.Item(3)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\Program Files (x86)\VisualSVN Server\log.txt")

objFile.Writeline "repos=" & repos
objFile.Writeline "rev=" & rev
objFile.Writeline "svnlook=" & svnlook
objFile.Writeline "jenkins=" & jenkins

Set shell = WScript.CreateObject("WScript.Shell")

Set uuidExec = shell.Exec(svnlook & " uuid " & repos)
Do Until uuidExec.StdOut.AtEndOfStream
  uuid = uuidExec.StdOut.ReadLine()
Loop

objFile.Writeline "uuid=" & uuid

Set changedExec = shell.Exec(svnlook & " changed --revision " & rev & " " & repos)
Do Until changedExec.StdOut.AtEndOfStream
  changed = changed + changedExec.StdOut.ReadLine() + Chr(10)
Loop
objFile.Writeline "changed=" & changed

url = jenkins + "crumbIssuer/api/xml?xpath=concat(//crumbRequestField,"":"",//crumb)"
Set http = CreateObject("Microsoft.XMLHTTP")
http.open "GET", url, False
http.setRequestHeader "Content-Type", "text/plain;charset=UTF-8"
http.send
crumb = null

objFile.Writeline "rev url=" & url
objFile.Writeline "http.status=" & http.status
objFile.Writeline "http.responseText=" & http.responseText

if http.status = 200 then
  crumb = split(http.responseText,":")
end if

url = jenkins + "subversion/" + uuid + "/notifyCommit?rev=" + rev + "&token=pinkfloyd65"
objFile.Writeline "url=" & url

if not isnull(crumb) then 
    objFile.Writeline "crumb(0)=" & crumb(0)
    objFile.Writeline "crumb(1)=" & crumb(1)
end if

if isnull(crumb) then 
    objFile.Writeline "crumb=null"
end if

Set http = CreateObject("Microsoft.XMLHTTP")
http.open "POST", url, False
http.setRequestHeader "Content-Type", "text/plain;charset=UTF-8"
if not isnull(crumb) then 
  http.setRequestHeader crumb(0),crumb(1)
  http.send changed
  if http.status <> 200 then
    objFile.Writeline "Error. HTTP Status: " & http.status & ". Body: " & http.responseText
  end if

  if http.status = 200 then
    objFile.Writeline "HTTP Status: " & http.status & ".\n Body: " & http.responseText
  end if
end if

问题是,虽然上面的 POST 命令最终得到一个 200 响应,作业从不跳过。没有什么结果发生。好的,让我们检查Jenkins作业配置;也许我错过了一个设置或东西。那么,在Build Triggers部分,我检查了触发器远程构建(例如,从脚本)的选项,并且我提供了一个身份验证令牌。但是,该部分下方的方向与我一直在做的不同:

The issue is that, although the POST command above ends up getting a 200 response back, the job never kicks off. Nothing ends up happening. Alright, so let's check the Jenkins job configuration; maybe I'm missing a setting or something. Well, under the Build Triggers section, I've checked the option for "Trigger builds remotely (e.g., from scripts)" and I've supplied an authentication token as well. But, the directions underneath that section look different from what I've been doing:


使用以下URL远程触发构建: JENKINS_URL / job /< job-name> / build?token = TOKEN_NAME / buildWithParameters?token = TOKEN_NAME
(可选)附加& cause = Cause + Text 以提供将包括在记录的构建原因中的文本。

Use the following URL to trigger build remotely: JENKINS_URL/job/<job-name>/build?token=TOKEN_NAME or /buildWithParameters?token=TOKEN_NAME Optionally append &cause=Cause+Text to provide text that will be included in the recorded build cause.

所以,看起来似乎在我看到的指令集之间有一个增量,我不知道如何弥合这个差距。看起来很明显遵循Jenkins作业配置页面上的说明,除了我不知道如何获得作业名称,而不是UUID。

So, it seems like there's a delta between the sets of instructions I'm seeing, and I'm not sure how to bridge that gap. It seems pretty obvious to follow the instructions on the Jenkins job configuration page, except that I don't know how I'd get the job name, rather than the UUID.

另一件要注意的事情是我的存储库设置。由于CI服务器被许多组和部门使用,我认为我会是聪明的,并创建一个顶层存储库,以容纳我部门的项目。所以,我有一个设置类似:

Another thing to note is my repository setup. Since the CI server is used by many groups and departments, I thought I'd be all smart and create a top-level repository to house just my department's projects. So, I have a setup something like:

VisualSVN Server  
  -- Repositories  
     -- Project_A  
     -- Project_B  
     -- <my-department>  
        -- DepartmentProject_A  
        -- DepartmentProject_B  

结构增加了我的问题在这里,但我觉得我应该能够找出哪个特定的存储库任何更改来自。如果这是真的,那么我可以调整我的脚本使用作业名称,而不是UUID,然后按照我的CI服务器的配置页面上看到的显式说明。当我在vbs脚本中记录传入的 repos 变量时,它指向顶层部门存储库,而不是项目的子存储库(即 D:\< visual-svn-repos> \< my-department> ,而不是 D:\< visual-svn-repos> \< ;我的部门> \DepartmentProject_B )。

I'm wondering if the repository structure is adding to my issues here, but I feel like I should be able to find out which specific repository any changes came from. If that were true, then I could adjust my script to use the job name, rather than UUID, and then follow the explicit instructions seen on my CI server's configuration page. When I log the incoming repos variable in my vbs script, it points to the top-level department repository, rather than the project's child repository (i.e., D:\<visual-svn-repos>\<my-department> rather than D:\<visual-svn-repos>\<my-department>\DepartmentProject_B).

任何帮助将非常感谢,谢谢你们。

Any help would be greatly appreciated, thanks guys.

推荐答案

您链接的文章


Jenkins上的工作需要配置有SCM轮询选项到
受益于此行为。这是为了你可以有一些工作
,它们从来不会被post-commit钩子(在
$ REPOSITORY / hooks目录中)触发,比如释放相关的任务,通过
省略SCM轮询选项。配置的轮询可以有任何
调度(可能不频繁如每月或每年)。净效果
就好像轮询发生在他们通常的周期之外。

Jobs on Jenkins need to be configured with the SCM polling option to benefit from this behavior. This is so that you can have some jobs that are never triggered by the post-commit hook (in the $REPOSITORY/hooks directory), such as release related tasks, by omitting the SCM polling option. The configured polling can have any schedule (probably infrequent like monthly or yearly). The net effect is as if polling happens out of their usual cycles.


为了这个工作,你的Jenkins必须允许匿名读取访问
(具体来说,作业>读取访问)到系统。如果访问控制
到您的Jenkins是更加限制,您可能需要指定
用户名和密码,取决于您的身份验证是
配置。

For this to work, your Jenkins has to allow anonymous read access (specifically, "Job > Read" access) to the system. If access control to your Jenkins is more restrictive, you may need to specify the username and password, depending on how your authentication is configured.

您的服务器是否满足此限制?

Does you server meets this restrictions?

这篇关于后提交钩子触发自动Jenkins Build的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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