0%

Jenkins-API

Jenkins-API

需求:

在自动化平台中调用jenkins API,实现程序发版、自定义任务等

实现:

Python API

  • 安装

    1
    pip install python-jenkins
  • examples

    1
    2
    3
    4
    5
    6
    import jenkins

    server = jenkins.Jenkins('http://localhost:8080', username='myuser', password='mypassword')
    user = server.get_whoami()
    version = server.get_version()
    print('Hello %s from Jenkins %s' % (user['fullName'], version))
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import jenkins
    job_name='k8s/cicd-test-0613'
    jenkins_server_url='http://jenkins.visualchina.com/jenkins'
    user_id='vcgdev'
    api_token='***************************'
    server=jenkins.Jenkins(jenkins_server_url, username=user_id, password=api_token)
    #String参数化构建job名为job_name的job, 参数param_dict为字典形式,如:param_dict= {"param1":“value1”, “param2”:“value2”}
    param_dict= {"Action":"程序发版","Scope":"生产环境","JenkinsApi":"true"}
    server.build_job(job_name, parameters=param_dict)
    last_build_number = server.get_job_info(job_name)['lastCompletedBuild']['number']
    build_info = server.get_build_info(job_name, last_build_number)
    print(build_info)
项目截图