0
0
Jenkinsdevops~5 mins

Build history and logs in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run a Jenkins job, it keeps a record of each build and its logs. This helps you see what happened during each run and find problems if the build fails.
When you want to check if a recent build succeeded or failed.
When you need to find errors or warnings from a build to fix issues.
When you want to compare the results of different builds over time.
When you want to audit who triggered a build and when.
When you want to download or share build logs with your team.
Commands
This command lists the build history for the Jenkins job named 'my-job'. It shows build numbers and their status.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 list-builds my-job
Expected OutputExpected
Build #5 SUCCESS Build #4 FAILURE Build #3 SUCCESS Build #2 SUCCESS Build #1 FAILURE
-s - Specifies the Jenkins server URL
This command shows the console log output of build number 5 for the job 'my-job'. It helps you see what happened during that build.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 console my-job 5
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building project [Pipeline] sh + mvn clean install [INFO] BUILD SUCCESS [Pipeline] End of Pipeline
-s - Specifies the Jenkins server URL
This command triggers a new build of the job 'my-job' and waits for it to finish. Useful to start a build from the command line.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job -w
Expected OutputExpected
Started build #6 Finished build #6 SUCCESS
-w - Waits until the build completes before returning
This command downloads the job configuration XML file for 'my-job'. It can be used to backup or inspect job settings.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 get-job my-job > my-job-config.xml
Expected OutputExpected
No output (command runs silently)
-s - Specifies the Jenkins server URL
Key Concept

If you remember nothing else from this pattern, remember: Jenkins keeps every build's details and logs so you can track and troubleshoot your jobs easily.

Common Mistakes
Trying to view build logs without specifying the correct build number.
Jenkins needs the exact build number to show logs; otherwise, it cannot find which build's logs to display.
Always specify the build number when requesting logs, for example: 'console my-job 5'.
Not using the Jenkins CLI with the correct server URL.
The CLI commands fail if they don't know which Jenkins server to connect to.
Always include the '-s http://localhost:8080' flag or your Jenkins server URL.
Assuming the build command triggers a build instantly without waiting.
Without the '-w' flag, the build command triggers the build but returns immediately, so you don't know when it finishes.
Use the '-w' flag to wait for the build to complete and get the result.
Summary
Use Jenkins CLI commands to list builds and see their status.
View detailed logs of any build by specifying the job name and build number.
Trigger new builds from the command line and wait for their completion.
Download job configuration files for backup or inspection.