jenkins cli configuration-jenkins cli commands with examples
We can do all the things from jenkins gui ,but jenkins cli is very easier and faster and you can access jenkins from remote machine by using jenkins cli commands. by using jenkins cli we can list the jobs , we can run jenkins jobs from command line, we can run the jobs with parameters.
jenkins cli configuration
Install jenkins
Go to manage jenkins and go to configure global security and make sure that enabled security is checked
And save it
And go to manage jenkins and go to jenkins cli you will get jenkins cli commands window
or in browser <your jenkins url>/cli and enter it will take you to jenkins cli window
example http://18.205.24.13:8080/cli/
if you want some detailed information about jenkins cli click on documentation. It will give you official jenkins documentation
in this window you can see jenkins-cli.jar, download this jar file.
access jenkins cli commands from windows
to run the jenkins commands from your windows command prompt you need java so download java and install it. You can refer here how to install java in windows
after installing java on your windows system
open command prompt and go to the location where you downloaded the jenkins cli.war file and execute below jenkins cli commands
access jenkins cli commands from linux
to run the jenkins cli command in linux machine, you need java so install java in your linux machine. and download jenkins-cli.jar file. go to the location where you downloaded the jenkins-cli.jar file and execute below jenkins cli commands.
To list all jenkins commands
java -jar jenkins-cli.jar -s http://18.205.24.13:8080 -auth username:password
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth decodingdevops:devops
here http://18.205.24.13:8080/ is my jenkins url or path.
here decodingdevops is my username and devops is my password , here you have to enter your username:password
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:password
or you can use api token for password
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:12cb4a9876b0414fa8e9453e93a7d218
for api token of perticular user you can go to manage jenkins and go to manage users and clcik on configuration(gear) symbol. it will open new window like below image click on show API token, it will show you API token for that user.
when you are executing jenkins cli commands for the first time in windows if you get any error like JAVA is not recognized as an internal or external command see this.
jenkins cli commands with examples
To list all jenkins jobs
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:password list-jobs or java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:12cb4a9876b0414fa8e9453e93a7d218 list-jobs
here we added list-jobs. list-jobs will show jobs in jenkins, according to permissions given to the user.
if the user don't have the permissions then it will not show you the jobs.
run jenkins jobs from command line
after listing of jobs if you want to build any job you can use below command.
here iam having job name called abcd, now i will run the job by using below command.
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:password build jobname java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:password build abcd or java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:12cb4a9876b0414fa8e9453e93a7d218 build abcd
build abcd is added here(abcd is my job name)
see the console output after executing the job
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:password build abcd -s -v or java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:12cb4a9876b0414fa8e9453e93a7d218 build abcd –s –v
if you want to see console output you have to add -s -v to previous command.
run jenkins job from command line with parameters
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:password build abcd -p name=”value” java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:password build abcd -p dev=”jenkins cli commands”
to run parameterized jobs you have to add -p name="value"
in above command abcd is my job name and passing parameters with -p and my parameter is dev="jenkins cli commands"
jenkins safe-restart
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:password safe-restart java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth naresh:12cb4a9876b0414fa8e9453e93a7d218 safe-restart
here iam added safe-restart
to know more about perticular command
java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:12cb4a9876b0414fa8e9453e93a7d218 help <commandname> java -jar jenkins-cli.jar -s http://18.205.24.13:8080/ -auth username:12cb4a9876b0414fa8e9453e93a7d218 help build
if you execute above command it will give you more inforamtion about that perticular command