2

我正在尝试在 python 中使用“ jenkinsapi ”包禁用一项工作。虽然我能够连接到 Jenkins 服务器并获取作业列表和作业状态,但我无法启用/禁用任何作业。我使用的是文档中提供的相同脚本,但不知何故,作业不会被禁用,也不会引发错误。注意:我已将 Jenkins 配置为在端口 8081 上运行

from jenkinsapi.jenkins import Jenkins

def get_server_instance():
    jenkins_url = "http://<Jenkins_URL>:8081"
    server = Jenkins(jenkins_url, username="admin", password="password")
    return server

def get_job_details(job_name=None):
    server = get_server_instance()
    if job_name is None:
        for j in server.get_jobs():
            job_instance = server.get_job(j[0])
            print('Job Name:%s' % (job_instance.name))
            print('Job Description:%s' % (job_instance.get_description()))
            print('Is Job running:%s' % (job_instance.is_running()))
            print('Is Job enabled:%s' % (job_instance.is_enabled()))
    else:
        job_instance = server.get_job(job_name)
        print('Job Name:%s' % (job_instance.name))
        print('Job Description:%s' % (job_instance.get_description()))
        print('Is Job running:%s' % (job_instance.is_running()))
        print('Is Job enabled:%s' % (job_instance.is_enabled()))

def disable_job():
    server = get_server_instance()
    job_name = "RestartTest"
    if (server.has_job(job_name)):
        job_instance = server.get_job(job_name)
        job_instance.disable()
        print('Name:%s,Is Job Enabled ?:%s' %(job_name, job_instance.is_enabled()))

if __name__ == "__main__":
    get_job_details("RestartTest")
    disable_job()

程序的输出:

Job Name: RestartTest
Job Description: The job runs restart_test.sh
Is Job running: False
Is Job enabled: True
Name:EventsTesting,Is Job Enabled ?: True

Process finished with exit code 0

我期望 Job Enabled 为 False ,并且浏览器上的作业状态也显示 Disabled ,但不会发生这种情况。我的连接设置似乎是正确的(因为我可以连接到服务器、获取作业列表、状态等)。或者是否有用于启用/禁用作业的特殊设置?

非常感谢任何帮助,谢谢。

4

0 回答 0