Jenkins-client Implementation method
The jenkins-client is implemented using HttpClient HttpPost to send a POST request, which encapsulates the REST API
Add dependency
<!--Jenkins--><dependency> <groupId>com.offbytwo.jenkins</groupId> < Artifactid>jenkins-client</artifactid> <version>0.3.7</version></dependency>
Connecting to a server
public synchronized static Jenkinsserver getinstance () { if (jenkinsserver = = null try {jenkinsserver = new jenkinsserver (new URI ( Envparam.getinstance (). Getjenkinsurl ()), Envparam.getinstance (). Getjenkinsuserid (), Envparam.getinstanc E (). Getjenkinstoken ()); catch (urisyntaxexception e) {E.prin Tstacktrace (); }} return Jenkinsserver;}
The new Jenkinsserver () bottom layer uses the httpclient underlying code as follows
Basiccredentialsprovider Provider =NewBasiccredentialsprovider (); Authscope Scope=NewAuthscope (Uri.gethost (), Uri.getport (), "Realm"); Usernamepasswordcredentials Credentials=Newusernamepasswordcredentials (username, password);p rovider.setcredentials (scope, credentials); Builder.setdefaultcredentialsprovider (provider); Builder.addinterceptorfirst (NewPreemptiveauth ());if(Stringutils.isnotblank (username)) { This. Localcontext =NewBasichttpcontext (); This. Localcontext.setattribute ("Preemptive-auth",Newbasicscheme ());}
Get all job information
Jenkinsserver jenkins = getinstance (); Map<string, job> jobs = jenkins.getjobs ();
Jenkins.getjobs (); A map object with the JobName key is returned
Get log information for a build
Public Static String getjoblog (intthrows IOException { = getinstance (); = Jenkins.getjob (jobName); = job.details (); = Details.getbuildbynumber (buildnumber); = buildbynumber.details (); = Details2.getconsoleoutputtext (); return Outputtext;}
Get the start time and duration of a build
Public StaticMap<string, long> getstarttimeandendtime (String jobName,intNumberthrowsIOException {jenkinsserver Jenkins=getinstance (); Map<string, job> jobs =jenkins.getjobs (); Jobwithdetails Job=Jobs.get (jobName). Details (); Build Buildbynumber=job.getbuildbynumber (number); LongStartTime =buildbynumber.details (). Gettimestamp (); LongDuration =buildbynumber.details (). Getduration (); Map<string, long> data =NewHashmap<>(); Data.put ("StartTime", StartTime); Data.put ("duration", duration); returndata;}
Gets whether the last build was successful
Number: The Jobname:job name is determined by obtaining the last successful, failed build number to determine whether it is equal
Public Static BooleanIssuccess (String JobName,intNumberthrowsIOException {jenkinsserver Jenkins=getinstance (); Map<string, job> jobs =jenkins.getjobs (); Jobwithdetails Job=Jobs.get (jobName). Details (); intLastsuccessfulnumber =job.getlastsuccessfulbuild (). GetNumber (); intLastunsuccessfulnumber =job.getlastunsuccessfulbuild (). GetNumber (); BooleanFlag =false; if(Lastsuccessfulnumber = =Number ) {Flag=true; } if(Lastunsuccessfulnumber = =Number ) {Flag=false; } returnFlag; }
Judging the final result by getting the final result of the build
Results returned: SUCCESS, FAILURE
Public Static int throws IOException { = getinstance (); Map<string, job> jobs = jenkins.getjobs (); = Jobs.get (jobName). Details (); = Job.getbuildbynumber (number); = buildbynumber.details (); return Details.getresult (). toString (); }
Determine if job execution is complete
Public Static BooleanIsfinished (intNumber , String jobName) { BooleanIsbuilding =false; if(Number <= 0) { Throw NewIllegalArgumentException ("Jodid must greater than 0!")); } Try{jenkinsserver Jenkins=getinstance (); Map<string, job> jobs =jenkins.getjobs (); Jobwithdetails Job=Jobs.get (jobName). Details (); Build Buildbynumber=job.getbuildbynumber (number); if(NULL!=Buildbynumber) {buildwithdetails Details=buildbynumber.details (); if(NULL!=details) {isbuilding=details.isbuilding (); } Else{isbuilding=true; } } Else{isbuilding=true; } return!isbuilding; } Catch(Exception e) {e.printstacktrace (); } finally { } return false; }
by calling Jenkins-client related functions above, you can get related Jenkins operations, such as log, build time, job build length, build number, whether it is successful, whether it is being built, etc., and also find some problems such as
The view is configured in Jenkins, and there is a view below the view, and the view,jenkins-client can only get two levels of view, and the third layer view cannot get
If you use the build with parameters plugin in Jenkins to select multiple parameters using the check box, it is not possible to pass the Jenkins-client parameter.
The Jenkins API operation