background

Jenkins, as an indispensable tool for project construction, is continuously deployed. How to use programs or scripts to control the construction process has become the content of this paper.

By default, after Jenkins is installed, the/API path contains some basic apis for reference, but it is not intuitive and does not list the apis.

The Jenkins-Java client used in this article is:

<dependency>
	<groupId>com.offbytwo.jenkins</groupId>
	<artifactId>jenkins-client</artifactId>
	<version>0.3.7</version>
</dependency>
Copy the code

Assume that the Jenkins deployment address is http://localhost:8080

The API used in this article:

The name of the API
Create a Job POST http://localhost:8080/createItem/api/json? name=xxx
Update the Job POST http://localhost:8080/job/{job_name}/config.xml/api/json
To get the Job GET http://localhost:8080/job/{job_name}/api/json
Get JobXml GET http://localhost:8080/job/{job_name}/config.xml/api/json
Create a Build POST http://localhost:8080/job/{job_name}/build/api/json
Get QueueItem GET http://localhost:8080/queue/item/17/api/json
Get Build information GET http://localhost:8080/job/test/6/api/json
Obtaining TXT logs GET http://localhost:8080/job/test/{build_number}/logText/progressiveText/api/json
Getting Html logs GET http://localhost:8080/job/test/{build_number}/logText/progressiveHtml/api/json

Create Job API

Jenkins’ configuration is implemented in XML format, so the configuration is actually in XML form.

POST http://localhost:8080/createItem/api/json? name=xxxCopy the code

The config.xml required for creation is also passed to the server with the request, and the creation succeeds.

How do you know how config.xml should be written?

  • You can create a desired project after Jenkins has finished work, and then go to Jenkins’ work directory to find jobs/{job_name}/config.xml and use it as a template to write the template you need.
  • Get this by getting the API for XML
GET http://localhost:8080/job/{job_name}/config.xml/api/json
Copy the code

Java Client

jenkinsServer.createJob("auto_test_job", replacedText, true);
String xml = jenkinsServer.getJobXml("auto_test_job");
Copy the code

Modify the Job API

Modifying a job is also modifying XML.

POST http://localhost:8080/job/{job_name}/config.xml/api/json
Copy the code

The content of config.xml is passed into the body, and contentType is set to text/ XML

Java Client

 jenkinsServer.updateJob("auto_test_job", replacedText, true);
Copy the code

Build the Build the API

Obtain the job by job name

GET http://localhost:8080/job/{job_name}/api/json 
Copy the code

The new building

POST http://localhost:8080/job/{job_name}/build/api/json 
Copy the code

It returns something like a queue ID after a successful submission to the server, because it’s an asynchronous build, so to get the status of the build, you need to use that queue ID to get further. Such as:

 $ curl -v  -XPOST http://localhost:8080/job/test/build/api/json
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (# 0)
> POST /job/test/build/ API /json HTTP/1.1 > Host: localhost:8080 > user-agent: curl/7.54.0 > Accept: */* > < HTTP/1.1 201 Created < Date: Sat, 05 Jan 2019 08:33:45 GMT < x-content-type-options: nosniffing < Location: sniff http://localhost:8080/queue/item/17/ < Content - Length: 0 < Server: Jetty (9.4 z - the SNAPSHOT) < * Connection#0 to host localhost left intact
Copy the code

The above Location: http://localhost:8080/queue/item/17/ is returned to the queue information, the queueItem gain is dependent on the below.

Get QueueItem based on QueueId

GET http://localhost:8080/queue/item/17/api/json 
Copy the code

For example,

$ curl http://localhost:8080/queue/item/17/api/json\? pretty\=true
{
  "_class" : "hudson.model.Queue$LeftItem"."actions": [{"_class" : "hudson.model.CauseAction"."causes": [{"_class" : "hudson.model.Cause$UserIdCause"."shortDescription" : "Started by User Anonymous"."userId" : null,
          "userName" : "anonymous"}}]]."blocked" : false."buildable" : false."id": 17."inQueueSince" : 1546677225670,
  "params" : ""."stuck" : false."task" : {
    "_class" : "hudson.maven.MavenModuleSet"."name" : "test"."url" : "http://localhost:8080/job/test/"."color" : "blue"
  },
  "url" : "queue/item/17/"."why" : null,
  "cancelled" : false."executable" : {
    "_class" : "hudson.maven.MavenModuleSetBuild"."number" : 6,
    "url" : "http://localhost:8080/job/test/6/"}}Copy the code

The key parameter is the executable. If the executable is not empty, it means that Jenkins is already building the task,build Is number, which is the number we saw on the Jenkins page. You can open it in the browser using the value of the URL field.

Get Build details

Now that we’ve started building, we can get build details.

curl http://localhost:8080/job/test/6/api/json\? pretty\=true{..."building" : false."description" : null,
  "displayName" : "# 6"."duration" : 13631,
  "estimatedDuration" : 17999,
  "executor" : null,
  "fullDisplayName" : "test #6"."id" : "6"."keepLog" : false."number" : 6,
  "queueId": 17."result" : "SUCCESS"."timestamp" : 1546677234794,
  "url" : "http://localhost:8080/job/test/6/"."builtOn" : "". }Copy the code

Build :”building” : false, if build ends in :”result” :” SUCCESS”,

Obtaining Build Logs

Text:

GET http://localhost:8080/job/test/{build_number}/logText/progressiveText/api/json
Copy the code

html

GET http://localhost:8080/job/test/{build_number}/logText/progressiveHtml/api/json
Copy the code

Java Client

JobWithDetails job = jenkinsServer.getJob(jenkinsJob); . QueueReference reference = job.build(true); . QueueItem queueItem = jenkinsServer.getQueueItem(newQueueReference(queuePart)); . Build build = jenkinsServer.getBuild(queueItem); . BuildWithDetails details = build.details(); BuildResult result = details.getResult(); . String logs = details.getConsoleOutputText();Copy the code