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.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.
Table of Contents
- Jenkins CLI over SSH
1.1 Enable SSH Endpoint
1.2 List Available Commands
1.3 Trigger a Job via SSH - Jenkins CLI Client (jenkins-cli.jar)
- Jenkins REST API
3.1 Install a Plugin
3.2 Authentication Methods - 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:Make sure your SSH key is correctly formatted (RFC4716 or OpenSSH) before pasting into Jenkins.
List Available Commands
Once SSH is configured, list all CLI commands:Trigger a Job via SSH
Build a job namedhello-kodekloud and stream console output:
Jenkins CLI Client (jenkins-cli.jar)
If SSH isn’t an option, download thejenkins-cli.jar from your Jenkins master:
Jenkins REST API
The Jenkins REST API provides HTTP endpoints for jobs, nodes, plugins, and more.Install a Plugin
Usecurl to install or update plugins:
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 |
Never commit credentials or API tokens into version control. Use environment variables or secret managers.