Using Jenkins CLI
1. Downloading Jenkins CLI
| Method | Description | Notes |
|---|---|---|
| Jenkins Web UI | Go to http://<your-jenkins-url>/cli |
Download the jenkins-cli.jar file from the web UI. |
| Command Line | curl -s http://<your-jenkins-url>/jnlpJars/jenkins-cli.jar -o jenkins-cli.jar |
Use curl or wget to download the CLI JAR file. |
| Jenkins Plugin | Install the cli plugin via Jenkins Plugin Manager |
Enables CLI access through the web UI and command-line tools. |
Example: Downloading CLI via curl
curl -s http://jenkins.example.com/jnlpJars/jenkins-cli.jar -o jenkins-cli.jar
Note: Ensure that the Jenkins server allows CLI access and that you have appropriate permissions to download the CLI JAR.
2. Authenticating with CLI
| Method | Description | Notes |
|---|---|---|
| jenkins-cli.jar | Download the CLI JAR file from your Jenkins instance. | Available at http://your-jenkins-url/jnlpJars/jenkins-cli.jar |
| CLI Token | Generate a CLI token from your Jenkins user profile. | Go to http://your-jenkins-url/user/username/configure and find the "API Token" section. |
| SSH Key Authentication | Use SSH keys for secure authentication. | Requires Jenkins SSH plugin and configured SSH keys. |
Example: Using CLI Token
java -jar jenkins-cli.jar -s http://your-jenkins-url/ -remoting -auth username:token <command>
Note: Ensure the CLI token has appropriate permissions to execute commands.
Warning: Never expose your CLI token in logs or public repositories.
3. Creating Jobs via CLI
| Command | Description | Example |
|---|---|---|
| jenkins-cli create-job | Creates a new job from a configuration file | jenkins-cli -s http://localhost:8080 create-job my-new-job < job-config.xml |
| jenkins-cli get-job | Retrieves the configuration of an existing job | jenkins-cli -s http://localhost:8080 get-job my-new-job > job-config.xml |
Example: Creating a New Job
jenkins-cli -s http://localhost:8080 create-job my-new-job < job-config.xml
Note: The job configuration must be in XML format compatible with Jenkins.
4. Triggering Builds
| Command | Description | Parameters | Notes |
|---|---|---|---|
| build | Trigger a build for a specific job | job-name |
Triggers the latest build of the specified job |
| build -s | Trigger a build and wait for completion | job-name |
Blocks until the build completes or fails |
| build -p | Trigger a build with parameters | job-name, param1=value1 |
Allows passing build parameters to the job |
Example: Trigger a parameterized build
jenkins-cli -s http://jenkins.example.com build -p my-job param1=value1 param2=value2
Note: The Jenkins CLI requires authentication via a token or session cookie. Ensure the user has appropriate permissions to trigger builds.
5. Managing Plugins via CLI
| Command | Description | Options | Notes |
|---|---|---|---|
| jenkins-cli get-plugins | Lists all installed plugins | -s <server-url> |
Displays plugin name, version, and status |
| jenkins-cli install-plugin | Installs a plugin from the Jenkins Plugin Manager | -s <server-url>, -name <plugin-name> |
Can install a specific plugin by name |
| jenkins-cli uninstall-plugin | Uninstalls a plugin from Jenkins | -s <server-url>, -name <plugin-name> |
Removes plugin and its dependencies if not used elsewhere |
| jenkins-cli update-plugin | Updates a plugin to the latest version | -s <server-url>, -name <plugin-name> |
Requires plugin to be installed |
Example: Install a Plugin via CLI
java -jar jenkins-cli.jar -s http://localhost:8080/ install-plugin email-ext</code>
</pre>
</div>
</div>
<div class="example">
<h2 id="example-list-installed-plugins">Example: List Installed Plugins<a href="#example-list-installed-plugins" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<div class="codeBlock" data-language="bash">
<pre><code>java -jar jenkins-cli.jar -s http://localhost:8080/ get-plugins</code>
</pre>
</div>
</div>
<h2 id="6-getting-job-configuration">6. Getting Job Configuration<a href="#6-getting-job-configuration" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<table>
<tbody>
<tr>
<th>Command</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td class="property">get-job</td>
<td>Retrieves the configuration XML of a job</td>
<td><code>java -jar jenkins-cli.jar -s http://localhost:8080/ get-job job-name</code></td>
</tr>
</tbody>
</table>
<div class="note"><strong>Note:</strong> The output is the full XML configuration of the job, which can be used to recreate or migrate jobs.</div>
<h2 id="7-updating-job-configuration">7. Updating Job Configuration<a href="#7-updating-job-configuration" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<table>
<tbody>
<tr>
<th>Property</th>
<th>Values</th>
<th>Description</th>
</tr>
<tr>
<td class="property">job.name</td>
<td><code>my-job</code></td>
<td>Name of the job to update</td>
</tr>
<tr>
<td class="property">config.xml</td>
<td><code>path/to/config.xml</code></td>
<td>Path to the new configuration XML file</td>
</tr>
</tbody>
</table>
<div class="example">
<h2 id="example-update-job-configuration-using-cli">Example: Update job configuration using CLI<a href="#example-update-job-configuration-using-cli" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<div class="codeBlock" data-language="bash">
<pre><code>java -jar jenkins-cli.jar -s http://localhost:8080/ -auth admin:password set-job-config my-job /path/to/new-config.xml
Note: Ensure the XML file is valid and compatible with the Jenkins version.
8. Managing Nodes via CLI
| Command | Description | Options |
|---|---|---|
| jenkins-cli get-node | Retrieves information about a specific node | -s <server-url>, -i <node-name> |
| jenkins-cli create-node | Creates a new node with specified configuration | -s <server-url>, -i <node-name>, -f <config-file> |
| jenkins-cli delete-node | Deletes a node from Jenkins | -s <server-url>, -i <node-name> |
| jenkins-cli update-node | Updates an existing node's configuration | -s <server-url>, -i <node-name>, -f <config-file> |
Example: Create a new node via CLI
java -jar jenkins-cli.jar -s http://localhost:8080/ -i my-node create-node -f node-config.xml
Example: Retrieve node information
java -jar jenkins-cli.jar -s http://localhost:8080/ -i my-node get-node
Note: Node configuration files must be in XML format compatible with Jenkins' node definition schema.
9. Using Groovy Scripts
Jenkins provides extensive support for Groovy scripts, which can be used for automation, configuration management, and advanced scripting within the Jenkins environment.
| Script Type | Description | Usage |
|---|---|---|
| Jenkins Script Console | Allows running Groovy scripts directly in Jenkins UI | Access via Manage Jenkins > Script Console |
| Jenkinsfile | Declarative or Scripted Pipeline using Groovy syntax | Defined in Jenkinsfile within project root |
| Groovy Shared Libraries | Reusable Groovy code across multiple pipelines | Defined in vars/ directory within library |
Example: Groovy Script to List All Jobs
import jenkins.model.Jenkins
def jenkins = Jenkins.instance
jenkins.jobs.each { job ->
println "Job Name: " + job.name
}
Example: Groovy Script to Get Job Build Info
import jenkins.model.Jenkins
def jobName = "my-job"
def job = Jenkins.instance.getItem(jobName)
if (job != null) {
def lastBuild = job.getLastBuild()
if (lastBuild != null) {
println "Last Build Number: " + lastBuild.number
println "Build Status: " + lastBuild.result.toString()
}
}
Note: Groovy scripts in Jenkins can access the full Jenkins API and are powerful for automation but should be used with caution due to their ability to modify system settings or execute arbitrary code.
10. Listing Available Commands
| Command | Description | Usage |
|---|---|---|
| cli help | Lists all available CLI commands | java -jar jenkins-cli.jar -s http://localhost:8080/ help |
| cli help <command> | Shows detailed help for a specific command | java -jar jenkins-cli.jar -s http://localhost:8080/ help build |