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
:
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:
Flag | Description |
---|---|
-f | Follow the live build output |
-p KEY=VALUE | Pass one or more build parameters |
-s | Wait until the build completes |
-v | Include the full console log in the output |
-w | Wait 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:
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.
Links and References
Watch Video
Watch video content