如何使用Python包JenkinsAPI触发Jenkins构建? [英] How to trigger Jenkins build using the Python package JenkinsAPI?

查看:425
本文介绍了如何使用Python包JenkinsAPI触发Jenkins构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Test2的Jenkins作业,可以从Jenkins Web界面构建该作业.现在,我想使用JenkinsAPI触发该构建.除了 API文档提到Build类.所以我想我会尝试实例化它,然后等待它完成(希望这也会触发实际的构建),但是我比这早得多地得到了一些隐秘的错误.我在做什么错了?

I have a Jenkins job set up with the name Test2 which I can build from Jenkins web interface. Now I want to trigger that build using JenkinsAPI. I have only been able to find example code to do other things but the API Documentation mentions the Build class. So I thought I would try to instantiate it and then wait for it to complete (hoping that would also trigger the actual build) but I am getting rather cryptic errors much earlier than that. What am I doing wrong?

import jenkinsapi
b = jenkinsapi.build.Build("http://localhost:8080", 1, "test2")
b.block_until_complete()

给我:

Traceback (most recent call last):
  File "/Users/jonathan/Genetta/Eclipse_Django_workspace/FOO/foo/TriggerBuild.py", line 2, in <module>
    b = jenkinsapi.build.Build("http://localhost:8080", 1, "test2")
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/build.py", line 58, in __init__
    JenkinsBase.__init__(self, url)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 35, in __init__
    self.poll()
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 59, in poll
    data = self._poll(tree=tree)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/build.py", line 65, in _poll
    return self.get_data(url, params={'depth': self.depth}, tree=tree)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 72, in get_data
    requester = self.get_jenkins_obj().requester
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/build.py", line 371, in get_jenkins_obj
    return self.job.get_jenkins_obj()
AttributeError: 'str' object has no attribute 'get_jenkins_obj'

推荐答案

我尚不清楚您的示例为何不起作用,但是我发现JenkinsAPI文档总体上令人困惑,所以也许我不理解.

It's not clear to me why your example isn't working, but I find the JenkinsAPI documentation confusing in general so perhaps I just don't get it.

我发现要直接获取特定版本,可以使用 get_build方法.参数的顺序不同:

I've found that to get a particular build directly, you can use the get_build method in the api package. The arguments are in a different order:

import jenkinsapi
b = jenkinsapi.api.get_build("http://localhost:8080", "Test 1", 1)

这对于通过其他方式启动的现有内部版本来说是很好的.但这听起来像您实际上是想触发构建.在这种情况下,请通过Jenkins实例获取作业,然后使用invoke方法:

This is fine for existing builds, started through some other means. But it sounds like you actually want to trigger a build. In that case, get the job through a Jenkins instance and use the invoke method:

import jenkinsapi
jenkins = jenkinsapi.jenkins.Jenkins("http://192.168.99.100:8080")
job = jenkins["Test 1"]
job.invoke(block=True)

我认为,当可以通过 requests 程序包访问普通的Jenkins REST API时,使用记录混乱的接口程序包(为什么会有多种获取构建方式的方法)几乎没有好处.如 massiou的答案所述.

In my opinion, there is little benefit to using a confusingly documented interface package (why are there multiple ways to get a build?) when the plain Jenkins REST API can be accessed via the requests package as described by massiou's answer.

这篇关于如何使用Python包JenkinsAPI触发Jenkins构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆