Python's Jenkins interface call method

I liked being lazy

It’s best not to work

So recently we are studying the integration of Jenkins modules

Make it a fool interface so I can use it for them

I don’t want to spray Python, please guide me

Jenkins Python module module installation

pip:
pip install python-jenkins

easy_install:
easy_install python-jenkins

use:

classjenkins_tools():
def __init__(self):
cf =get_conf()
self.username = cf.get('jenkins','username')
self.password = cf.get('jenkins','password')
self.php_jenkins ='''			#Ben's own jenkins conf file
< project 				#Here you can copy the configuration file in the project folder of jenkins
< actions/ 				#Remember not to add the xml header, where the source code helped us to add it, adding it by yourself is dead
< description </description 		#The project requirements are different, and the configuration files are different. Don’t copy mine.
< keepDependencies false</keepDependencies 
< properties 
< hudson.model.ParametersDefinitionProperty 
< parameterDefinitions 
< hudson.model.StringParameterDefinition 
< name Branch</name 
< description </description 
< defaultValue %s</defaultValue 
< /hudson.model.StringParameterDefinition 
< /parameterDefinitions 
< /hudson.model.ParametersDefinitionProperty 
< /properties 
< scm class="hudson.scm.NullSCM"/<canRoam true</canRoam 
< disabled false</disabled 
< blockBuildWhenDownstreamBuilding false</blockBuildWhenDownstreamBuilding 
< blockBuildWhenUpstreamBuilding false</blockBuildWhenUpstreamBuilding 
< triggers/<concurrentBuild false</concurrentBuild 
< builders 
< hudson.tasks.Shell 
< command xxxxxxx</command 	
< /hudson.tasks.Shell 
< /builders 
< publishers/<buildWrappers/</project 
'''
self.java_newjenkins ='''		#Another jenkins conf file of this dick
< project 
< actions/<description </description 
< keepDependencies false</keepDependencies 
< properties 
< hudson.model.ParametersDefinitionProperty 
< parameterDefinitions 
< hudson.model.StringParameterDefinition 
< name Branch</name 
< description </description 
< defaultValue %s</defaultValue 
< /hudson.model.StringParameterDefinition 
< /parameterDefinitions 
< /hudson.model.ParametersDefinitionProperty 
< /properties 
< scm class="hudson.scm.NullSCM"/<canRoam true</canRoam 
< disabled false</disabled 
< blockBuildWhenDownstreamBuilding false</blockBuildWhenDownstreamBuilding 
< blockBuildWhenUpstreamBuilding false</blockBuildWhenUpstreamBuilding 
< triggers/<concurrentBuild false</concurrentBuild 
< builders 
< hudson.tasks.Shell 
< command xxxx</command 	
< /hudson.tasks.Shell 
< /builders 
< publishers/<buildWrappers/</project 
'''
def __conn_jenkins_server(self, url):try:
# Obtain a jenkins operation instance
server = jenkins.Jenkins(url, username=self.username, password=self.password)return server
except Exception:
logging.warning('login jenkins failed!')return None
def create_project(self, host_ip, project_name, git_path, git_branch, url, environment):
server = self.__conn_jenkins_server(url)if server:
server.create_job(project_name, self.php_jenkins)	#Parameter 1 is the project name, parameter 2 is the xml document
return True
else:return None
def project_built(self, url, project_name, git_branch):	#This function is used to build the project
server = self.__conn_jenkins_server(url)		
server.build_job(project_name,{'Branch': git_branch})
def check_project_exist(self, project_name, url):		#This function is to check whether the project already exists, although it is very frustrating to write, don&#39;t be offended
server = self.__conn_jenkins_server(url)
name = server.get_job_name(project_name)if name is None:return False
return True

For details, please see the official document: http://python-jenkins.readthedocs.io/en/latest/api.html

Supplementary knowledge: python calls jenkinsapi

When calling jenkinsapi through python, some jobs need to be constructed regularly

Error:

< title Error 403 No valid crumb was included in the request</title \n</head \n<body <h2 HTTP ERROR 403</h2

The reason is to check the following option in the security configuration of jenkins. To prevent cross-site requests, just check it.

The above Python Jenkins interface call method is all the content shared by the editor, I hope to give you a reference.

Recommended Posts

Python&#39;s Jenkins interface call method
Python and js interactive call method