Build and test stages
JUnit reports, and why FAILURE and UNSTABLE are different promises.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/buildtest.tar.gz && tar -xzf buildtest.tar.gz, which creates ~/lab/buildtest/. SHA-256: e522fef8f65bd75a7f61268dfb2edaf244ac1974c88a220ea6e150aea8306f2eA team's release pipeline has been green for weeks. Then a customer reports a bug that a test has been catching the whole time. Someone had added || true to the test command months earlier, to get past a flaky test during a deadline. The test kept failing and the pipeline kept going. In this lesson you'll build the honest version and then the dishonest one, and learn to tell them apart from a build's result alone.
Build, test, report
pipeline {agent { label 'linux' }stages {stage('Build') {steps { sh 'python3 -m compileall -q .' }}stage('Test') {steps { sh 'python3 -m pytest -q --junitxml=reports/junit.xml' }post {always { junit 'reports/junit.xml' }}}stage('Package') {steps { echo 'packaging' }}}}
For this Python app, "build" means compileall, which finds syntax errors quickly without running anything. In a compiled language this is your compiler. The stage is first because it's cheap, and a syntax error makes the tests pointless. --junitxml writes results in the JUnit XML format nearly every test tool can produce, and the junit step reads them. It's in post { always } so results are published when tests fail, which is precisely when you need them.
Jenkins now holds results it can query, track across builds and show in the UI. [Checks API] No suitable checks publisher found is only telling you no GitHub or GitLab plugin is reporting status back, and it's harmless here.
Two kinds of red
Push a syntax error first:
The build stage caught it, and both later stages were skipped. No time went to tests that couldn't pass anyway. Now fix that and push a logic bug instead, one that compiles fine:
pytest exited 1, so the sh step failed, the post block still published the report, Package was skipped, and the result is FAILURE. You can also list which test failed from the API, without reading the log. Nothing got packaged from broken code, and that's the behaviour you want from a pipeline.
The || true trap
Here's the shortcut from the opening, in a separate job on the same broken commit:
pipeline {agent { label 'linux' }stages {stage('Test') {steps { sh 'python3 -m pytest -q --junitxml=reports/junit.xml || true' }post {always { junit 'reports/junit.xml' }}}stage('Package') {steps { echo 'packaging' }}}}
The same test failed. But || true made the shell exit 0, so Package ran on code with a known bug. The junit step saw the failure in the XML and set the result to UNSTABLE, yellow rather than red. Here's how the two results compare:
| Result | Set by | Later stages | Means |
|---|---|---|---|
FAILURE | A step failing | Skipped | The pipeline stopped |
UNSTABLE | junit finding failed tests in a report | Run | The pipeline carried on with failing tests |
If a step may fail without stopping the build, say so in the Jenkinsfile instead of hiding it in the shell. catchError catches the failure and lets you choose two results: one for the stage and one for the build. Here a scan fails, its stage is marked failed, and the build stays green and continues:
pipeline {agent { label 'linux' }stages {stage('Scan') {steps {catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {sh 'echo "scan found 2 issues"; exit 1'}}}stage('Package') { steps { echo 'packaging' } }}}
The failing step printed ERROR: script returned exit code 1, yet Package ran and the build finished SUCCESS. The stage itself is recorded as failed, which plugins that draw stages, such as Pipeline Graph View, show in red. This lab installs none, so here only the ERROR line shows it. Unlike || true, the failure stays visible and the intent is written down.
Some teams use UNSTABLE deliberately for a non-critical suite, with catchError(buildResult: 'UNSTABLE') or the unstable step to mark that intent. That can be reasonable for reporting. It's a bad choice ahead of anything that ships, because each later stage runs with failing tests behind it. If you see || true on a test command in a release pipeline, treat it as a bug.
Hands-on
In a fresh lab, seed and run the strict job. Add a test that always fails, and add a Deploy stage guarded by when { expression { currentBuild.currentResult == 'SUCCESS' } } to app/Jenkinsfile.tolerant. Push, run tolerant, and check whether Deploy ran. What does that tell you about guarding stages in a pipeline that has swallowed an exit code? Finish with ./down.sh. A worked version is in solutions/.
What you should see: two tests fail (test_always_fails and test_div, which the fresh lab inherited from the walkthrough's broken commit), || true makes the shell step pass, and junit in the Test stage's own post sets the build to UNSTABLE at that moment. Package still runs, and Deploy is skipped due to the when conditional, because currentBuild.currentResult was already UNSTABLE. The guard worked only because the result was set before Deploy. If the junit step ran in the pipeline-level post instead, the result would still have been SUCCESS when the guard ran. A guard is no substitute for letting the test step fail.
Remember
Let the test command's exit status fail the build, and publish reports from post { always }. A yellow UNSTABLE build still ran Package on the failing commit, so treat || true on a test command as a bug.
junit step sits in the steps block after sh 'pytest ...'. Tests fail. What do you see in the build?junit never ran here, so it cannot affect the result.post { always }, which runs after a failed step.steps. It's just in the wrong place to be useful.UNSTABLE, and its Package stage ran. What most likely happened?|| true, or a deliberate marking, produces exactly this.FAILURE.ABORTED, which is not this result.