in the study of Jenkinsapi, in Baidu and Google to find materials, always some fragmentary introduction, based on the time of the research, a little achievement, so the work of the study recorded. Jenkinsapiinstallation of the library
Download: load jenkinsapi-0.2.26.tar.gz on https://pypi.python.org/pypi/jenkinsapi/;
installation: Unzip the jenkinsapi-0.2.26.tar.gz, enter the jenkinsapi-0.2.26 directory, run the pythonsetup.py install , in the case of networking, the program will download some packages directly and install them, and the library can be operated after the run is complete.
JenkinsapiClass for library encapsulation
The library is encapsulated in the following categories, mainly jenkins.py,job.py,build.py,view.py,node.py and so on, its main role:
1.jenkins.py
mainly through this interface into the Jenkins server, the job,node operation, there are:
Get_jobs,create_job,build_job,delete_job,get_node, Get_queue, get_plugins, get_master_data, etc.;
2.job.py
for Job building operations, there are:
Get_first_buildnumber,get_last_stable_buildnumber,get_last_failed_buildnumber,get_ Next_build_number,is_queued,is_running,is_enabled,delete_from_queue, etc.;
3.build.py
The construction results of the built job are analyzed:
Get_number,get_status,get_revision_branch,get_master_job_name,get_master_ Build_number , etc.;
4.view.py
Manipulating The view view
5.node.py
Working with node nodes
JenkinsapiUse of librariesGetJenkinsinJobthe list
code:
# -*- coding: gb2312 -*-"" "Input: The address of the Jenkins server, User name and password output: The list of defined Jobs "" "import datetime, timefrom jenkinsapi.jenkins import *from Jenkinsapi.job import *from jenkinsapi.build import builddef url_get_job_list ( Url= ' http://localhost:8080 ', username= ' Jenkins ', password= ' Jenkins '): jenkins = jenkins (Url, username, password) count = 0 for job_name in jenkins.keys (): my_ Job = jenkins.get_job (job_name) count = count + 1 print "Job" + str (count) + " : " +job_nameif __name__ == "__main__": url_get_ Job_list ()
Post-run output:
Job1:create
Job2:jenkinstest
Job3:test_1
based on the givenJobname obtainedJobthe build situation
Code:
# -*- coding: cp936 -*-"" "Input: Jenkins job name and date (year, year-month, year-month-day) output: Job name, date, number of executions, number of successes, number of failures , number of executions, number of instabilities "" "import datetime, timefrom jenkinsapi.jenkins import *from Jenkinsapi.job import *from jenkinsapi.build import builddef get_date_list (Job_ Name): my_job = jenkins.get_job (job_name) first_build = my_job.get_first_buildnumber () #获得jenkins列表中的第一个构建数目, not necessarily starting from 1 last_build = my_job.get_last_buildnumber () #获得最近一次构建的数目 for count In xrange (first_build, last_build+1): #依次获得每一个job号 count_build = my_job.get_build (count) start_time = count_build.get_timestamp () #获得构建时间, time zone doesn't seem very right adjust_time = start_ Time + datetime.timedelta (hours=8) #所以在这里加了8小时, changed to Beijing, build_time = adjust_time.strftime ("%y-%m-%d") # Format of the output Time status =count_build.get_status () #返回这次构建的状态, success or failure print my_job, count, Build_Time, Status if __name__ == "__main__": jenkins = jenkins (' http://localhost : 8080 ', ' Jenkins ', ' Jenkins ') Job_Name = ' create ' get_date_list (job_name)
The results of the operation are as follows:
Create 1 2015-03-24 FAILURE
Create 2 2015-03-24 FAILURE
Create 3 2015-03-24 SUCCESS
Create 4 2015-03-25 SUCCESS
Create 5 2015-03-30 FAILURE
With this module, you can run scripts to get detailed information about the build of Jenkins every day, convenient for daily stats.
I contact Python time is not long, the study is relatively superficial, I hope to learn with you, Welcome to discuss together !
This article from the "7130238" blog, reproduced please contact the author!
Use of Jenkins Jenkinsapi Library