jenkins_windows(七):SVN自动触发项目构建的配置
在前一篇博客中详细介绍了如何通过github的webkook功能实现push到github代码仓库后,自动触发jenkins项目构建的流程,实际工作中还有用svn进行代码管理,本文具体介绍一下svn触发项目构建的具体配置,原理和github类似。
简介
svn触发jenkins任务的构建有以下三种方式:
1.jenkins定时轮询触发
2.客户端创建的webhook触发
3.服务端创建的webhook触发
第一种属于被动通知jenkins,后两种属于主动通知,当svn代码仓库有变动时主动通知jenkins。第3种实际操作更可行,我们重点介绍。
流程
当有代码commit到svn服务器后,自动执行配置好的Post-Commit脚本,通过HTTP请求调用jenkins触发的接口。这种方式需要svn服务端和jenkins同时配置。
jenkins中生成用户授权的Token
1.单击系统管理
2.将页面拉至最下方,单击管理用户
3.单击用户名的设置按钮
4.单击添加新Token
5.输入任意的值单击生成token
6.保存新生成的token
id:admin
token:11fac319f4f43fb6a74262b31c4916bd9c
7.在jenkins项目配置中生成项目授权Token
勾选远程触发:
勾选:轮询SCM,日程表为空,表示被post-commit钩子触发执行,如下:
SVN服务端配置webhook
新建触发脚本
切换到svn服务器仓库的目录,比如我的是:D:\Repositories\LoginRegister\hooks
新建名为post-commit-hook-jenkins.vbs的脚本,用来真正调用jenkins任务构建的接口,此脚本由post-commit.bat调用,内容如下:
repos = WScript.Arguments.Item(0)
Wscript.Echo "repos="&repos
rev = WScript.Arguments.Item(1)
Wscript.Echo "rev="&rev
svnlook = WScript.Arguments.Item(2)
Wscript.Echo "svnlook="&svnlook
jenkins = WScript.Arguments.Item(3)
Wscript.Echo "jenkins="&jenkins
authorization = WScript.Arguments.Item(4)
Wscript.Echo "authorization="&authorization
Set shell = WScript.CreateObject("WScript.Shell")
Set uuidExec = shell.Exec(svnlook & " uuid " & repos)
Do Until uuidExec.StdOut.AtEndOfStream
uuid = uuidExec.StdOut.ReadLine()
Loop
Wscript.Echo "uuid=" & uuid
Set changedExec = shell.Exec(svnlook & " changed --revision " & rev & " " & repos)
Do Until changedExec.StdOut.AtEndOfStream
changed = changed + changedExec.StdOut.ReadLine() + Chr(10)
Loop
Wscript.Echo "changed=" & changed
url = jenkins + "crumbIssuer/api/xml?
xpath=concat(//crumbRequestField,"":"",//crumb)"
Wscript.Echo "url="&url
Set http = CreateObject("Microsoft.XMLHTTP")
http.open "GET", url, False
http.setRequestHeader "Content-Type", "text/plain;charset=UTF-8"
if not authorization = "" then
http.setRequestHeader "Authorization", "Basic " + authorization
end if
http.send
crumb = null
if http.status = 200 then
crumb = split(http.responseText,":")
end if
url = jenkins + "subversion/" + uuid + "/notifyCommit?rev=" + rev
Wscript.Echo url
Set http = CreateObject("Microsoft.XMLHTTP")
http.open "POST", url, False
http.setRequestHeader "Content-Type", "text/plain;charset=UTF-8"
if not authorization = "" then
http.setRequestHeader "Authorization", "Basic " + authorization
end if
if not isnull(crumb) then
http.setRequestHeader crumb(0),crumb(1)
end if
http.send changed
if http.status <> 200 then
Wscript.Echo "Error. HTTP Status: " & http.status & ". Body: " & http.responseText
end if
说明:一般只需要根据自己实际项目的url行(url = jenkins + “?token=” + token),表示你要调用的jenkins项目中触发构建的url,其它不用修改
单击左下角的windows按钮,找到VisualSVN Server Manager,单击
选中要触发jenkins构建的仓库,右键, 单击Properites
切换到hooks标签页,单击Post-commit hook
输入脚本
接收svn服务端提交的参数,并将参数传递给我们第1步生成的post-commit-hook-jenkins.vbs脚本并执行,触发jenkins任务的构建。
脚本内容:
SET REPOS=%1
SET REV=%2
SET CSCRIPT=%windir%\system32\cscript.exe
SET VBSCRIPT=D:\Repositories\LoginRegister\hooks\post-commit-hook-jenkins.vbs
SET SVNLOOK=C:\Program Files\VisualSVN Server\bin\svnlook.exe
SET JENKINS=http://127.0.0.1:8080/jenkins/
REM AUTHORIZATION: Set to “” for anonymous acceess
REM AUTHORIZATION: Set to encoded Base64 string, generated from “user_id:api_token”
REM found on Jenkins under “user/configure/API token”
REM User needs “Job/Read” permission on Jenkins
REM AUTHORIZATION=Base64(admin:11fac319f4f43fb6a74262b31c4916bd9c)
SET AUTHORIZATION=“YWRtaW46MTFmYWMzMTlmNGY0M2ZiNmE3NDI2MmIzMWM0OTE2YmQ5Yw==”
“%CSCRIPT%” “%VBSCRIPT%” “%REPOS%” “%REV%” “%SVNLOOK%” %JENKINS% %AUTHORIZATION%
pause
说明:
1.根据电脑实际的安装目录配置CSCRIPT,VBSCRIPT,SVNLOOK以及JENKINS所在的路径,2.AUTHORIZATION是base64编码,由jenkins中生成的id和token一起计算生成的
单击确定
查看结果
修改代码提交到SVN,查看自动触发构建结果
更多推荐
所有评论(0)