What it is
Jenkins CLI is a command-line interface for interacting with and managing a Jenkins instance, useful for scripting administrative tasks, triggering builds, and retrieving build information.
Installation
Linux / macOS
- Download the
jenkins-cli.jarfile from your Jenkins instance: Navigate tohttp://<your-jenkins-url>/jnlpJars/jenkins-cli.jarand save the file. - Place the
jenkins-cli.jarin a convenient location (e.g.,~/bin/). - Make it executable:
chmod +x ~/bin/jenkins-cli.jar - You can then invoke it using
java -jar ~/bin/jenkins-cli.jar. For convenience, create an alias or a shell script.
Windows
- Download the
jenkins-cli.jarfile from your Jenkins instance: Navigate tohttp://<your-jenkins-url>/jnlpJars/jenkins-cli.jarand save the file. - Place the
jenkins-cli.jarin a convenient location (e.g.,C:\JenkinsCLI\). - You can then invoke it using
java -jar C:\JenkinsCLI\jenkins-cli.jar.
Authentication
The CLI uses either API tokens or SSH keys for authentication.
API Token:
- In Jenkins, go to
Your username>Configure>API Token. - Click
Add new Token, give it a name (e.g.,cli_token), and copy the generated token. - When using the CLI, you’ll typically provide credentials like:
Or set them via environment variables:java -jar jenkins-cli.jar -auth <your-username>:<your-api-token> ...export JENKINS_USER=<your-username> export JENKINS_API_TOKEN=<your-api-token> java -jar jenkins-cli.jar ...
SSH Key:
- Generate an SSH key pair if you don’t have one:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/jenkins_cli_id_rsa - Add the public key (
~/.ssh/jenkins_cli_id_rsa.pub) to your Jenkins user’s~/.ssh/authorized_keysfile. - When using the CLI, specify the private key:
java -jar jenkins-cli.jar -ssh -i ~/.ssh/jenkins_cli_id_rsa ...
Core Concepts
- Jenkins URL: The base URL of your Jenkins instance (e.g.,
http://localhost:8080). - Credentials: How the CLI authenticates with Jenkins (username/API token or SSH key).
- Command Namespace: Commands are often grouped under namespaces (e.g.,
build,node,plugin). - Item Name: Refers to Jenkins jobs, folders, or other configuration items.
Commands / Usage
All commands are invoked via java -jar jenkins-cli.jar [options] <command>.
Connection Options
These options are global and usually specified first.
-
Specify Jenkins URL:
java -jar jenkins-cli.jar -s http://localhost:8080 ...Specifies the Jenkins server URL.
-
Basic Authentication (Username/API Token):
java -jar jenkins-cli.jar -auth <username>:<api-token> -s http://localhost:8080 ...Authenticates using username and API token.
-
SSH Authentication (Private Key):
java -jar jenkins-cli.jar -ssh -i ~/.ssh/jenkins_cli_id_rsa -s http://localhost:8080 ...Authenticates using an SSH private key.
-
Timeout:
java -jar jenkins-cli.jar -timeout 60000 -s http://localhost:8080 ...Sets the connection timeout in milliseconds.
Building Jobs
Triggering builds and checking their status.
-
Build a job:
java -jar jenkins-cli.jar -s http://localhost:8080 build my-jobTriggers a build for the job named
my-job. -
Build a job with parameters:
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job -p PARAM1=value1 -p PARAM2=value2Triggers a build for
my-jobwith specified build parameters. -
Build a job and wait for completion:
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job -wTriggers a build for
my-joband waits until it’s finished. -
Build a job and wait, returning exit code based on build status:
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job -w -vWaits for the build to complete and returns a non-zero exit code if the build failed or was unstable.
-
Build a job in a folder:
java -jar jenkins-cli.jar -s http://localhost:8080 build "My Folder/my-job"Triggers a build for a job located within a folder.
-
Get build status:
java -jar jenkins-cli.jar -s http://localhost:8080 get-build my-job 5Retrieves the status of build number 5 for
my-job. -
Get the latest build status:
java -jar jenkins-cli.jar -s http://localhost:8080 get-build my-jobRetrieves the status of the last completed build for
my-job. -
Get build console output:
java -jar jenkins-cli.jar -s http://localhost:8080 console my-job 10Retrieves the console output for build number 10 of
my-job. -
Get latest build console output:
java -jar jenkins-cli.jar -s http://localhost:8080 console my-jobRetrieves the console output for the last completed build of
my-job.
Job Management
Creating, deleting, and configuring jobs.
-
List jobs in the root folder:
java -jar jenkins-cli.jar -s http://localhost:8080 list-jobsLists all top-level jobs.
-
List jobs in a folder:
java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs "My Folder"Lists jobs within the specified folder.
-
Create a job from an XML file:
java -jar jenkins-cli.jar -s http://localhost:8080 create-job my-new-job < /path/to/config.xmlCreates a new job named
my-new-jobusing the configuration fromconfig.xml. -
Update a job from an XML file:
java -jar jenkins-cli.jar -s http://localhost:8080 update-job my-job < /path/to/updated_config.xmlUpdates the configuration of
my-jobwith the content fromupdated_config.xml. -
Get job configuration:
java -jar jenkins-cli.jar -s http://localhost:8080 get-job my-job > my-job-backup.xmlRetrieves the XML configuration for
my-joband saves it to a file. -
Delete a job:
java -jar jenkins-cli.jar -s http://localhost:8080 delete-job my-old-jobDeletes the job named
my-old-job. -
Copy a job:
java -jar jenkins-cli.jar -s http://localhost:8080 copy-job my-job my-job-copyCopies
my-jobto a new job namedmy-job-copy. -
Enable a job:
java -jar jenkins-cli.jar -s http://localhost:8080 enable-job my-jobEnables the job
my-job. -
Disable a job:
java -jar jenkins-cli.jar -s http://localhost:8080 disable-job my-jobDisables the job
my-job.
Node Management (Agents)
Managing Jenkins agents.
-
List all nodes (agents and master):
java -jar jenkins-cli.jar -s http://localhost:8080 list-nodesLists all connected nodes.
-
Get node information:
java -jar jenkins-cli.jar -s http://localhost:8080 show-node myslaveDisplays detailed information about the node named
myslave. -
Delete a node:
java -jar jenkins-cli.jar -s http://localhost:8080 delete-node myslaveDeletes the node named
myslave. -
Disconnect a node:
java -jar jenkins-cli.jar -s http://localhost:8080 disconnect-node myslaveDisconnects the node
myslavefrom Jenkins. -
Connect a node:
java -jar jenkins-cli.jar -s http://localhost:8080 connect-node myslaveConnects the node
myslaveto Jenkins.
Plugin Management
Installing and managing plugins.
-
List installed plugins:
java -jar jenkins-cli.jar -s http://localhost:8080 list-pluginsLists all installed plugins and their versions.
-
Install a plugin:
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin gitInstalls the Git plugin.
-
Install a plugin with a specific version:
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin workflow-aggregator:2.6Installs version 2.6 of the Workflow Aggregator plugin.
-
Install a plugin from a URL:
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin http://updates.jenkins-ci.org/download/plugins/ssh-agent/1.13/ssh-agent.hpiInstalls the SSH Agent plugin from a direct URL.
-
Uninstall a plugin:
java -jar jenkins-cli.jar -s http://localhost:8080 uninstall-plugin mailerUninstalls the Mailer plugin.
System Information and Management
Retrieving system status and performing administrative actions.
-
Get Jenkins version:
java -jar jenkins-cli.jar -s http://localhost:8080 versionPrints the Jenkins version.
-
Get system information:
java -jar jenkins-cli.jar -s http://localhost:8080 system-infoPrints detailed system information.
-
Get Jenkins configuration:
java -jar jenkins-cli.jar -s http://localhost:8080 get-configRetrieves the global Jenkins configuration XML.
-
Update Jenkins configuration:
java -jar jenkins-cli.jar -s http://localhost:8080 update-config < /path/to/jenkins.xmlUpdates the global Jenkins configuration from an XML file.
-
Restart Jenkins:
java -jar jenkins-cli.jar -s http://localhost:8080 restartRestarts the Jenkins instance.
-
Safe Restart Jenkins:
java -jar jenkins-cli.jar -s http://localhost:8080 safe-restartInitiates a safe restart, allowing current builds to complete.
-
Reload Jenkins configuration:
java -jar jenkins-cli.jar -s http://localhost:8080 reloadReloads the Jenkins configuration from disk.
-
Shutdown Jenkins:
java -jar jenkins-cli.jar -s http://localhost:8080 shutdownShuts down the Jenkins instance.
Script Console
Executing Groovy scripts on the Jenkins master.
-
Execute a Groovy script:
java -jar jenkins-cli.jar -s http://localhost:8080 groovy -f /path/to/script.groovyExecutes a Groovy script file.
-
Execute a Groovy script with inline code:
java -jar jenkins-cli.jar -s http://localhost:8080 groovy "println 'Hello from CLI'"Executes the given Groovy code directly.
User Management
Managing Jenkins users.
-
List users:
java -jar jenkins-cli.jar -s http://localhost:8080 list-usersLists all Jenkins users.
-
Get user information:
java -jar jenkins-cli.jar -s http://localhost:8080 get-user johndoeRetrieves details for the user
johndoe. -
Create a user:
java -jar jenkins-cli.jar -s http://localhost:8080 create-user jane.doe --password secretpassword --full-name "Jane Doe" --email jane.doe@example.comCreates a new user with specified details.
-
Update a user:
java -jar jenkins-cli.jar -s http://localhost:8080 update-user john.doe --email john.doe@example.comUpdates the email for the user
john.doe. -
Delete a user:
java -jar jenkins-cli.jar -s http://localhost:8080 delete-user olduserDeletes the user
olduser.
Credentials Management
Managing Jenkins credentials.
-
List credentials in a domain:
java -jar jenkins-cli.jar -s http://localhost:8080 list-credentials system::system::JenkinsLists credentials in the global Jenkins domain.
-
List credentials in a folder:
java -jar jenkins-cli.jar -s http://localhost:8080 list-credentials "My Folder"Lists credentials within a specific folder.
-
Create username/password credential:
java -jar jenkins-cli.jar -s http://localhost:8080 create-credentials-binding username_password --id my-user-pass --username admin --password 'supersecret' --description "Admin credentials"Creates a new username/password credential.
-
Create secret text credential:
java -jar jenkins-cli.jar -s http://localhost:8080 create-credentials-binding secret_text --id my-secret-token --secret "s3cr3tT0k3n" --description "API Token"Creates a new secret text credential.
-
Delete credential:
java -jar jenkins-cli.jar -s http://localhost:8080 delete-credential system::system::Jenkins::my-user-passDeletes the specified credential.
Other Useful Commands
-
Help:
java -jar jenkins-cli.jar -s http://localhost:8080 helpDisplays help information for all commands.
-
Help for a specific command:
java -jar jenkins-cli.jar -s http://localhost:8080 help buildDisplays help for the
buildcommand. -
Get Jenkins status:
java -jar jenkins-cli.jar -s http://localhost:8080 get-jenkins-statusRetrieves the current status of the Jenkins instance.
-
Get Jenkins uptime:
java -jar jenkins-cli.jar -s http://localhost:8080 get-jenkins-uptimeRetrieves the uptime of the Jenkins instance.
Common Patterns
-
Triggering a build and tailing its output in real-time:
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job -w -v && echo "Build finished successfully" || echo "Build failed!"This command builds
my-job, waits for completion (-w), and returns a non-zero exit code if the build fails (-v). The&&and||operators provide immediate feedback on the build status. -
Automating plugin installation and Jenkins restart:
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin git java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin workflow-aggregator java -jar jenkins-cli.jar -s http://localhost:8080 safe-restartInstalls two plugins and then performs a safe restart to apply the changes.
-
Backing up job configurations:
mkdir jenkins_job_backups java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs | while read job; do java -jar jenkins-cli.jar -s http://localhost:8080 get-job "$job" > "jenkins_job_backups/${job}.xml" echo "Backed up job: $job" doneThis script iterates through all jobs, retrieves their XML configuration, and saves each to a separate file in a
jenkins_job_backupsdirectory. -
Executing a script to update multiple jobs:
# script.groovy # import hudson.model.* // Jenkins.instance.getAllItems(AbstractProject.class).each { project -> // if (project.name.startsWith("legacy-")) { // project.setQuietPeriod(5) // project.save() // println "Updated ${project.name}" // } // } java -jar jenkins-cli.jar -s http://localhost:8080 groovy -f update_jobs.groovyThis executes a Groovy script that modifies properties of jobs matching a certain pattern.
-
Creating a folder and a job within it:
# folder_config.xml # <?xml version='1.1' encoding='UTF-8'?> # <com.cloudbees.hudson.plugins.folder.Folder plugin="cloudbees-folder@6.16"> # <properties/> # <icon class="com.cloudbees.hudson.plugins.folder.icons.StockFolderIcon"/> # </com.cloudbees.hudson.plugins.folder.Folder> # job_config.xml # <?xml version='1.1' encoding='UTF-8'?> # <org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition plugin="workflow-cps@2698.v7952246085b_c"> # <scm class="hudson.scm.NullSCM"/> # <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition"> # <script>pipeline { agent any stages { stage('Hello') { steps { echo 'World' } } } }</script> # <sandbox>true</sandbox> # </definition> # <triggers/> # <quietPeriod>4</quietPeriod> # <properties/> # </org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition> java -jar jenkins-cli.jar -s http://localhost:8080 create-job "My New Folder" < folder_config.xml java -jar jenkins-cli.jar -s http://localhost:8080 create-job "My New Folder/my-pipeline-job" < job_config.xmlThis sequence creates a Jenkins folder and then creates a Pipeline job inside that folder.
Gotchas
- JAR File Location: Always ensure
jenkins-cli.jaris accessible and the path is correct. Usingjava -jar /path/to/jenkins-cli.jaris safer than assuming it’s in your PATH. - Authentication Methods: Mixing
-authand-sshis not allowed. Choose one method and stick to it. Environment variables (JENKINS_USER,JENKINS_API_TOKEN) can simplify repeated commands. - Item Names with Spaces: Job names or folder names containing spaces must be quoted, e.g.,
"My Folder/My Job". - Script Console Security: Be extremely cautious when running arbitrary Groovy scripts via
groovy. Malicious scripts can compromise your Jenkins instance. Always review scripts thoroughly. - Plugin Installation Dependencies: Installing plugins might require other plugins to be installed first. The CLI might not automatically resolve all dependencies, leading to errors. Check Jenkins logs for details.
- Job XML Format: Ensure the XML configuration files used for
create-jobandupdate-jobare valid and compatible with your Jenkins version and installed plugins. - Permissions: The user credentials used for authentication must have the necessary permissions in Jenkins to perform the requested actions (e.g., triggering builds, deleting jobs, managing nodes).
- Long-Running Commands: Commands like
build -wcan hang if Jenkins becomes unresponsive. Use timeouts (-timeout) where appropriate. - CLI Version Compatibility: While generally backward compatible, it’s best practice to use a
jenkins-cli.jarversion that closely matches your Jenkins server version for optimal compatibility.