Automate your deployments with the Jenkins API

October 12 2012

0 comments 0
apis-icon

The build systemhas an open API which means we can do stuff with it. Today we’re going to automate the deployment of an application with a specific stable version.  Jenkins has a great UI, it’s very flexible indeed, but sometimes is not enough. For instance, for CloudHub we deploy a Jenkins build to our QA environment, according the period in our development cycle, and we also need to execute automated tests. However, we can create a quick web application to choose a version of an application in  Jenkins to deploy, using Mule Studio and CloudHub.

So, here is a application that gather information from Jenkins and even start builds using a UI that satisfy our need creating:

Now, lets dig into this application

The Flows

Creating the UI

The first flow is an HTTP Endpoint that serves static resources, in this case the HTML page that makes calls to get the information from Jenkins. This info is exposed throw others endpoints in this same Mule application.

 

Get the information from Jenkins

There are two Endpoints to get information from Jenkins exposed by the connector. The HTML page uses ajax to hit those endpoints and obtain the information in JSON format.

But first there is a configuration to make the connection to Jenkins server work:

<jenkins:config name="jenkinsconf" username="user name" connectionName="main" password="password" jenkinsURL="https://jenkins-server.com" doc:name="Jenkins"/>

 

This endpoint is used to get the Jobs list and obtain their names. Mule connects to Jenkins API and returns in JSON format the requested information. (Jenkins API access: https://JenkinsServer/api/json)

 

With this endpoint we get the information about the Job. In this web app we need to get two pieces of information, the build number and its status (success, failed). (Jenkins API access: https://JenkinsServer/jobName/api/json)

 

Perform job build

Finally, we expose an Endpoint with the ability to initiate a job build in Jenkins. This is the real work to call the deployer job:

 

Calling endpoints using AJAX

Now, the final piece is to make the UI to work, connecting all the elements in the page with the information that mule returns.

The following is an example of access the previous endpoints using JQuery ajax call:

 

Conclusion

Using Mule Studio, we are able to create quickly this kind of small apps without any line of code. And, this application gave us the ability to leverage the independence of QA team in order to maintain its environment.


We'd love to hear your opinion on this post