Jenkins CLI, your one stop shop for a ‘quicky’ with Jenkins.
Jenkins is an amazing CI tool which allows us to run tasks via jobs. It allows us to runs automated jobs over time. This allows developers to understand how stable their code base is. Along side this, Jenkins is one of the most popular CI tools around today. With this said, what role does Jenkins CLI play?
Jenkins CLI Setup
Great, let’s try to setup Jenkins CLI locally first. This post assume that you have already installed Jenkins on your machine. If not then you may want to firstly install Jenkins on your machine.
Once you have Jenkins up and running, navigate to the Jenkins Dashboard. Once there, we need to firstly setup a few quick things.
Configure Global Setting
For the purpose of this post, let’s allow any user to be able to do anything. Ensure the ‘Allow anyone to do anything’ radio button is selected.
You will most likely need to know what your passphrase is for the purpose of this post. To get this, navigate to your user name in the header of Jenkins and select the ‘Configure’ option. Once on the ‘User Configuration’ page, scroll down until you see the value of ‘SSH Public Key’. Make a note of the key.
Now that we have our passphrase and a given user is able to do anything, let’s try to access the CLI. Navigate back to the ‘Manage Jenkins’ and select the ‘Jenkins CLI’ option.
In order to run Jenkins CLI, we need to download then ‘jenkins-cli.war’ file. Fortunately for us, the link is provided in the Jenkins CLI page. Download the war file and navigate to the folder where you downloaded it. I would suggest that you place the ‘jenkins-cli.war’ file in the same directory as your ‘jenkins.war’ file.
Running Jenkins CLI
With the file now downloaded, let’s try to run it. War files as just another type of Java file, this means we can simply run it using java commands. To run the file, we provide commands in the following format.
Java -jar Jenkins-cli.jar -s [JENKINSHOST] [COMMAND]
Let’s try to print all the help commands for Jenkins CLI. Try running the following below:
Java -jar Jenkins-cli.jar -s http://localhost:8080/ help
This should now return a list of all the available help commands below:
Jenkins CLI Commands
Now that you are able to run commands in Jenkins CLI, let’s try to run some of the commands below:
Java -jar Jenkins-cli.jar -s http://localhost:8080/ restart
List All Jenkins Jobs
Java -jar Jenkins-cli.jar -s http://localhost:8080/ list-jobs
Run A Jenkins Build
Java -jar Jenkins-cli.jar -s http://localhost:8080/ build <job-name>
Jenkins CLI is a quick way of accessing the same builds, the same jobs and services but much quicker. As opposed to traversing the GUI of Jenkins, you can instead perform the same actions by some simple commands.