Pages

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 http://puppet.jas.com:8080/jnlpJars/jenkins-cli.jar. 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 http://puppet.jas.com:8080/ help
 add-job-to-view
      Adds jobs to view.
 build
          Builds a job, and optionally waits until its completion.
 cancel-quiet-down
          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 http://puppet.jas.com:8080/ version
1.651.3

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 http://puppet.jas.com:8080 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 http://puppet.jas.com:8080/ who-am-i
Authenticated as: jagadish12
Authorities:
  authenticated

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 http://puppet.jas.com:8080/ list-jobs
jenkinsnexustesting
Mint

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  http://puppet.jas.com:8080/ 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 http://puppet.jas.com:8080/ list-jobs
jenkinsnexustesting
Mint
sampleTest

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  http://puppet.jas.com:8080/  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  http://puppet.jas.com:8080/  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] ------------------------------------------------------------------------
[INFO] Building javaee7-simple-sample 1.11-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[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] BUILD SUCCESS
[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  http://puppet.jas.com:8080/  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 http://puppet.jas.com:8080/ 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  http://puppet.jas.com:8080/   create-job test3 < config.xml  

Disable a Job – In order to disable a job we can use
java -jar jenkins-cli.jar -s http://puppet.jas.com:8080/   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: ${run.name}"}

This can be run as,
java -jar jenkins-cli.jar -s http://172.16.202.95:8080 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 http://puppet.jas.com:8080/ 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>
> EOF

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  http://172.16.202.95:8080/ -noKeyAuth get-node MasterNode
<?xml version="1.0" encoding="UTF-8"?>
<slave>
  <name>MasterNode</name>
  <description></description>
  <remoteFS>/tmp</remoteFS>
  <numExecutors>1</numExecutors>
  <mode>NORMAL</mode>
  <launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.9">
          <port>0</port>
  </launcher>
  <label>mastertesting</label>
  <nodeProperties/>
  <userId>jagadish12</userId>
</slave>

Installing a plugin – To install a plugin we can use the command line as,
java -jar jenkins-cli.jar -s  http://puppet.jas.com:8080/ install-plugin 
http://updates.jenkins-ci.org/latest/build-monitor-plugin.hpi  -restart

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

4 comments :

  1. I would like to thank you for the efforts you have put in penning this blog.

    I'm hoping to see thhe same high-grade blogg posts by you later on as well.
    In truth, your creative writing abilities has encouraged me to gett my very own website
    now ;)

    ReplyDelete
  2. Hey there would you mind letting me know which web host you're working
    with? I've loaded your blog in 3 different internet browsers and I
    must say this blog loads a lot quicker then most.
    Can you recommend a good internet hosting provider at a
    reasonable price? Cheers, I appreciate it!

    ReplyDelete