How to Use Jenkins CLI: Commands and Examples
Use the
jenkins-cli.jar to run Jenkins commands from your terminal by connecting to your Jenkins server URL. Run commands like java -jar jenkins-cli.jar -s http://your-jenkins-url command to interact with Jenkins remotely.Syntax
The basic syntax to use Jenkins CLI is:
java -jar jenkins-cli.jar: Runs the Jenkins CLI tool.-s http://your-jenkins-url: Specifies the Jenkins server URL.command: The Jenkins command you want to execute (e.g.,build,list-jobs).options: Additional options or arguments for the command.
You can also authenticate using --username and --password or an API token.
bash
java -jar jenkins-cli.jar -s http://your-jenkins-url command [options]Example
This example shows how to list all jobs on a Jenkins server using the CLI.
bash
java -jar jenkins-cli.jar -s http://localhost:8080 list-jobsOutput
ExampleJob1
ExampleJob2
ExampleJob3
Common Pitfalls
Common mistakes when using Jenkins CLI include:
- Not specifying the correct Jenkins URL with
-s. - Failing to authenticate properly, causing permission errors.
- Using an outdated
jenkins-cli.jarthat does not match the Jenkins server version. - Not having Java installed or using an incompatible Java version.
Always download the jenkins-cli.jar from your Jenkins server at http://your-jenkins-url/jnlpJars/jenkins-cli.jar to ensure compatibility.
Quick Reference
| Command | Description |
|---|---|
| list-jobs | Lists all jobs on the Jenkins server |
| build | Triggers a build for the specified job |
| get-job | Retrieves the job configuration XML |
| create-job | Creates a new job from XML input |
| delete-job | Deletes the specified job |
| help | Shows help for Jenkins CLI commands |
Key Takeaways
Always download the jenkins-cli.jar from your Jenkins server to ensure version compatibility.
Use the syntax: java -jar jenkins-cli.jar -s http://your-jenkins-url command [options].
Authenticate properly with username and API token to avoid permission errors.
Common commands include list-jobs, build, get-job, create-job, and delete-job.
Ensure Java is installed and compatible to run the Jenkins CLI tool.