Need help running a Jenkins job from VBScript using JenkinsAPI

275 views
Skip to first unread message

D. C.

unread,
Jul 9, 2019, 1:54:43 PM7/9/19
to Jenkins Users
I have searched all over but haven't found a solution to this. The JenkinsAPI page doesn't provide information on using VBScript either.
I am trying to start a job in Jenkins from a classic ASP web page (so VBScript - code below).  I used wget from examples on the Jenkins API page to get the Jenkins crumb. The testjob is configured with "Trigger builds remotely" and I included the token in the URL string as specified in the examples on the Jenkins API page. I am unsure if the login information is setup correctly in my .setRequestHeader, but the error I am getting is "No valid crumb" even though I include the crumb in the header.  Can anyone tell me if the .setRequestHeaders are in the correct format or anything else that might help me get this to work?  I have also tried using curl to start the job, but that doesn't start the job, but it also doesn't return an error. From what I have found, curl should return an HTTP 201 response to indicate the job was started. I'm not getting that.  I would prefer to start the job from VBScript; curl was just for a test.

VBScript:
--------------
    Dim strJenkinsURL, HttpReq
    strJenkinsURL = "http://<jenkinsURL>/job/testjob/buildWithParameters?token=test&Description=sometext"

    Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
    HttpReq.Open "POST", strJenkinsURL, False
    HttpReq.setRequestHeader "UserName", "<jenkinsuser>"
    HttpReq.setRequestHeader "Password", "<userpassword>"
    HttpReq.setRequestHeader "Jenkins-Crumb", "<validcrumb>"
    HttpReq.Send
    Response.Write "<br>Status: "& HttpReq.Status & vbNewline
    Response.Write "<br>Response: "& HttpReq.responseText & vbNewline

Result
---------------------
Status: 403
Response: HTTP ERROR 403
Problem accessing /job/testjob/buildWithParameters. Reason: No valid crumb was included in the request

curl:
------------------------
curl -v --user <jenkinsuser>:<password> -X POST "http://jenkinsurl/job/testjob/buildWithParameters?token=test&Description=sometext" -H "Jenkins-Crumb:<crumb value>"

* timeout on name lookup is not supported
*   Trying xxx.xxx.xxx.xxx...
* TCP_NODELAY set
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to jenkinsurl (xxx.xxx.xxx.xxx) port 80 (#0)
* Server auth using Basic with user 'jenkinsuser'
> POST /job/testjob/buildWithParameters?token=test&Description=sometext HTTP/1.1
> Host: jenkinsurl
> Authorization: Basic Y3MyYXV0b3Rlc3Q6QXV0b21hdGlvbjE=
> User-Agent: curl/7.55.0
> Accept: */*
> Jenkins-Crumb:<crumb value>
>
* HTTP 1.0, assume close after body
< HTTP/1.0 302 Found
< Location: https://jenkinsurl/job/testjob/buildWithParameters?token=test&Description=sometext
< Server: BigIP
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 0
<
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
* Connection #0 to host jenkinsurl left intact

Slide

unread,
Jul 9, 2019, 3:06:57 PM7/9/19
to Jenkins User Mailing List
I think you want to pass the username/password as part of the Open method call

HttpReq.Open "POST", strJenkinsURL, False, strUsername, strPassword

In addition, you may need to set an Authorization header, but I am not sure how the  MSXML2.ServerXMLHTTP object handles that if you pass the username/password with the Open call.

You would need to do something like this if just passing it via the Open method doesn't work:

HttpReq.setRequestHeader "Authentication", "Basic " & Base64Encode(strUsername & ":" & strPassword)

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/acd05793-f5c3-47c1-9049-75835b7933a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

D. C.

unread,
Jul 9, 2019, 5:41:25 PM7/9/19
to Jenkins Users
Thanks for the suggestions.  I had actually already tried "HttpReq.Open "POST", strJenkinsURL, False, strUsername, strPassword" and it didn't help.  Since the error returned has to do with the crumb (403 No valid crumb was included), I hoped the auth part was actually working, but maybe not.  I have tried again with passing the username/password as part of the Open method, but still no luck there.

I tried the .setRequestHeader "Authentication" like you showed but it returns "Type mismatch: 'Base64Encode'" so something is wrong with the syntax.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkins...@googlegroups.com.

Slide

unread,
Jul 9, 2019, 6:59:57 PM7/9/19
to Jenkins User Mailing List
You would have to implement a function to base64 encode the data (I should have mentioned that in my previous post). 

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/40ab289f-2d68-465d-8c00-4791968a38a0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--

D. C.

unread,
Jul 11, 2019, 9:39:10 AM7/11/19
to Jenkins Users
After piecing together many suggestions with some trial and error, I was finally able to get the Jenkins job to run remotely with the VBScript (ASP) code below (with variables set to proper values such as strCrumb).  The returned Status will be 201 for success.  The Base64 functions were taken from https://stackoverflow.com/questions/496751/base64-encode-string-in-vbscript

    Dim strJenkinsURL, strParam1, strParam2, strCrumb, strUsername, strPassword
    strJenkinsURL = "https://<jenkinsURL>/job/testjob/buildWithParameters?token=test&param1="& strParam1 &"&param2="& strParam2


    Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
    HttpReq.Open "POST", strJenkinsURL, False
    HttpReq.setOption 2, 13056
    HttpReq.setRequestHeader "Jenkins-Crumb", strCrumb
    HttpReq.setRequestHeader "Authorization", "Basic "& Base64Encode(strUsername &":"& strPassword)

    HttpReq.Send
    Response.Write "<br>Status: "& HttpReq.Status & vbNewline
    Response.Write "<br>Response: "& HttpReq.responseText & vbNewline

    Function Base64Encode(sText)
        Dim oXML, oNode
        Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
        Set oNode = oXML.CreateElement("base64")
        oNode.dataType = "bin.base64"
        oNode.nodeTypedValue = Stream_StringToBinary(sText)
        Base64Encode = oNode.text
        Set oNode = Nothing
        Set oXML = Nothing
    End Function

    Private Function Stream_StringToBinary(Text)
        Const adTypeText = 2
        Const adTypeBinary = 1
        Dim BinaryStream 'As New Stream
        Set BinaryStream = CreateObject("ADODB.Stream")
        BinaryStream.Type = adTypeText
        BinaryStream.CharSet = "us-ascii"
        BinaryStream.Open
        BinaryStream.WriteText Text
        BinaryStream.Position = 0
        BinaryStream.Type = adTypeBinary
        BinaryStream.Position = 0
        Stream_StringToBinary = BinaryStream.Read
        Set BinaryStream = Nothing
    End Function

Reply all
Reply to author
Forward
0 new messages