> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Automating Jenkins using CLI and APIs

> Automating Jenkins tasks via CLI and REST API enhances efficiency and consistency in CI/CD pipelines, integrating seamlessly into automation workflows.

Automating Jenkins tasks via the CLI and REST API delivers repeatability, consistency, and efficiency to your CI/CD pipelines. While the web UI is great for manual interactions, scripting with the Jenkins CLI (SSH or JAR) and the REST API enables you to integrate Jenkins seamlessly into your automation workflows.

## Table of Contents

1. [Jenkins CLI over SSH](#jenkins-cli-over-ssh)\
   1.1 [Enable SSH Endpoint](#enable-ssh-endpoint)\
   1.2 [List Available Commands](#list-available-commands)\
   1.3 [Trigger a Job via SSH](#trigger-a-job-via-ssh)
2. [Jenkins CLI Client (jenkins-cli.jar)](#jenkins-cli-client-jenkins-clijar)
3. [Jenkins REST API](#jenkins-rest-api)\
   3.1 [Install a Plugin](#install-a-plugin)\
   3.2 [Authentication Methods](#authentication-methods)
4. [References](#references)

***

## Jenkins CLI over SSH

Jenkins includes a built-in CLI accessible over SSH. This approach avoids additional HTTP calls and works even behind strict firewalls.

### Enable SSH Endpoint

By default, SSH is disabled. You can reveal the SSH endpoint with:

```bash theme={null}
curl -Lv https://JENKINS_URL/login 2>&1 | grep -i 'x-ssh-endpoint'
# Example output:
# < X-SSH-Endpoint: localhost:53801
```

Next, add your SSH public key in **Jenkins » People » YourUser » Configure**.

<Callout icon="lightbulb" color="#1CB2FE">
  Make sure your SSH key is correctly formatted (RFC4716 or OpenSSH) before pasting into Jenkins.
</Callout>

### List Available Commands

Once SSH is configured, list all CLI commands:

```bash theme={null}
ssh -l username -p 53801 localhost help
```

### Trigger a Job via SSH

Build a job named `hello-kodekloud` and stream console output:

```bash theme={null}
ssh -l username -p 53801 localhost build hello-kodekloud -f -v
```

Sample output:

```bash theme={null}
Started hello-kodekloud #1
Building in workspace /tmp/jenkins/workspace/hello-kodekloud
+ echo Hello KodeKloud
Hello KodeKloud
Finished: SUCCESS
```

***

## Jenkins CLI Client (jenkins-cli.jar)

If SSH isn’t an option, download the `jenkins-cli.jar` from your Jenkins master:

```bash theme={null}
wget http://JENKINS_URL/jnlpJars/jenkins-cli.jar
```

Use HTTP or WebSocket transport:

```bash theme={null}
java -jar jenkins-cli.jar -s http://JENKINS_URL/ -webSocket help
```

Common commands:

```bash theme={null}
# List all jobs
java -jar jenkins-cli.jar -s JENKINS_URL -auth user:APITOKEN list-jobs

# Trigger a build
java -jar jenkins-cli.jar -s JENKINS_URL -auth user:APITOKEN build my-job

# Fetch job configuration (XML)
java -jar jenkins-cli.jar -s JENKINS_URL -auth user:APITOKEN get-job my-job
```

***

## Jenkins REST API

The [Jenkins REST API](https://www.jenkins.io/doc/book/using/remote-access-api/) provides HTTP endpoints for jobs, nodes, plugins, and more.

### Install a Plugin

Use `curl` to install or update plugins:

```bash theme={null}
curl -s -X POST \
  --data '<jenkins><install plugin="git@4.10.0" /></jenkins>' \
  -H 'Content-Type: text/xml' \
  http://JENKINS_URL/pluginManager/installNecessaryPlugins \
  --user admin:${JENKINS_TOKEN}
```

### Authentication Methods

Jenkins supports multiple authentication schemes. Use API tokens to avoid exposing plain passwords.

| Method     | Usage                              | Pros               | Cons                               |
| ---------- | ---------------------------------- | ------------------ | ---------------------------------- |
| Basic Auth | `--user user:password`             | Simple setup       | Exposes credentials in scripts     |
| API Token  | `--user user:APITOKEN`             | Secure, revocable  | Requires token generation per user |
| SSH Key    | SSH transport for CLI (`ssh -l …`) | Key-based security | Needs SSH endpoint enabled in UI   |

<Callout icon="triangle-alert" color="#FF6B6B">
  Never commit credentials or API tokens into version control. Use environment variables or secret managers.
</Callout>

***

## References

* [Jenkins CLI Documentation](https://www.jenkins.io/doc/book/managing/cli/)
* [Jenkins Remote Access API](https://www.jenkins.io/doc/book/using/remote-access-api/)
* [Managing Jenkins Plugins](https://www.jenkins.io/doc/book/managing/plugins/)

Use these methods to automate job creation, builds, plugin management, and achieve consistent, repeatable CI/CD processes with Jenkins.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/certified-jenkins-engineer/module/4d6d1f39-307c-4fdb-8d2b-834c1650e792/lesson/e4400cdc-9c6b-40aa-823c-e99189d4b354" />
</CardGroup>
