Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the format of the JSON for a Jenkins REST buildWithParameters to override the default parameters values

Tags:

rest

jenkins

api

I am able to build a Jenkins job with its parameters' default values by sending a POST call to http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters

and I can override the default parameters "product", "suites" and "markers by sending to this URL: http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=ALL&suites=ALL&markers=ALL

But I saw examples were the parameters can be override by sending a JSON body with new values. I am trying to do that by sending the following json bodies. Neither of them works for me.

{
        'product': 'ALL',
        'suites': 'ALL',
        'markers': 'ALL'
}

and

{
  "parameter": [
        {
            "name": "product",
            "value": "ALL"
        },
        {
            "name": "suites",
            "value": "ALL"
        },
        {
            "name": "markers",
            "value": "ALL"
        }
  ]
}

What JSON to send if I want to override the values of parameters "product", "suites" & "markers"?

like image 719
RaamEE Avatar asked Jul 24 '18 22:07

RaamEE


People also ask

What is JSON format in REST API?

JavaScript Object Notation (JSON) is a lightweight data-interchange format that is readable and is easily parsed by machines. JSON is based on a subset of the JavaScript Programming Language.

What is REST API in Jenkins?

The jenkins-rest library is an object oriented Java project that provides access to the Jenkins REST API programmatically to some remote API Jenkins provides. It is built using the jclouds toolkit and can easily be extended to support more REST endpoints.


2 Answers

I'll leave the original question as is and elaborate here on the various API calls to trigger parameterized builds. These are the calls options that I used.

Additional documentation: https://wiki.jenkins.io/display/JENKINS/Remote+access+API

The job contains 3 parameters named: product, suites, markers

  1. Send the parameters as URL query parameters to /buildWithParameters: http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=ALL&suites=ALL&markers=ALL

  2. Send the parameters as JSON data\payload to /build: http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/build

The JSON data\payload is not sent as the call's json_body (which is what confused me), but rater in the data payload as:

json:'{
       "parameter": [
                     {"name":"product", "value":"123"}, 
                     {"name":"suites", "value":"high"}, 
                     {"name":"markers", "value":"Hello"}
                    ]
      }'

And here are the CURL commands for each of the above calls:

curl -X POST -H "Jenkins-Crumb:2e11fc9...0ed4883a14a" http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/build --user "raameeil:228366f31...f655eb82058ad12d" --form json='{"parameter": [{"name":"product", "value":"123"}, {"name":"suites", "value":"high"}, {"name":"markers", "value":"Hello"}]}'

curl -X POST \ 'http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=234&suites=333&markers=555' \ -H 'authorization: Basic c2hsb21pb...ODRlNjU1ZWI4MjAyOGFkMTJk' \ -H 'cache-control: no-cache' \ -H 'jenkins-crumb: 0bed4c7...9031c735a' \ -H 'postman-token: 0fb2ef51-...-...-...-6430e9263c3b'

What to send to Python's requests In order to send the above calls in Python you will need to pass:

  1. headers = jenkins-crumb
  2. auth = tuple of your (user_name, user_auth_token)
  3. data = dictionary type { 'json' : json string of {"parameter":[....]} }
like image 189
RaamEE Avatar answered Oct 07 '22 08:10

RaamEE


curl -v POST http://user:token@host:port/job/my-job/build --data-urlencode json='{"parameter": [{"name":"xx", "value":"xxx"}]}

or use Python request:

import requests
import json
url = " http://user:token@host:port/job/my-job/build "
pyload = {"parameter": [
    {"name":"xx", "value":"xxx"},
]}
data = {'json': json.dumps(pyload)}
rep = requests.post(url, data)
like image 31
ahprosim Avatar answered Oct 07 '22 08:10

ahprosim