Jenkins CLI via SSH
The Jenkins CLI allows both users and administrators to interact directly with Jenkins from a shell or script, streamlining daily tasks and integrations. You can access the CLI over SSH or via a downloadable Java-based client. When using SSH, note that the SSH service is disabled by default. Jenkins will select a random port for SSH connections, so you need to determine the port number first.Ensure that the SSH service is enabled and your public key is added to the Jenkins user configuration before attempting SSH-based sessions.
Retrieve the SSH Port
Run the following command to obtain the SSH port used by Jenkins:Authenticating via SSH
Jenkins uses SSH-based public-private key authentication. After retrieving the SSH port, add your SSH public key to the Jenkins configuration page for your account. Once configured, you can run several built-in CLI commands.Listing Available CLI Commands
To display all available commands, use the following command:Triggering a Job Build
One common use case is triggering a job build. For example, to start a job named “hello-kodekloud”, execute:Jenkins CLI Client (HTTP Transport)
For certain scenarios, the Jenkins CLI client provided as a JAR file can be more convenient than SSH. This client uses HTTP or WebSocket transport, which means you do not need to open additional ports on your firewall.Getting Started with the CLI Client
- Download the
jenkins-cli.jarfrom your Jenkins instance. - Run the client with the following command:
Secure Authentication with the CLI Client
For secure usage, the CLI client supports basic authentication as well as API tokens. Although basic authentication using--auth username:password is available, it exposes credentials in plain text. It is recommended to use API tokens instead. Below are some examples:
Always use API tokens instead of plain text passwords to enhance security when automating Jenkins tasks.
Jenkins REST API
The Jenkins REST API provides a comprehensive set of HTTP endpoints that allow you to control Jenkins programmatically. This API can be used for various tasks including installing plugins, managing jobs, triggering builds, and retrieving logs.Key Use Cases for the REST API
- Installing or managing plugins
- Creating, updating, or deleting jobs
- Triggering builds and retrieving job details
- Accessing console logs and artifacts
- Managing nodes and agents
Installing a Plugin via the REST API
For example, you can install a plugin by sending a POST request with XML data using cURL:Avoid hardcoding credentials in your scripts. Use API tokens and secure HTTP header configurations to reduce the risk of exposing sensitive information.