安装python-jenkins
1.pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pbr # python-jenkins依赖pbr,multi_key_dict
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple multi_key_dict
2.下载python-jenkins-1.3.0.tar.gz 3.python setup.py install
使用python-jenkins
import jenkins
url = 'xxxxx:8080'
username = 'xxxxx'
password = 'xxxxx'
params = {'Branch': 'origin/master', 'host': '192.168.1.1'} # 构建参数
# 创建jenkins对象
server = jenkins.Jenkins(url=url,username=username,password=password)
#user = server.get_whoami()
#version = server.get_version()
###############job CRUD###############
#print(server.job_exists('hyh-test'))
# 获取所有的job
#print(server.get_jobs())
# 创建job
#server.create_job('hyh-test',jenkins.EMPTY_CONFIG_XML)
# 删除job
#server.delete_job('hyh-test')
# 复制job
#server.copy_job('hyh-test','hyh-test1') # hyh-test是源,hyh-test1是目标
#server.copy_job('job-name','hyh-test')
# enable job
#server.enable_job('hyh-test')
# 禁用job
#server.disable_job('hyh-test')
# 重命名job
#server.rename_job('hyh-test','hyh-test1')
# 重新配置Job
config_file = """
<project>
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties>
<com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.5.3">
<gitLabConnection>gitlab</gitLabConnection>
</com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
<jenkins.model.BuildDiscarderProperty>
<strategy class="hudson.tasks.LogRotator">
<daysToKeep>-1</daysToKeep>
<numToKeep>5</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</strategy>
</jenkins.model.BuildDiscarderProperty>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition plugin="git-parameter@0.9.1">
<name>br</name>
<description>选择要发布的TAG或分支</description>
<uuid>3100f068-f987-4f6f-82f7-aaa4807cda9b</uuid>
<type>PT_BRANCH_TAG</type>
<branch></branch>
<tagFilter>*</tagFilter>
<branchFilter>.*</branchFilter>
<sortMode>ASCENDING</sortMode>
<defaultValue>origin/develop</defaultValue>
<selectedValue>DEFAULT</selectedValue>
<quickFilterEnabled>true</quickFilterEnabled>
</net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition>
<hudson.model.ChoiceParameterDefinition>
<name>hosts</name>
<description>选择发布的机器名称</description>
<choices class="java.util.Arrays$ArrayList">
<a class="string-array">
<string>gamma-product-center</string>
</a>
</choices>
</hudson.model.ChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.plugins.git.GitSCM" plugin="git@3.8.0">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>git@xxxx</url>
<credentialsId>deploy2</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>${br}</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Maven>
<targets>clean deploy -Dmaven.test.skip=true</targets>
<usePrivateRepository>false</usePrivateRepository>
<settings class="jenkins.mvn.DefaultSettingsProvider"/>
<globalSettings class="jenkins.mvn.DefaultGlobalSettingsProvider"/>
<injectBuildVariables>false</injectBuildVariables>
</hudson.tasks.Maven>
<hudson.tasks.Shell>
<command>#!/bin/bash
echo 发布的Tag或分支:"$br"
echo 发布的主机:"${hosts}"
/bin/sh /data/jenkins/shell/publish.sh</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>
"""
server.reconfig_job('hyh-test', config_xml=config_file) # config_xml必须是html格式的字符串
# 获取job配置
#my_job = server.get_job_config('hyh-test') 获取的my_obj是html格式的字符串
#print(my_job)
# 触发job(方式一)
#server.build_job_url('hyh-test',parameters=params)
## 触发job(方式二)
#server.build_job('hyh-test',parameters=params)
# 查看输出
#last_build_number = server.get_job_info('job-name')['lastCompletedBuild']['number'] # 获取构建编号
#print(server.get_build_console_output('job-name', last_build_number))
# 查看最后一次构建的job信息
# last_build_number = server.get_job_info('job-name')['lastCompletedBuild']['number']
# print(last_build_number)
# build_info = server.get_build_info('job-name', last_build_number)
# print(build_info)
####################################
转载于:https://blog.51cto.com/haoyonghui/2310257