Certified Jenkins Engineer

Automation and Security

Automating Jenkins using CLI and APIs

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
    1.1 Enable SSH Endpoint
    1.2 List Available Commands
    1.3 Trigger a Job via SSH
  2. Jenkins CLI Client (jenkins-cli.jar)
  3. Jenkins REST API
    3.1 Install a Plugin
    3.2 Authentication Methods
  4. 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:

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.

Note

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:

ssh -l username -p 53801 localhost help

Trigger a Job via SSH

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

ssh -l username -p 53801 localhost build hello-kodekloud -f -v

Sample output:

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:

wget http://JENKINS_URL/jnlpJars/jenkins-cli.jar

Use HTTP or WebSocket transport:

java -jar jenkins-cli.jar -s http://JENKINS_URL/ -webSocket help

Common commands:

# 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 provides HTTP endpoints for jobs, nodes, plugins, and more.

Install a Plugin

Use curl to install or update plugins:

curl -s -X POST \
  --data '<jenkins><install plugin="[email protected]" /></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.

MethodUsageProsCons
Basic Auth--user user:passwordSimple setupExposes credentials in scripts
API Token--user user:APITOKENSecure, revocableRequires token generation per user
SSH KeySSH transport for CLI (ssh -l …)Key-based securityNeeds SSH endpoint enabled in UI

Warning

Never commit credentials or API tokens into version control. Use environment variables or secret managers.


References

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

Watch Video

Watch video content

Previous
Demo Create Pipeline with Parameters