Build is not getting fail when the jacoco coverage is met threshold

Hi, I am using jacoco plugin to make sure if the coverage is less than 70 % then build should failed. for that i am using below script, but in logs of jacoco step getting failure but pipeline shows Green color, and it continues.

stage('Jacoco') {
                 steps {
                    script {
                        jacoco classPattern: '**/build/classes/java', changeBuildStatus: true, minimumInstructionCoverage: '70', maximumInstructionCoverage: '80'
                       if(env.BRANCH_NAME.equals('develop')) {
                          def publishResult = publishCoverage failUnhealthy: true,
						  globalThresholds: [[thresholdTarget: 'Instruction', unhealthyThreshold: 80.0]],
						  adapters: [jacocoAdapter(path: 'build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml')],
						  sourceFileResolver: sourceFiles('STORE_ALL_BUILD')
						  script {
							if (currentBuild.result == 'FAILURE') {
							error("Test coverage is too low.")
							}
							}
                       }
                    }
                 }
        }

In the log of jacoco step getting below message at the end :

[JaCoCo plugin] Apply Min/Max thresholds result: FAILURE

Please suggest the solution

anybody comment on this query

Hello @Laddu and welcome to this community :wave:

The issue might be related to the placement of the error message inside the script block. The script block within the jacoco step is executed independently of the overall pipeline, which is maybe why the pipeline continues even though the JaCoCo step fails.

Typically pipelines do not fail, plugins simply set the build status to unstable.

It is also unclear to me, why you are mixing two different coverage plugins? I would recommend to not use the deprecated step publishCoverage from the coverage plugin anymore, see GitHub - jenkinsci/code-coverage-api-plugin: Jenkins Code Coverage Plugin for details. Additionally, you are also using the JaCoCo plugin to record the coverage. I think this one is not needed anymore, as the functionality has been integrated into the code coverage plugin.