Jenkins: Trigger deferred failure when any build of any stage fails

This commit is contained in:
RichardG867 2021-11-16 00:22:46 -03:00
parent eb7a7fed18
commit 015c6b1151
2 changed files with 37 additions and 20 deletions

31
.ci/Jenkinsfile vendored
View file

@ -48,7 +48,7 @@ def saveArtifacts() {
archiveArtifacts artifacts: "${env.JOB_BASE_NAME}-*" archiveArtifacts artifacts: "${env.JOB_BASE_NAME}-*"
} }
def anySuccess = false def successCount = 0
def buildChain = [ def buildChain = [
'86Box': '86Box-Dev', '86Box': '86Box-Dev',
@ -75,32 +75,40 @@ pipeline {
stages { stages {
stage('Build Windows') { stage('Build Windows') {
agent {
node {
label 'windows'
}
}
steps { steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
node('windows') {
gitClone() gitClone()
windowsBuild() windowsBuild()
saveArtifacts() saveArtifacts()
}
script { script {
anySuccess = true successCount += 1
} }
} }
} }
} }
stage('Build Linux') { stage('Build Linux') {
agent {
node {
label 'debian'
}
}
steps { steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
node('debian') {
gitClone() gitClone()
unixBuild() unixBuild()
saveArtifacts() saveArtifacts()
}
script { script {
anySuccess = true successCount += 1
} }
} }
} }
@ -110,11 +118,12 @@ pipeline {
post { post {
always { always {
script { script {
/* Trigger next job is applicable. */
if (buildChain[env.JOB_BASE_NAME]) { if (buildChain[env.JOB_BASE_NAME]) {
def nextJob = buildChain[env.JOB_BASE_NAME] def nextJob = buildChain[env.JOB_BASE_NAME]
/* Set next build number for the next job. */
try { try {
/* Set next build number for this job. */
def job = Jenkins.instance.getItem(nextJob) def job = Jenkins.instance.getItem(nextJob)
job.nextBuildNumber = env.BUILD_NUMBER as Integer job.nextBuildNumber = env.BUILD_NUMBER as Integer
job.saveNextBuildNumber() job.saveNextBuildNumber()
@ -122,7 +131,7 @@ pipeline {
println "[!] Could not set next build number for [$nextJob], make sure all required script approvals are in place" println "[!] Could not set next build number for [$nextJob], make sure all required script approvals are in place"
} }
/* Trigger this job. */ /* Trigger next job. */
build propagate: false, build propagate: false,
wait: false, wait: false,
job: nextJob, job: nextJob,
@ -132,8 +141,8 @@ pipeline {
] ]
} }
if (!anySuccess) { if (successCount < 2) {
println "[!] Failing build because all build stages failed" println "[!] Failing build because a build stage failed"
currentBuild.result = 'FAILURE' currentBuild.result = 'FAILURE'
} }

View file

@ -122,6 +122,7 @@ build() {
if [ -z "$job_name" ] if [ -z "$job_name" ]
then then
echo [!] Missing environment variables: received JOB_BASE_NAME=[$JOB_BASE_NAME] BUILD_TYPE=[$BUILD_TYPE] BUILD_NUMBER=[$BUILD_NUMBER] GIT_COMMIT=[$GIT_COMMIT] echo [!] Missing environment variables: received JOB_BASE_NAME=[$JOB_BASE_NAME] BUILD_TYPE=[$BUILD_TYPE] BUILD_NUMBER=[$BUILD_NUMBER] GIT_COMMIT=[$GIT_COMMIT]
job_status=1
return 1 return 1
fi fi
@ -145,11 +146,12 @@ build() {
echo [-] Switching to MSYSTEM [$msys] echo [-] Switching to MSYSTEM [$msys]
cd "$cwd" cd "$cwd"
CHERE_INVOKING=yes MSYSTEM="$msys" JOB_BASE_NAME="$JOB_BASE_NAME" BUILD_TYPE="$BUILD_TYPE" BUILD_NUMBER="$BUILD_NUMBER" GIT_COMMIT="$GIT_COMMIT" \ CHERE_INVOKING=yes MSYSTEM="$msys" JOB_BASE_NAME="$JOB_BASE_NAME" BUILD_TYPE="$BUILD_TYPE" BUILD_NUMBER="$BUILD_NUMBER" GIT_COMMIT="$GIT_COMMIT" \
bash -lc 'exec "'$0'" -b "'$arch'" '"$cmake_flags" && job_status=0 # make sure the main script exits cleanly on any success bash -lc 'exec "'$0'" -b "'$arch'" '"$cmake_flags"
return $? return $?
fi fi
else else
echo [!] No MSYSTEM for architecture [$arch] echo [!] No MSYSTEM for architecture [$arch]
job_status=1
return 2 return 2
fi fi
echo [-] Using MSYSTEM [$MSYSTEM] echo [-] Using MSYSTEM [$MSYSTEM]
@ -253,6 +255,7 @@ EOF
if [ $? -gt 0 ] if [ $? -gt 0 ]
then then
echo [!] CMake failed with status [$status] echo [!] CMake failed with status [$status]
job_status=1
return 3 return 3
fi fi
@ -263,6 +266,7 @@ EOF
if [ $status -gt 0 ] if [ $status -gt 0 ]
then then
echo [!] Make failed with status [$status] echo [!] Make failed with status [$status]
job_status=1
return 4 return 4
fi fi
@ -273,6 +277,7 @@ EOF
if [ ! -d "archive_tmp" ] if [ ! -d "archive_tmp" ]
then then
echo [!] Archive directory creation failed echo [!] Archive directory creation failed
job_status=1
return 5 return 5
fi fi
@ -337,6 +342,7 @@ EOF
if [ $status -gt 0 ] if [ $status -gt 0 ]
then then
echo [!] Executable move failed with status [$status] echo [!] Executable move failed with status [$status]
job_status=1
return 6 return 6
fi fi
@ -363,12 +369,12 @@ EOF
if [ $status -gt 0 ] if [ $status -gt 0 ]
then then
echo [!] Artifact archive creation failed with status [$status] echo [!] Artifact archive creation failed with status [$status]
job_status=1
return 7 return 7
fi fi
# All good. # All good.
echo [-] Build of [$job_name] [$build_type] [$build_qualifier] [$git_hash] for [$arch] with flags [$cmake_flags] successful echo [-] Build of [$job_name] [$build_type] [$build_qualifier] [$git_hash] for [$arch] with flags [$cmake_flags] successful
job_status=0
} }
tarball() { tarball() {
@ -383,6 +389,7 @@ tarball() {
if [ -z "$job_name" ] if [ -z "$job_name" ]
then then
echo [!] Missing environment variable: received JOB_BASE_NAME=[$JOB_BASE_NAME] echo [!] Missing environment variable: received JOB_BASE_NAME=[$JOB_BASE_NAME]
job_status=1
return 1 return 1
fi fi
@ -405,6 +412,7 @@ tarball() {
if [ $? -gt 0 ] if [ $? -gt 0 ]
then then
echo [!] Tarball creation failed with status [$status] echo [!] Tarball creation failed with status [$status]
job_status=1
return 2 return 2
fi fi
@ -415,7 +423,7 @@ tarball() {
project=86Box project=86Box
cwd=$(pwd) cwd=$(pwd)
first_build=1 first_build=1
job_status=1 job_status=0
# Parse arguments. # Parse arguments.
single_build=0 single_build=0