3

I am implementing post commit hook in svn repo to trigger jenkins build but getting one exception which is I think in the commit.vb file. I know this is so simple question but I haven't work on vb so no idea about. Following this tutorial - https://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin . Also pls help me out in specifying specific job which I need to trigger. I am assuming that with this configuration all the jobs in the jenkins will trigger.

post-commit.bat

SET REPOS=%1
SET REV=%2
SET CSCRIPT=%windir%\system32\cscript.exe
SET VBSCRIPT=C:\Repositories\commit.vbs
SET SVNLOOK=C:\Program Files\VisualSVN Server\bin\svnlook.exe
SET JENKINS=http://localhost:8080/jenkins
"%CSCRIPT%" "%VBSCRIPT%" "%REPOS%" %2 "%SVNLOOK%" %JENKINS%
@pause

commit.vbs

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

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)"
Set http = CreateObject("Microsoft.XMLHTTP")
http.open "GET", url, False
http.setRequestHeader "Content-Type", "text/plain;charset=UTF-8"
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 isnull(crumb) then 
  http.setRequestHeader crumb(0),crumb(1)
  http.send changed
  if http.status <> 200 then
    Wscript.Echo "Error. HTTP Status: " & http.status & ". Body: " & http.responseText
  end if
end if
user692942
  • 16,398
  • 7
  • 76
  • 175
  • `"%CSCRIPT%" "%VBSCRIPT%" "%REPOS%" "%REV%" "%SVNLOOK%" "%JENKINS%"` – Ansgar Wiechers Dec 21 '16 at 11:51
  • This is simple to debug, just a write aq `For` loop to check your `WScript.Arguments` collection, my guess from the error is your not getting all the arguments you expect. – user692942 Dec 21 '16 at 11:51
  • @AnsgarWiechers I didn't think `WScript.Arguments` distinguishes between quoted and unquoted arguments? Plus `%REV%` is just `%2` anyway so why wouldn't that work? – user692942 Dec 21 '16 at 11:53
  • @All - no idea about vbscript :( – Vaibhav Dalela Dec 21 '16 at 12:11
  • 1
    @Lankymart If `%2` is empty the arguments `"%SVNLOOK%"` and `%JENKINS%` are effectively shifted one to the left. Putting the arguments in double quotes avoids this, because empty parameters/variables become empty strings instead of disappearing from the statement entirely. I used `%REV%` just because `%2` was already assigned to it. Using `"%~2"` would have the same effect. – Ansgar Wiechers Dec 21 '16 at 13:18
  • @AnsgarWiechers good point didn't think about that, I wasn't disagreeing just wondered what switching from `%2` to `"%REV%"` did, you've answered that. Thanks. – user692942 Dec 21 '16 at 13:30

1 Answers1

4

The error is coming from VBScript the two arguments tell you what line and column have triggered the error, in this case, line 4 and column 1.

So the issue is likely (if this is the whole source script)

jenkins = WScript.Arguments.Item(3)

The Subscript Out of Range error roughly translates to, the current array index you are passing is past the bounds of the array.

So the likelihood is, there is no argument 4 being passed (VBScript arrays start are 0, so 3 is in fact 4).

You can test this yourself with a little alteration to the script to debug the WScript.Arguments collection. Just add the below code to the top of your script.

Dim i
For i = 0 To WScript.Arguments.Count - 1
  WScript.Echo "Index " & i & " = " & WScript.Arguments.Item(i)
Next

It will loop through the list WScript.Arguments and output what is contained in each.

Testing with

cscript //nologo "test62.vbs" "SVNRepo" "Rev2" "C:\Program Files\VisualSVN Server\bin\svnlook.exe" http://localhost:8080/jenkins

Output:

Index 0 = SVNRepo
Index 1 = Rev2
Index 2 = C:\Program Files\VisualSVN Server\bin\svnlook.exe
Index 3 = http://localhost:8080/jenkins
user692942
  • 16,398
  • 7
  • 76
  • 175
  • still not getting the solution. pls help me out – Vaibhav Dalela Dec 21 '16 at 14:22
  • @VaibhavDalela look it's not a solution, I'm trying to help you debug the problem. Did you manage to add that code and run it again, what was the output? It should help you identify if any arguments are not being passed correctly. – user692942 Dec 21 '16 at 14:24
  • @VaibhavDalela In that case what is happening as [@ansgar-wiechers](http://stackoverflow.com/users/1630171/ansgar-wiechers) describes [here in the comments](http://stackoverflow.com/questions/41261499/subscript-out-of-range-4-1/41263183?noredirect=1#comment69728074_41261499) is the arguments are shifted to the left so `WScript.Arguments.Item(3)` doesn't exist. In that comment he suggests using `"%CSCRIPT%" "%VBSCRIPT%" "%REPOS%" "%REV%" "%SVNLOOK%" "%JENKINS%"` in the batch script so even if `%2` is null it will return as an empty string in the arguments. – user692942 Dec 21 '16 at 14:58
  • @VaibhavDalela Try that then run the debug code again what is the output? – user692942 Dec 21 '16 at 14:58
  • 1
    Yes, it working now, thanks but my job is not executing in jenkins... :-( – Vaibhav Dalela Dec 26 '16 at 15:56