pipeline { agent any environment { GRADLE_USER_HOME = '/var/jenkins_home/.gradle' GRADLE_OPTS = '-Dorg.gradle.parallel=true -Dorg.gradle.caching=true' TESTS_STARTED = 'false' VERIFICATION_SUCCEEDED = 'false' } stages { stage('Clean') { steps { // A failed clean must not publish a previous build's reports. withGradle { sh './gradlew clean --no-daemon --console=plain' } } } stage('Tests and coverage') { steps { script { env.TESTS_STARTED = 'true' } withChecks(name: 'Test', includeStage: true) { withGradle { // Continue independent verification while retaining a nonzero exit on failure. sh './gradlew test jacocoTestReport jenkinsPipelineTest --continue --parallel --max-workers=2 --build-cache --no-daemon --console=plain' } } script { env.VERIFICATION_SUCCEEDED = 'true' } } } } post { always { script { if (env.TESTS_STARTED == 'true') { // Attempt each publisher even if another publisher fails. catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { junit testResults: 'build/test-results/test/TEST-*.xml', checksName: 'Test / Tests and coverage', allowEmptyResults: env.VERIFICATION_SUCCEEDED != 'true' } catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { if (fileExists('build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml')) { withChecks(name: 'JaCoCo Coverage', includeStage: false) { recordCoverage(tools: [[parser: 'JACOCO', pattern: 'build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml']], id: 'jacoco', name: 'JaCoCo Coverage', checksName: 'JaCoCo Coverage', enabledForFailure: true, failOnError: true, sourceDirectories: [[path: 'src/main/java']], sourceCodeEncoding: 'UTF-8', sourceCodeRetention: 'EVERY_BUILD', qualityGates: [ [threshold: 50.0, metric: 'CLASS', baseline: 'PROJECT', criticality: 'UNSTABLE'], [threshold: 50.0, metric: 'METHOD', baseline: 'PROJECT', criticality: 'UNSTABLE'], [threshold: 50.0, metric: 'LINE', baseline: 'PROJECT', criticality: 'UNSTABLE'], [threshold: 50.0, metric: 'INSTRUCTION', baseline: 'PROJECT', criticality: 'UNSTABLE'], [threshold: 40.0, metric: 'BRANCH', baseline: 'PROJECT', criticality: 'UNSTABLE']]) } } else if (env.VERIFICATION_SUCCEEDED == 'true') { error 'Successful verification is missing the JaCoCo XML report.' } else { echo 'Verification failed before coverage was available; preserving available test reports.' } } catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { publishHTML(target: [ allowMissing: env.VERIFICATION_SUCCEEDED != 'true', alwaysLinkToLastBuild: false, keepAll: true, reportDir: 'build/reports/jacoco/jacocoTestReport/html', reportFiles: 'index.html', reportName: 'Coverage Report' ]) } catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { archiveArtifacts artifacts: 'build/test-results/test/**, build/reports/tests/test/**, build/reports/jacoco/**, build/jacoco/*.exec', allowEmptyArchive: env.VERIFICATION_SUCCEEDED != 'true', onlyIfSuccessful: false } } else { echo 'Preparation did not complete; no current test run to publish. See the build console for diagnostics.' } } } } }