How to Manage Jenkins from Command Line Easily
You can manage Jenkins from the command line using the
jenkins-cli.jar tool, which lets you run commands like creating jobs, building projects, and managing plugins. Download the CLI jar from your Jenkins server and run commands with java -jar jenkins-cli.jar followed by the desired action.Syntax
The basic syntax to use Jenkins CLI is:
java -jar jenkins-cli.jar -s JENKINS_URL COMMAND [OPTIONS]-s JENKINS_URL: URL of your Jenkins server (e.g., http://localhost:8080)COMMAND: The Jenkins command you want to run (likebuild,create-job,list-jobs)[OPTIONS]: Additional parameters or flags for the command
bash
java -jar jenkins-cli.jar -s http://localhost:8080 COMMAND [OPTIONS]Example
This example shows how to list all jobs on a Jenkins server using the CLI tool.
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 downloading the
jenkins-cli.jarfrom the correct Jenkins server URL. - Forgetting to authenticate if your Jenkins requires login, which can cause commands to fail.
- Using incorrect command names or missing required options.
- Running commands without proper permissions on Jenkins.
bash
Wrong (missing authentication): java -jar jenkins-cli.jar -s http://localhost:8080 build ExampleJob Right (with authentication): java -jar jenkins-cli.jar -s http://localhost:8080 -auth user:APITOKEN build ExampleJob
Quick Reference
Here are some useful Jenkins CLI commands:
| Command | Description |
|---|---|
| list-jobs | Lists all jobs on the Jenkins server |
| build JOB_NAME | Triggers a build for the specified job |
| create-job JOB_NAME | Creates a new job from XML configuration |
| delete-job JOB_NAME | Deletes the specified job |
| install-plugin PLUGIN_NAME | Installs a plugin by name |
| safe-restart | Restarts Jenkins safely after current builds finish |
| who-am-i | Shows the current user identity |
Key Takeaways
Use the jenkins-cli.jar tool with java -jar to run Jenkins commands from the terminal.
Always specify your Jenkins server URL with the -s option.
Authenticate with -auth user:APITOKEN if your Jenkins requires login.
Common commands include list-jobs, build, create-job, and install-plugin.
Download the CLI jar directly from your Jenkins server at /jnlpJars/jenkins-cli.jar.