I'm using Jenkins in a multi-modular Maven project and I have three stages in my pipeline: build, test and deploy. I want to be able to run all unit tests during test
stage and, when there are test failures, deploy
stage should be skipped.
For now, I managed to find a workaround (which works as I want it to), but I had to explicitly approve usage of some methods, which are marked by Jenkins as vurnelabilities.
I am wondering if there is any clearer way to achieve my goals?
import hudson.tasks.test.AbstractTestResultAction
pipeline {
agent {
docker {
image 'maven:3-jdk-8-alpine'
args '--name maven3 -v /root/.m2:/root/.m2'
}
}
stages {
stage('Build') {
steps {
sh 'mvn clean install -DskipTests'
}
}
stage('Test') {
steps {
sh 'mvn test --fail-never'
}
post {
always {
junit '**/target/surefire-reports/*.xml'
}
}
}
stage('Deploy') {
steps {
script {
def AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction == null) {
error("Could not read test results")
}
def numberOfFailedTests = testResultAction.failCount
testResultAction = null
if (numberOfFailedTests == null || numberOfFailedTests != 0) {
error("Did not deploy. Number of failed tests: " + numberOfFailedTests)
}
sh 'mvn deploy -DskipTests'
}
}
}
}
}