在SVN提交后触发jenkins构建

时间:2014-05-13 12:08:00

标签: svn batch-file vbscript jenkins

我对SVN提交后的Jenkins构建有一个小问题。 我已经配置了一个jenkins.vb脚本在post-commit钩子之后调用,但是在提交后没有触发构建。

当我通过命令行传递Repository路径和版本#时,会触发构建。

任何人都可以帮助我摆脱这个问题。请在下面找到post-commit hook和jenkins.vbs文件 Jenkins Url触发构建:http:\ server-name \ job \ branchname \ build?delay = 2sec

发布后挂钩:

@echo on
rem POST-COMMIT HOOK
set REPOS=%1
set REV=%2
SET CSCRIPT=C:\WINNT\system32\cscript.exe
SET VBSCRIPT=D:\SVN\hooks\post-commit-hook-jenkins.vbs
SET SVNLOOK=C:\Subversion\bin\svnlook.exe %VBSCRIPT% %REPOS% %REV% 

Jenkins.vbs

repos   = WScript.Arguments.Item(0)
rev     = WScript.Arguments.Item(1)
svnlook = "C:\Subversion\bin\svnlook.exe"
jenkinsBaseUrl = **JenkinsURL** (unable to paste the URL would be in this format "URL:8080")
Set shell = WScript.CreateObject("WScript.Shell")
Set uuidExec = shell.Exec(svnlook & " uuid " & repos)
Do Until uuidExec.StdOut.AtEndOfStream
  uuid = uuidExec.StdOut.ReadLine()
Loop
' Create a list of all branches that have been modified.
Dim branches()
ReDim Preserve branches(-1)
Set changedExec = shell.Exec(svnlook & " changed --revision " & rev & " " & repos)
Do Until changedExec.StdOut.AtEndOfStream
  fileChanged = changedExec.StdOut.ReadLine()
  ' Get the branch names if any
  If InStr(fileChanged, "source/branches/") Then
    branchStartIndex = InStr(fileChanged, "source/branches/") + 38
    branch = Mid(fileChanged, branchStartIndex)
    branchEndIndex = InStr(branch, "/") - 1
    branch = Left(branch, branchEndIndex)
    branchFound = False
    For count = 0 to UBound(branches)
        If (CStr(branches(count)) = CStr(branch)) Then
            branchFound = True
        End If
    Next

    ' Add the branch if it was not previously added
    If branchFound = False Then
        ReDim Preserve branches(UBound(branches) + 1)
        branches(UBound(branches)) = branch
    End If
  End If
Loop
' the above code gives the branch name in which the check-in has been done (THE ABOVE CODE WORKS FINE)
' Fire out a build for every branch that has changed
For count = LBound(branches) to UBound(branches)
    url = jenkinsBaseUrl + "/job/" & branches(count) & "/build?delay=0sec"
    Set http = CreateObject("Microsoft.XMLHTTP")
    http.open "POST", url, False
    http.setRequestHeader "Content-Type", "text/plain;charset=UTF-8"
    http.send changed
Next

1 个答案:

答案 0 :(得分:1)

您需要在“源代码管理”中指定要在“源代码管理”中使用的存储库的URL(具有特定分支的完整路径),如评论中所述。

如果需要,您可以使用轮询,它将以指定的定期间隔检查为新更改指定的所有路径,并在检测到新版本时触发新版本。

使用post-commit钩子同样有效,甚至可能是必要的,例如如果您不想经常轮询存储库以进行更改(可能因为更改很少),或者您需要在源代码管理下指定项目所需的多个路径,但您不想触发构建其中一些变化。

Subversion Plugin的wiki页面为post-commit钩子提供了一些很好的例子,包括一个VBScript - 注意它不像你在脚本中那样使用构建触发器url,但是通知了jenkins服务器对特定svn存储库的提交。