Essential Jenkins CLI Commands for Job Management and Control
Jenkins, as the leading open-source automation server, provides powerful mechanisms for managing CI/CD pipelines. While the web UI is excellent for visual oversight, administrative efficiency often demands automation, especially when dealing with hundreds of jobs or repetitive setup tasks. This is where the Jenkins Command Line Interface (CLI) becomes indispensable.
The Jenkins CLI allows administrators and developers to interact with a Jenkins instance remotely via a terminal, enabling powerful scripting capabilities. This article serves as a practical guide to the core CLI commands necessary for managing the entire lifecycle of Jenkins jobs, from creation and configuration to enabling, disabling, and deletion. Mastering these commands is crucial for effective Jenkins administration and integration into DevOps toolchains.
Setting Up the Jenkins CLI
Before executing any commands, you must establish a secure connection to your Jenkins master. This involves downloading the CLI client and ensuring proper authentication.
1. Downloading the jenkins-cli.jar
The CLI client is typically found directly on your Jenkins server at a specific endpoint. You can download it using wget or curl:
wget http://your-jenkins-url:8080/jnlpJars/jenkins-cli.jar
# OR using curl
curl -O http://your-jenkins-url:8080/jnlpJars/jenkins-cli.jar
2. Authentication Methods
Jenkins CLI connections require authentication. The most secure and recommended method is using an API Token instead of a plain user password.
To generate an API Token, navigate to User Settings > Configure > API Token in the Jenkins web UI.
All subsequent commands will require authentication flags, typically using the -s (server URL) and -auth (authentication credentials) flags:
java -jar jenkins-cli.jar -s http://your-jenkins-url:8080 -auth USERNAME:API_TOKEN <command>
Tip: For security, store your API token in a secure environment variable or use the SSH key authentication method if configured, rather than typing it directly into scripts.
Core Commands for Job Discovery
Before modifying jobs, it's often necessary to list existing jobs and retrieve their configurations.
Listing All Jobs: list-jobs
This command displays the names of all jobs configured on the Jenkins master.
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH list-jobs
Retrieving Job Configuration: get-job
Jenkins jobs are defined by XML configuration files. The get-job command allows you to retrieve this XML definition, which is essential for creating new jobs based on templates.
# Retrieve the config for 'my-template-job' and save it to a file
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH get-job my-template-job > template-config.xml
Essential Job Management Commands
These commands handle the fundamental lifecycle operations for Jenkins jobs.
1. Creating a New Job: create-job
The create-job command requires the name of the new job and the XML configuration file defining its parameters, build steps, and triggers.
Prerequisites: You must first have a valid XML configuration file (e.g., new-config.xml), usually derived by modifying a template retrieved using get-job.
# Example: Create a new job named 'project-feature-branch' using the local XML file
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH create-job project-feature-branch < new-config.xml
2. Copying an Existing Job: copy-job
This is often the fastest way to create a new job, taking an existing job as a template and applying a new name. This command effectively duplicates the source job's configuration.
# Syntax: copy-job SOURCE_JOB_NAME DESTINATION_JOB_NAME
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH copy-job original-pipeline new-dev-pipeline
3. Enabling and Disabling Jobs: enable-job and disable-job
These commands are crucial for maintenance, temporary halts, or administrative control, preventing or allowing builds to be triggered.
# Command to halt further scheduled or triggered builds for maintenance
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH disable-job legacy-project-build
# Command to reactivate the job
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH enable-job legacy-project-build
4. Deleting a Job: delete-job
This command permanently removes a job from the Jenkins instance, including its build history and configuration. Use this command with caution.
# Permanent removal of the job
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH delete-job obsolete-test-job
Warning: The
delete-jobcommand executes immediately without further confirmation in the CLI environment. Ensure your scripts include proper validation steps before executing this command.
Triggering and Monitoring Builds
Beyond configuration management, the CLI is frequently used to trigger builds, especially those requiring specific parameters.
Triggering a Build: build
The build command starts a new build for the specified job. If the job is parameterized, you can pass arguments directly using the -p flag.
# Trigger a simple, non-parameterized build
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH build nightly-deploy
# Trigger a parameterized job, passing parameters
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH build integration-test -p TARGET_ENV=staging -p BRANCH_NAME=hotfix-123
Note: If you use the
-sflag withbuild, the CLI will wait for the build to complete and report the exit status. Otherwise, the command returns immediately after queuing the build.
Viewing Build Status: get-job and console
While the CLI doesn't offer real-time streaming comparable to the web UI, you can check job status and retrieve console output.
To view the console output of a specific build number:
# Retrieve the console output for build #55 of 'my-job'
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH console my-job 55
Best Practices for CLI Scripting
Leveraging the Jenkins CLI efficiently requires adopting specific best practices for stability and security:
1. Scripting and Automation
The true power of the CLI lies in shell scripting. Use commands like get-job, modify the resulting XML using tools like sed or awk, and then feed the modified configuration back using create-job or reload-job.
**Example Workflow (Copy, Modify, and Create):
**
1. Get template: get-job template-job > tmp.xml
2. Use sed to replace placeholders (e.g., project name, repository URL) in tmp.xml.
3. Create new job: create-job new-job < tmp.xml
2. Handling Configuration Updates
To update an existing job's configuration using the XML file, use the reload-job command. This is safer than deleting and recreating the job, as it preserves build history.
# Update 'project-a' using a locally modified configuration file
java -jar jenkins-cli.jar -s $JENKINS_URL -auth $AUTH reload-job project-a < updated-config.xml
3. Error Handling
Always check the exit code ($?) after executing a CLI command in your scripts. A return code of 0 indicates success; any non-zero code signals an error. This is vital for robust automation and debugging.
Summary
The Jenkins CLI provides a robust, scriptable interface for managing jobs, enabling administrators to automate repetitive tasks, manage configuration migrations, and integrate Jenkins administration into larger CI/CD deployment scripts. By utilizing commands like copy-job, create-job, delete-job, and build, you can achieve significant efficiencies and maintain consistency across your Jenkins environments.