2

I'm having trouble with Jenkins' API while using Python 3.10.2 with Ubuntu LTS 20.4 with Jenkins 2.361 with api4jenkins 1.11 as the wrapper for the API.

My main trouble is passing parameters to a Task. I'm able to start the task, and I'm doing as the examples indicate to pass the parameters, but the taks' execution don't print the parameters.

Jenkins has a global security configuration for each user, this is the one for the user that's starting the task:

Jenkins Global Security Configuration

My Task is called Prueba4, the task security configuration is this one:

Prueba4 security configuration

Right now, I only have the parameter client:

Prueba4 parameters

I also enabled script execution:

Script execution

The only step is this shell command:

echo Hello
echo $USER
echo $cliente

With Python I'm doing this:

from api4jenkins import Jenkins
j = Jenkins('http://localhost:8080/', auth=('my_user', 'mypass'))

j.build_job('Prueba4', client="my cliente", token="my_token", delay='1sec')

So I can't find the problem that the line echo $client does not show the parameter I passed. Adding print to api4jenkins I was able to identify that it pass this kwards:

method: POST
url +: http://localhost:8080/job/Prueba4/buildWithParameters
kwards: {'params': {'client': 'my_client'}, 'token': 'my_token', 'delay': '1sec'}, 'headers': {'Jenkins-Crumb': '81bec758701d0783a68f3ad4050e42c647953834f1c552120b194d6e8b989f52'}} 

I don't know what I' doing wrong. This is an execution started with python that doesn't print the parameter:

Python execution

I tried with CURL and Python's Request library, but I'm not able to make a request with them yet.

Hope someone can help me. Thanks

EDIT: Ok, I was making a dumb mistake, I really needed to pass a dictionary and I was adding the variable directly, I forgot to send it as a string, the true reason why Jenkins was not accepting the request.

Alex Turner
  • 698
  • 6
  • 16

1 Answers1

1

Ok, I forgot to search in the issues pages in the Github page from api4jenkins, there i found this issue. In there Sinutok commented on 7 Sep 2021 the solution:

Sorry wasted your time....got it facepalm I'll post my now running code as example, if someone needs it

jenkins_server = api4jenkins.Jenkins('https://myjenkins.example.com:8443', auth=('someuser','the_api_token'),verify=False)
user = jenkins_server.me.exists()
version = jenkins_server.version
print(str(user))
print(str(version))
job = jenkins_server.get_job("API-TEST - JOB")
print(job)
lauf = job.build(TEXT1="C",TEXT2="lalala")
print(lauf)

Output:
True
2.277.4
<FreeStyleProject: https://myjenkins.example.com:8443/job/API-TEST%20-%20JOB/>
<QueueItem: https://myjenkins.example.com:8443/queue/item/1149/>
and the jenkis job runs correctly with the Parameters: "C" (of the dropdown-box) and "lalala" (of the text-in-field)

EDIT: Ok, I was making a dumb mistake, I really needed to pass a dictionary and I was adding the variable directly, I forgot to send it as a string, the true reason why Jenkins was not accepting the request. This issue help me see it.

Alex Turner
  • 698
  • 6
  • 16