简体   繁体   English

如何使用 Python 和 api4jenkins 将参数传递给 Jenkins 的 API?

[英]How to pass parameters to Jenkins' API with Python and api4jenkins?

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. 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.我能够启动任务,并且我正在按照示例指示传递参数,但是 taks 的执行不打印参数。

Jenkins has a global security configuration for each user, this is the one for the user that's starting the task: Jenkins 对每个用户都有一个全局安全配置,这是为启动任务的用户配置的:

Jenkins 全局安全配置

My Task is called Prueba4 , the task security configuration is this one:我的任务叫做Prueba4 ,任务安全配置是这样的:

Prueba4 安全配置

Right now, I only have the parameter client :现在,我只有参数client

Prueba4参数

I also enabled script execution:我还启用了脚本执行:

脚本执行

The only step is this shell command:唯一的步骤是这个 shell 命令:

echo Hello
echo $USER
echo $cliente

With Python I'm doing this:使用 Python 我正在这样做:

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.所以找不到echo $client这行没有显示我传递的参数的问题。 Adding print to api4jenkins I was able to identify that it pass this kwards:将 print 添加到 api4jenkins 我能够确定它通过了这个 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 开始的执行,不打印参数:

Python 执行

I tried with CURL and Python's Request library, but I'm not able to make a request with them yet.我尝试使用 CURL 和 Python 的 Request 库,但我还不能向他们提出请求。

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.编辑:好的,我犯了一个愚蠢的错误,我真的需要传递一个字典并且我直接添加了变量,我忘记将它作为字符串发送,这是 Jenkins 不接受请求的真正原因。

Ok, I forgot to search in the issues pages in the Github page from api4jenkins, there i found this issue .好的,我忘记在 api4jenkins 的 Github 页面中的问题页面中搜索,我发现了这个问题 In there Sinutok commented on 7 Sep 2021 the solution:在那里,Sinutok 于 2021 年 9 月 7 日评论了解决方案:

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.编辑:好的,我犯了一个愚蠢的错误,我真的需要传递一个字典并且我直接添加了变量,我忘记将它作为字符串发送,这是 Jenkins 不接受请求的真正原因。 This issue help me see it.这个问题帮我看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM