1

我正在使用 jenkins rest API 来递归作业,然后重新配置这个。除一种方法外,所有方法都有效。他是我的代码:

def get_server_instance():
    jenkins_url = 'xxxx'
    #server = Jenkins(jenkins_url, username = '', password = '')
    # Connect to instance - username and password are optional
    server = jenkins.Jenkins(jenkins_url, username = '', password = '')
    return server


def get_job_details():
    # Refer Example #1 for definition of function 'get_server_instance'
    server = get_server_instance()
    for job in server.get_jobs_list():
        if job == "GithubMigration":
            configuration = server.get_job(job).get_config().encode('utf-8')
            #server.reconfig_job(job, configuration)
            if server.has_job("GithubMigration"):
                server.reconfig_job('GithubMigration', config_xml)

它获取了我的 configuration.xml,也找到了工作,但在server.reconfig_job('GithubMigration', config_xml)上失败并出现错误,AttributeError: 'Jenkins' object has no attribute 'reconfig_job'

当 jenkins rest API 中显然存在这个函数时,是的,我正在从 jenkinsapi.jenkins import Jenkins 导入 jenkins。

编辑 1 - 我卸载了 Jenkinsapi 并且只有 python-jenkins 模块,现在它甚至在说之前就失败了

AttributeError:'module'对象没有属性'Jenkins'的行:AttributeError:'module'对象没有属性'Jenkins'

有任何想法吗?

编辑 2:

我只尝试了 python-jenkins API 并尝试了他们自己的示例,如您在此处看到的http://python-jenkins.readthedocs.org/en/latest/example.html

import jenkins
j = jenkins.Jenkins('http://your_url_here', 'username', 'password')
j.get_jobs()
j.create_job('empty', jenkins.EMPTY_CONFIG_XML)
j.disable_job('empty')
j.copy_job('empty', 'empty_copy')
j.enable_job('empty_copy')
j.reconfig_job('empty_copy', jenkins.RECONFIG_XML)

即使这在 jenkins.Jenkins 上也失败了,在 Jenkins 上出现属性错误 - 没有模块。

我很确定 API 坏了。

4

3 回答 3

9

Your script is probably importing wrong module. You can check it as follows:

import jenkins
print jenkins.__file__

If printed path is other than installation path of jenkins module (eg. C:\Python27_32\lib\site-packages\jenkins\__init__.pyc), then you should check pythonpath:

import sys
print sys.path

Common problem is existence of python script with same name as imported module in current directory, which is at the first place in search path ''.

For more info on import order see module search path

于 2015-02-04T23:22:08.593 回答
2

@Chemik answer之后,我意识到我编写的脚本已命名jenkins.py,并且与 python-jenkins 导入冲突。

图书馆没有坏。检查您的脚本名称。

于 2019-10-31T17:52:08.767 回答
1

必须添加另一个解决方案,同时运行相同的命令

server = jenkins.Jenkins(jenkins_url, username = '', password = '')

我得到了错误:

'jenkins' has no attribute 'Jenkins'

我的错误是在安装包时,我安装了包“jenkins”,而我需要的包是“python-jenkins”。可以找到文档: python-jenkins 文档

所以我要做的就是

pip install python-jenkins
于 2020-03-23T12:41:31.577 回答