Post-Commit Hook to trigger automatic Jenkins Build

假装没事ソ 提交于 2019-11-30 03:56:16

The article you linked says

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.

and

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?

I tried to get the svn plugin examples to work, but no luck. Instead I used the build token root plugin and this works without having to poll.

https://wiki.jenkins-ci.org/display/JENKINS/Build+Token+Root+Plugin

Build Triggers > Trigger builds remotely option > give it a token

On VisualSVN server add this to the post-commit hook:

SET CSCRIPT=%windir%\system32\cscript.exe
SET VBSCRIPT=C:\Repositories\post-commit-hook-jenkins.vbs
"%CSCRIPT%" "%VBSCRIPT%" "MyJobName" "MyTokenFromBuildTrigger"

For post-commit-hook-jenkins.vbs:

Set args = WScript.Arguments
JobName = args.Item(0)
Token = args.Item(1)

'URL to open....
sUrl = "http://MyJenkinsServer.MyCompany.com/buildByToken/build?job=" + JobName + "&token=" + Token
'POST Request to send.
sRequest = ""

HTTPPost sUrl, sRequest

Function HTTPPost(sUrl, sRequest)
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
  oHTTP.open "POST", sUrl,false
  oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.send sRequest
  HTTPPost = oHTTP.responseText
 End Function 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!