Skip to content

Commit 13b9b3d

Browse files
authored
Merge pull request eugenp#3302 from dassiorleando/master
BAEL-720: better handle failed test in Jenkins pipeline
2 parents e9c7fef + 3219228 commit 13b9b3d

2 files changed

Lines changed: 33 additions & 23 deletions

File tree

‎spring-jenkins-pipeline/scripted-pipeline-unix-nonunix‎

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,48 @@ node {
3131

3232
stage("Tests and Deployment") {
3333
parallel 'Unit tests': {
34-
stage("Runing unit tests") {
35-
if (isUnix()) {
36-
sh "./mvnw test -Punit"
37-
} else {
38-
bat "./mvnw.cmd test -Punit"
34+
stage("Running unit tests") {
35+
try {
36+
if (isUnix()) {
37+
sh "./mvnw test -Punit"
38+
} else {
39+
bat "./mvnw.cmd test -Punit"
40+
}
41+
} catch(err) {
42+
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*UnitTest.xml'])
43+
throw err
3944
}
4045
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*UnitTest.xml'])
4146

4247
}
4348
}, 'Integration tests': {
44-
stage("Runing integration tests") {
45-
if (isUnix()) {
46-
sh "./mvnw test -Pintegration"
47-
} else {
48-
bat "./mvnw.cmd test -Pintegration"
49+
stage("Running integration tests") {
50+
try {
51+
if (isUnix()) {
52+
sh "./mvnw test -Pintegration"
53+
} else {
54+
bat "./mvnw.cmd test -Pintegration"
55+
}
56+
} catch(err) {
57+
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*IntegrationTest.xml'])
58+
throw err
4959
}
5060
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*IntegrationTest.xml'])
5161
}
52-
}, 'Deployment': {
53-
stage("Staging") {
62+
}
63+
64+
stage("Staging") {
65+
if (isUnix()) {
66+
sh "pid=\$(lsof -i:8989 -t); kill -TERM \$pid || kill -KILL \$pid"
67+
} else {
68+
bat "FOR /F \"tokens=5 delims= \" %%G IN (\"netstat -a | findstr :8989\") DO TaskKill.exe /PID %%G /fi \"memusage gt 0\""
69+
}
70+
71+
withEnv(['JENKINS_NODE_COOKIE=dontkill']) {
5472
if (isUnix()) {
55-
sh "pid=\$(lsof -i:8989 -t); kill -TERM \$pid || kill -KILL \$pid"
73+
sh 'nohup ./mvnw spring-boot:run -Dserver.port=8989 &'
5674
} else {
57-
bat "FOR /F \"tokens=5 delims= \" %%G IN (\"netstat -a | findstr :8989\") DO TaskKill.exe /PID %%G /fi \"memusage gt 0\""
58-
}
59-
60-
withEnv(['JENKINS_NODE_COOKIE=dontkill']) {
61-
if (isUnix()) {
62-
sh 'nohup ./mvnw spring-boot:run -Dserver.port=8989 &'
63-
} else {
64-
bat 'start ./mvnw.cmd spring-boot:run -Dserver.port=8989'
65-
}
75+
bat 'start ./mvnw.cmd spring-boot:run -Dserver.port=8989'
6676
}
6777
}
6878
}

‎spring-jenkins-pipeline/src/test/java/com/baeldung/SomeUnitTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
public class SomeUnitTest {
77
@Test
88
public void init() {
9-
assertEquals(1, 0);
9+
assertEquals(1, 1);
1010
}
1111
}

0 commit comments

Comments
 (0)