Certified Jenkins Engineer

Automation and Security

Demo Jenkins CLI Build a job

In this tutorial, you’ll learn how to download the Jenkins CLI, authenticate, and trigger jobs directly from your terminal. This approach helps automate deployments and integrate Jenkins into your CI/CD workflows.

1. Download the Jenkins CLI JAR

Navigate to Manage Jenkins → Jenkins CLI and copy the link to jenkins-cli.jar:

The image shows the "Manage Jenkins" page of a Jenkins server, displaying options for troubleshooting and tools such as "Manage Old Data," "Jenkins CLI," and "Script Console."

Note

Replace http://139.84.159.194:8080/ with your own Jenkins URL if different.

On your Jenkins controller VM:

# Switch to home directory and download the CLI JAR
cd ~
wget http://139.84.159.194:8080/jnlpJars/jenkins-cli.jar

Verify the download:

ls
# docker-compose.yml  gitea  jenkins-cli.jar

2. Explore Available CLI Commands

List all CLI commands and options:

java -jar jenkins-cli.jar -s http://139.84.159.194:8080/ help

Check your current identity:

java -jar jenkins-cli.jar -s http://139.84.159.194:8080/ who-am-i
Authenticated as: anonymous
Authorities:
  anonymous

Attempting to list jobs without proper credentials results in:

java -jar jenkins-cli.jar -s http://139.84.159.194:8080/ list-jobs
ERROR: anonymous is missing the Overall/Read permission

3. Authenticate with Username and Password

Use -auth USER:SECRET to provide credentials. For example:

java -jar jenkins-cli.jar -s http://139.84.159.194:8080/ \
  -auth admin:password \
  list-jobs
ascii-build-job
ascii-deploy-job
ascii-test-job
Generate ASCII Artwork
hello-world-pipeline
jenkins-hello-world
parameterized-pipeline-job

Confirm your authenticated identity:

java -jar jenkins-cli.jar -s http://139.84.159.194:8080/ \
  -auth admin:password \
  who-am-i
Authenticated as: admin
Authorities:
  authenticated

Warning

Avoid embedding plaintext passwords in scripts. Use Jenkins API tokens or SSH key authentication for better security.

4. Build Command and Flags

The build command accepts several useful flags:

FlagDescription
-fFollow the live build output
-p KEY=VALUEPass one or more build parameters
-sWait until the build completes
-vInclude the full console log in the output
-wWait until the build starts

5. Triggering a Parameterized Job

To trigger parameterized-pipeline-job with a BRANCH_NAME parameter:

java -jar jenkins-cli.jar -s http://139.84.159.194:8080/ \
  -auth admin:password \
  build parameterized-pipeline-job -f -p BRANCH_NAME=test
Started parameterized-pipeline-job #4
Completed parameterized-pipeline-job #4 : SUCCESS

6. View Job Status in Jenkins UI

After running the CLI command, you can monitor the build status and trends in the Jenkins dashboard:

The image shows a Jenkins dashboard displaying the status of a parameterized pipeline job, with a test result trend graph and a detailed pipeline execution timeline.

7. Include Full Console Output

If you need detailed logs in your terminal, add the -v flag:

java -jar jenkins-cli.jar -s http://139.84.159.194:8080/ \
  -auth admin:password \
  build parameterized-pipeline-job -f -v -p BRANCH_NAME=test
[Pipeline] Start of Pipeline
...
[INFO] Total time:  3.452 s
[INFO] Finished at: 2024-08-19T19:42:29Z
...
[Pipeline] End of Pipeline
Finished: SUCCESS
Completed parameterized-pipeline-job #5 : SUCCESS

You can automate these steps in shell scripts or integrate them into your CI/CD pipelines. Beyond building jobs, the Jenkins CLI lets you manage views, plugins, credentials, and more.

Watch Video

Watch video content

Previous
Automating Jenkins using CLI and APIs