Jenkins command line

Jenkins command line

Jenkins command line or Jenkins CLI is a tool by which we can manage Jenkins task from the command line. Its very user-friendly approach to handling Jenkins day to day task it offers several features like create a job, run job, export job, take console output, create a view, install the plugin and much more.

By Jenkins CLI it is easy to call Jenkins job from any script and take the console output and perform complex tasks. Jenkins CLI require less memory as compare to Jenkins GUI. Jenkins has a built-in command line interface called Jenkins CLI that allows the user to access Jenkins from a script or from your shell. This is convenient for bulk updates, automation of routine tasks, trouble diagnosis, and so on.

jenkins CLI require JAVA JAR file to set up and run Jenkins task. You can download the JAR file for the client from the URL “http://localhost/jnlpJars/jenkins-cli.jar” on your Jenkins server,

To configure Jenkins CLI some prerequisite needs to set up.

  • Install Jenkins either on windows or Linux.
  • Jenkins service should up and running.
  • Enable security option under “Configure Global Security”
    Go to Jenkins dashboard in Homepage ( e.g http://localhost:8080/ ) -> Manage Jenkins
    -> Configure Global Security -> Click on “Enable security” checkbox
  • Take the Jenkins User ID and API Token for the user.
  •  Go to Jenkins dashboard in Homepage ( e.g http://localhost:8080/ ) and click to the username and go to configure and select the API token.

jenkins-cli

 

Download Jenkins JAR FIle wget http://localhost:8080/jenkins/jnlpJars/jenkins-cli.jar

Note In the remainder of this document I assume the Jenkins URL is http://localhost:8080/jenkins. You should, of course, change this to the URL you use for your Jenkins instance.

For more Jenkins CLI command details refer to http://localhost:8080/cli/ which show all the available commands and there use.

First, save the Jenkins User ID and API token in some file

jenkins_user:fc3fedfdc32334e9c1345edf43359df4 > jenkins_api_token

Jenkins CLI Examples:-

1. Connect and authenticate with Jenkins Server and show help.

java -jar jenkins-cli.jar -s http://localhost:8080/ help

2. Find the User details by running who am I command.

java -jar ./jenkins-cli.jar -s http://localhost:8080 -auth @jenkins_api_token who-am-i

3. Show the existing job list.

java -jar ./jenkins-cli.jar -s http://localhost:8080 -auth @jenkins_api_token list-jobs

4. Run the existing build (test1).

java -jar ./jenkins-cli.jar -s http://localhost:8080 -auth @jenkins_api_token build test1

5. Get the existing build (test2) configuration file in XML format.

java -jar ./jenkins-cli.jar -s http://localhost:8080 -auth @jenkins_api_token get-job test2

6. Run the Existing Build (test2) with the parameter like (Choice parameter and String parameter). In below command test2 is my build name and Centos is my Choice parameter.

java -jar ./jenkins-cli.jar -s http://localhost:8080 -auth @jenkins_api_token build test2 -s -v -p env=Centos

jenkins-cli_1

-c  : Check for SCM changes before starting the build, and if there’s no change, exit without doing a build.
-f  : Follow the build progress. Likes only interrupt are not passed through to the build.
-p  : Specify the build parameters in the key=value format.
-s  : Wait until the completion/abortion of the command. Interrupts are passed through to the build.
-v  : Prints out the console output of the build. Use with -s
-w  : Wait until the start of the command.

7. Show the console output after run job.

java -jar Jenkins-cli.jar -s http://localhost:8080/ -auth @jenkins_api_token console test2 -n 10

JOB: Name of the job
BUILD: Build number or permalink to point to the build. Defaults to the last
build
-f : If the build is in progress, stay around and append console output as
it comes, like ‘tail -f’
-n N : Display the last N lines

8. Copy build.

java -jar jenkins-cli.jar -s http://localhost:8080/ -auth @jenkins_api_token console copy-job SRC DST

Copies a job.

SRC : Name of the job to copy
DST : Name of the new job to be created.

9. Connect the Node.

java -jar Jenkins-cli.jar -s http://localhost:8080/ -auth @jenkins_api_token console connect-node NAME ... [-f]

Reconnect to a node(s)

NAME Slave name or empty string for master; the comma-separated list is
supported
-f : Cancel any currently pending connect operation and retry from scratch

10. Restart Jenkins.

java -jar Jenkins-cli.jar -s http://localhost:8080/ -auth @jenkins_api_token restart

So these are some example of Jenkins CLI try in your environment.

You May Also Like

One thought on “Jenkins command line

  1. My partner and I stumbled over here by a different website and thought I might check things
    out. I like what I see so now i am following you.
    Look forward to finding out about your web page again.

Leave a Reply

Your email address will not be published. Required fields are marked *