Try to get rid of global state issues in Jenkinsfile

This commit is contained in:
Christian Beikov 2022-04-08 10:39:36 +02:00
parent dc42ec38ed
commit a822192002
1 changed files with 77 additions and 70 deletions

147
Jenkinsfile vendored
View File

@ -74,10 +74,10 @@ if (currentBuild.getBuildCauses().toString().contains('BranchIndexingCause')) {
stage('Build') { stage('Build') {
Map<String, Closure> executions = [:] Map<String, Closure> executions = [:]
Map<String, Map<String, String>> state = [:]
environments.each { BuildEnvironment buildEnv -> environments.each { BuildEnvironment buildEnv ->
// Don't build environments for newer JDKs when this is a PR // Don't build environments for newer JDKs when this is a PR
def isNewerJdk = buildEnv.getVersion() != defaultJdk if ( buildEnv.getVersion() != defaultJdk ) {
if ( isNewerJdk ) {
if ( helper.scmSource.pullRequest ) { if ( helper.scmSource.pullRequest ) {
return return
} }
@ -87,11 +87,13 @@ stage('Build') {
// Use withEnv instead of setting env directly, as that is global! // Use withEnv instead of setting env directly, as that is global!
// See https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md // See https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md
withEnv(["JAVA_HOME=${tool buildEnv.buildJdkTool}", "PATH+JAVA=${tool buildEnv.buildJdkTool}/bin", "TEST_JAVA_HOME=${tool buildEnv.testJdkTool}"]) { withEnv(["JAVA_HOME=${tool buildEnv.buildJdkTool}", "PATH+JAVA=${tool buildEnv.buildJdkTool}/bin", "TEST_JAVA_HOME=${tool buildEnv.testJdkTool}"]) {
def additionalOptions = "" if ( buildEnv.getVersion() != defaultJdk ) {
if ( isNewerJdk ) { state[buildEnv.tag]['additionalOptions'] = " -Ptest.jdk.version=${buildEnv.getTestVersion()} -Porg.gradle.java.installations.paths=${JAVA_HOME},${TEST_JAVA_HOME}";
additionalOptions = " -Ptest.jdk.version=${buildEnv.getTestVersion()} -Porg.gradle.java.installations.paths=${JAVA_HOME},${TEST_JAVA_HOME}"
} }
def containerName = null else {
state[buildEnv.tag]['additionalOptions'] = "";
}
state[buildEnv.tag]['containerName'] = null;
stage('Checkout') { stage('Checkout') {
checkout scm checkout scm
} }
@ -103,14 +105,14 @@ stage('Build') {
docker.image('mysql:8.0.21').pull() docker.image('mysql:8.0.21').pull()
} }
sh "./docker_db.sh mysql_8_0" sh "./docker_db.sh mysql_8_0"
containerName = "mysql" state[buildEnv.tag]['containerName'] = "mysql"
break; break;
case "mariadb": case "mariadb":
docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') { docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') {
docker.image('mariadb:10.5.8').pull() docker.image('mariadb:10.5.8').pull()
} }
sh "./docker_db.sh mariadb" sh "./docker_db.sh mariadb"
containerName = "mariadb" state[buildEnv.tag]['containerName'] = "mariadb"
break; break;
case "postgresql_9_5": case "postgresql_9_5":
// use the postgis image to enable the PGSQL GIS (spatial) extension // use the postgis image to enable the PGSQL GIS (spatial) extension
@ -118,7 +120,7 @@ stage('Build') {
docker.image('postgis/postgis:9.5-2.5').pull() docker.image('postgis/postgis:9.5-2.5').pull()
} }
sh "./docker_db.sh postgresql_9_5" sh "./docker_db.sh postgresql_9_5"
containerName = "postgres" state[buildEnv.tag]['containerName'] = "postgres"
break; break;
case "postgresql_13": case "postgresql_13":
// use the postgis image to enable the PGSQL GIS (spatial) extension // use the postgis image to enable the PGSQL GIS (spatial) extension
@ -126,33 +128,33 @@ stage('Build') {
docker.image('postgis/postgis:13-3.1').pull() docker.image('postgis/postgis:13-3.1').pull()
} }
sh "./docker_db.sh postgresql_13" sh "./docker_db.sh postgresql_13"
containerName = "postgres" state[buildEnv.tag]['containerName'] = "postgres"
break; break;
case "oracle": case "oracle":
docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') { docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') {
docker.image('quillbuilduser/oracle-18-xe').pull() docker.image('quillbuilduser/oracle-18-xe').pull()
} }
sh "./docker_db.sh oracle_18" sh "./docker_db.sh oracle_18"
containerName = "oracle" state[buildEnv.tag]['containerName'] = "oracle"
break; break;
case "db2": case "db2":
docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') { docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') {
docker.image('ibmcom/db2:11.5.7.0').pull() docker.image('ibmcom/db2:11.5.7.0').pull()
} }
sh "./docker_db.sh db2" sh "./docker_db.sh db2"
containerName = "db2" state[buildEnv.tag]['containerName'] = "db2"
break; break;
case "mssql": case "mssql":
docker.image('mcr.microsoft.com/mssql/server:2017-CU13').pull() docker.image('mcr.microsoft.com/mssql/server:2017-CU13').pull()
sh "./docker_db.sh mssql" sh "./docker_db.sh mssql"
containerName = "mssql" state[buildEnv.tag]['containerName'] = "mssql"
break; break;
case "sybase": case "sybase":
docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') { docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') {
docker.image('nguoianphu/docker-sybase').pull() docker.image('nguoianphu/docker-sybase').pull()
} }
sh "./docker_db.sh sybase" sh "./docker_db.sh sybase"
containerName = "sybase" state[buildEnv.tag]['containerName'] = "sybase"
break; break;
case "edb": case "edb":
docker.withRegistry('https://containers.enterprisedb.com', 'hibernateci.containers.enterprisedb.com') { docker.withRegistry('https://containers.enterprisedb.com', 'hibernateci.containers.enterprisedb.com') {
@ -162,7 +164,7 @@ stage('Build') {
docker.image('containers.enterprisedb.com/edb/edb-as-lite:v11').pull() docker.image('containers.enterprisedb.com/edb/edb-as-lite:v11').pull()
} }
sh "./docker_db.sh edb" sh "./docker_db.sh edb"
containerName = "edb" state[buildEnv.tag]['containerName'] = "edb"
break; break;
} }
} }
@ -171,88 +173,46 @@ stage('Build') {
case "h2": case "h2":
case "derby": case "derby":
case "hsqldb": case "hsqldb":
runTest("-Pdb=${buildEnv.dbName}${additionalOptions}") runTest("-Pdb=${buildEnv.dbName}${state[buildEnv.tag]['additionalOptions']}")
break; break;
case "mysql8": case "mysql8":
runTest("-Pdb=mysql_ci${additionalOptions}") runTest("-Pdb=mysql_ci${state[buildEnv.tag]['additionalOptions']}")
break; break;
case "tidb": case "tidb":
runTest("-Pdb=tidb -DdbHost=localhost:4000${additionalOptions}", 'TIDB') runTest("-Pdb=tidb -DdbHost=localhost:4000${state[buildEnv.tag]['additionalOptions']}", 'TIDB')
break; break;
case "postgresql_9_5": case "postgresql_9_5":
case "postgresql_13": case "postgresql_13":
runTest("-Pdb=pgsql_ci${additionalOptions}") runTest("-Pdb=pgsql_ci${state[buildEnv.tag]['additionalOptions']}")
break; break;
case "oracle": case "oracle":
runTest("-Pdb=oracle_ci -PexcludeTests=**.LockTest.testQueryTimeout*${additionalOptions}") runTest("-Pdb=oracle_ci -PexcludeTests=**.LockTest.testQueryTimeout*${state[buildEnv.tag]['additionalOptions']}")
break; break;
case "oracle_ee": case "oracle_ee":
runTest("-Pdb=oracle_jenkins${additionalOptions}", 'ORACLE_RDS') runTest("-Pdb=oracle_jenkins${state[buildEnv.tag]['additionalOptions']}", 'ORACLE_RDS')
break; break;
case "hana": case "hana":
runTest("-Pdb=hana_jenkins${additionalOptions}", 'HANA') runTest("-Pdb=hana_jenkins${state[buildEnv.tag]['additionalOptions']}", 'HANA')
break; break;
case "edb": case "edb":
runTest("-Pdb=edb_ci -DdbHost=localhost:5433${additionalOptions}") runTest("-Pdb=edb_ci -DdbHost=localhost:5433${state[buildEnv.tag]['additionalOptions']}")
break; break;
case "s390x": case "s390x":
runTest("-Pdb=h2${additionalOptions}") runTest("-Pdb=h2${state[buildEnv.tag]['additionalOptions']}")
break; break;
default: default:
runTest("-Pdb=${buildEnv.dbName}_ci${additionalOptions}") runTest("-Pdb=${buildEnv.dbName}_ci${state[buildEnv.tag]['additionalOptions']}")
break; break;
} }
} }
} }
finally { finally {
if ( containerName != null ) { if ( state[buildEnv.tag]['containerName'] != null ) {
sh "docker rm -f ${containerName}" sh "docker rm -f ${state[buildEnv.tag]['containerName']}"
} }
// Skip this for PRs // Skip this for PRs
if ( !env.CHANGE_ID && buildEnv.notificationRecipients != null ) { if ( !env.CHANGE_ID && buildEnv.notificationRecipients != null ) {
boolean success = currentBuild.result == 'SUCCESS' handleNotifications(currentBuild, buildEnv)
String previousResult = currentBuild.previousBuild == null ? null : currentBuild.previousBuild.result == 'SUCCESS'
// Ignore success after success
if ( !( success && previousResult == 'SUCCESS' ) ) {
def subject
def body
if ( success ) {
if ( previousResult != 'SUCCESS' && previousResult != null ) {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Fixed"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Fixed:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
else {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Success"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Success:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
}
else if ( currentBuild.result == 'FAILURE' ) {
if ( previousResult != null && previousResult == "FAILURE" ) {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Still failing"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Still failing:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
else {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Failure"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Failure:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
}
else {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - ${currentBuild.result}"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - ${currentBuild.result}:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
emailext(
subject: subject,
body: body,
to: buildEnv.notificationRecipients
)
}
} }
} }
} }
@ -348,3 +308,50 @@ void runTest(String goal, String lockableResource = null, boolean clean = true)
junit '**/target/test-results/test/*.xml' junit '**/target/test-results/test/*.xml'
} }
} }
void handleNotifications(currentBuild, buildEnv) {
boolean success = currentBuild.result == 'SUCCESS'
String previousResult = currentBuild.previousBuild == null ? null : currentBuild.previousBuild.result == 'SUCCESS'
// Ignore success after success
if ( !( success && previousResult == 'SUCCESS' ) ) {
def subject
def body
if ( success ) {
if ( previousResult != 'SUCCESS' && previousResult != null ) {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Fixed"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Fixed:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
else {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Success"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Success:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
}
else if ( currentBuild.result == 'FAILURE' ) {
if ( previousResult != null && previousResult == "FAILURE" ) {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Still failing"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Still failing:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
else {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Failure"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - Failure:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
}
else {
subject = "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - ${currentBuild.result}"
body = """<p>${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - ${currentBuild.result}:</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a> to view the results.</p>"""
}
emailext(
subject: subject,
body: body,
to: buildEnv.notificationRecipients
)
}
}