Purpose: Dynamically create the job of Jenkins by invoking Jenkins's command
How to use, using Python scripts, more APIs can go to the official website to view, http://jenkinsapi.readthedocs.io/en/latest/
Using Python to call Jenkinsapi, you first need to install the package, installation method
1. Installation
Pip Install Jenkinsapi
After the installation is complete, check to see if the installation is successful, import the package for viewing,
From Jenkinsapi.jenkins Import Jenkins
If the installation is not successful, the import will prompt an error
Installation succeeds without prompting for errors
1. Role
Create the job principle, is to use existing projects for project replication, each Jenkins job is actually an XML file, just need to copy the configuration file, the project name changes, you can generate a new job
The XML configuration file for the original project is
From __future__ import print_functionfrom jenkinsapi.jenkins import jenkinsimport sysimport timereload (SYS) Sys.setdefaultencoding (' utf8 ') Jenkins_url = ' Access to Jenkins ' domain address/jenkins/' jenkins_user = ' Jenkins login admin ' jenkins_ Password = ' Jenkins login password ' def creater_job (job_name): ss = Jenkins (Jenkins_url,jenkins_user,jenkins_password) copy_job_name = ' job1 ' #jenkins的job xml = Ss[copy_job_name].get_config () #获取配置文件 job = Ss.create_job (job_name, XML) if __name__== ' __main__ ': creater_job ("June")
The generated job
Create a job for Jenkins using Python