Tuesday, October 25, 2016

Jenkins Command Line Interface

Jenkins contains a built-in command line interface that allows us to access Jenkins from a Script or from our shell. This is convenient for automation of routine tasks. The interface is accessed via the Jenkins CLI client. The Client is a jar file distributed with Jenkins that we can download and use to communicate with the Jenkins Server. My Local Jenkins runs on

In order to download Jenkins-cli.jar we need to use the below url ( where Jenkins is running locally or remotely) like But before using the Jenkins cli jar we have some pre requisites that we do to use the Jenkins system on cli. Here are the checks that needs to be done before using the cli,

1. Jenkins service is running.
2.  Enable security option under “Configure Global Security”
Go to jenkins dashboard in Home page ( e.g http://localhost:8080/ ) -> Manage Jenkins
-> Configure Global Security -> Click on “Enable security” checkbox
You can also configure “Access Control” and “Authorization” option in Global Security page.

Jenkins allow us to trigger Jenkins build with any specific user, For that we have to pass username and password in command line.

Once we download the Jnekins jar file we can get the help as,
java -jar jenkins-cli.jar -s help
      Adds jobs to view.
          Builds a job, and optionally waits until its completion.
          Cancel the effect of the "quiet-down" command.

Version of the Jenkins – In Order to find the version of the Jenkins running, We can use the command line as,

java -jar jenkins-cli.jar -s version

Login to the Jenkins system – In order to perform some Operations  like Running builds, Installing plugin etc, a user must be logged in. This can done from command line as,

java -jar jenkins-cli.jar -s login --username jagadish12 --password jagadesh1982

This will not display any output but we can check whether the user logged in or not by using

java -jar jenkins-cli.jar -s who-am-i
Authenticated as: jagadish12

The above output tells us that the User is logged in to the Jenkins system.

Dealing with Jobs – Build Jobs are the most important one that we use in Jenkins. Many of the times we automate things like building jobs

Available Jobs - In order to find out the available jobs we can use the list-jobs as,
java -jar jenkins-cli.jar -s list-jobs

The above command will display the jobs currently available in Jenkins System.

Copy Job – In order to copy a Job from an existing one we can use the below command as,
java -jar jenkins-cli.jar -s copy-job jenkinsnexustesting sampleTest

This will create a job named sampleTest from existing job jenkinsnexustesting

Now run the list-jobs to see if the job has created successfully as,
java -jar jenkins-cli.jar -s list-jobs

We can see that the new job is created successfully.

Build Job – In order to build a job we can use the build command line as ,
java -jar jenkins-cli.jar -s  build sampleTest

This will starting running the job sampleTest.

Build Job with output - In order to check the Output of the Build job we can use the command as below,
java -jar jenkins-cli.jar -s  console sampleTest

Started from command line by ha:AAAAnB+LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT+vOD8nVc83PyU1x6OyILUoJzMv2y+/JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ+sxLJE/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAyuEgYB/dLi1CL9rMT0xJTM4gxDIwCWUQEexQAAAA==jagadish12
Building on master in workspace /var/lib/jenkins/jobs/sampleTest/workspace
Cloning the remote Git repository
 [INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building javaee7-simple-sample 1.11-SNAPSHOT
[INFO] ------------------------------------------------------------------------
ha:AAAAYB+LCAAAAAAAAP9b85aBtbiIQSmjNKU4P0+vJLE4u1gvPjexLDVPzxdEGvvmZ+X75ZekLlOVfvTjc8FPJgaGiiIGKaiG5Py84vycVD1nCA1SyAABjCCFBQCV27OjYAAAAA==[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ javaee7-simple-sample ---
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.386 s
[INFO] Finished at: 2016-10-21T01:02:13-04:00
[INFO] Final Memory: 9M/155M
[INFO] ------------------------------------------------------------------------

Get Job Configuration – Some times there may be cases where we need to check the Configuration settings for the Job as,

java -jar jenkins-cli.jar -s  get-job sampleTest > config.xml
[puppet@root$:~/Downloads/jenkins]$  ll
total 816
-rw-r--r-- 1 root root   2428 Oct 21 05:04 config.xml

The Configuration is saved as a XML file with a name (config.xml) that we pass as argument.

We can also run the get-job command to simply display the configuration details on the console as
java -jar jenkins-cli.jar -s get-job jenkinsnexustesting

Restore a Job – In order to Restore a saved job to a job named test3 from an XML file named config.xml we can use the command

java -jar jenkins-cli.jar -s   create-job test3 < config.xml  

Disable a Job – In order to disable a job we can use
java -jar jenkins-cli.jar -s   disable-job test3

Script Execution -  Jenkins cli not just allows us to run command provided by cli. It also allows us to execute scripts. The script needs to be written using the groovy language and the invocation is done using groovy command line.

Below is a script that will display the last successfully build job – succ.gsh

// Get the list of jobs that were SUCCESSFUL
// -- Get Active Jobs with 'Buildable' state
activeJobs = hudson.model.Hudson.instance.items.findAll{job -> job.isBuildable()}

// -- Get a subset of buildable jobs with result status of 'SUCCESSFUL'
goodRuns = activeJobs.findAll{job -> job.lastBuild != null && job.lastBuild.result == hudson.model.Result.SUCCESS}

// Do something with them - e.g. listing them
goodRuns.each{run -> println "Successful Job Name: ${}"}

This can be run as,
java -jar jenkins-cli.jar -s groovy succ.gsh
Successful Job Name: jenkinsnexustesting

We can see that the last successfully job is jenkinsnexustesting

Below is a another sample script which will create Nodes - create_node.gsh

import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
Jenkins.instance.addNode(new DumbSlave("test-script","test slave description","/tmp","1",Node.Mode.NORMAL,"test-slave-label",new JNLPLauncher(),new RetentionStrategy.Always(),new LinkedList()))

Now In order to run the script we can run the command line as ,
java -jar jenkins-cli.jar -s http://localhost:8080 groovy create_node.gsh

Creating Slave Node - In order to create Slave nodes we can use

cat <<EOF | java -jar jenkins-cli.jar -s create-node $2
> <slave>
>   <name>MasterNode</name>
>   <description></description>
>   <remoteFS>/tmp</remoteFS>
>   <numExecutors>1</numExecutors>
>   <mode>NORMAL</mode>
>   <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
>   <launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.5">
>       </launcher>
>   <label>mastertesting</label>
>   <nodeProperties/>
>   <userId>jagadish12</userId>
> </slave>

In the above command line we are using the underlying shell with Jenkins cli as above. We passed the Slave node configuration as an argument to the shell cat command. Now to
check whether our Node has been created or not we can use the Jenkins GUI or we can get the configuration of the created node as,

java -jar jenkins-cli.jar -s -noKeyAuth get-node MasterNode
<?xml version="1.0" encoding="UTF-8"?>
  <launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.9">

Installing a plugin – To install a plugin we can use the command line as,
java -jar jenkins-cli.jar -s install-plugin  -restart

Hope this helps you in using Jenkins CLI. More articles to come. Happy learning J
Read More

Tuesday, October 18, 2016

Integrating Jenkins With Sonar Qube

SonarQube (formerly Sonar) is an open source platform for continuous inspection of code quality. In this article we will see how we can integrate Sonar Qube in Jenkins. We will also build sample application which will use the integrated sonar Qube to test the code.

1. Sonar Qube Installation
a. Download the Sonar Qube with version 5.6.3
b. Unzip the sonar Qube
c. Go to <Sonar Qube>/bin/<OS-version>/
d. Run “sh console” command
e. Once the server is started, access the server from http://localhost:9000/ ( if you have a name to your system use that). This will be used in configuring the sonar qube servers in jenkins

2. Sonar Runner Installation
a. Download the Sonar-runner-2.4
b. Unzip the sonar runner
c. Don’t run the sonar-runner. This will be started from Jenkins.

Sonar Qube vs Sonar runner
SonarQube (formerly just "Sonar") is a server-based system. Of course you can install it on your local machine (the hardware requirements are minimal). But it is a central server with a database.
Analyses are performed by some Sonar "client" software, which could be the sonar runner, the sonar ant task, the sonar Eclipse plugin etc. The analysis results can be automatically uploaded to the server, where they can be accessed via the sonar Web application.

Once the Sonar Qube and Sonar runner are installed. We then need to install Sonar Plugin in the Jnekins

3. Download Jenkins Plugin
a. Download the Sonar Qube plugin 2.4.4 from Manage Jenkins => Manage Plugins

4. Configure the Jenkins – Sonar Qube Server
a. Configure System =>
    Under the Sonar Qube Server as,

Enter the Name.
Server URL – the one that we tried to access above when we started the Sonar qube

Note – Make sure to check the “Enable injection of sonarqube server configuration as build environment variables

5. Configure the Jenkins – Sonar Qube Scanner
 Under the Sonar Qube Scanner,

 Choose a new Sonar Qube Scanner Installation as,
Add the name and in the SONAR_RUNNER_HOME and pass the location of the unzip sonar-runner path above.

NOTE – also create a environment variable with SONAR_RUNNER_HOME variable with the location of the sonar-runner path

Save and Apply

6. Configure the Jenkins – Sonar Qube Project Properties file.
Every project that we want to check needs a file. The file can be a simple one by just passing the src location path. Here is a sample of the that I have

# must be unique in a given SonarQube instance
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.


# Encoding of the source code. Default is default system encoding

Save the file along with your source directory.
 7. Configure the Jenkins – Job
 Once the file is created, create a Job with the sonar qube as,
Check the “prepare SonarQube Scanner Environment”
In the Add pre-build Step , Choose the “Execute SonarQube Scanner”.
A new Item will be added and provide the path of the project’s “” file path as above.

Save and Run the Build. We can see the project Analysis at the sonar Qube Dashboard at http://localhost:9000/

That is all you need to do when configuring a Sonar Qube with Jenkins. More to Come. Happy Learning J
Read More