From 015c6b115186c6d1555b4636c44e3be74da1a83b Mon Sep 17 00:00:00 2001 From: RichardG867 Date: Tue, 16 Nov 2021 00:22:46 -0300 Subject: [PATCH] Jenkins: Trigger deferred failure when any build of any stage fails --- .ci/Jenkinsfile | 43 ++++++++++++++++++++++++++----------------- .ci/build.sh | 14 +++++++++++--- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/.ci/Jenkinsfile b/.ci/Jenkinsfile index f60bf06d8..360b5e1b7 100644 --- a/.ci/Jenkinsfile +++ b/.ci/Jenkinsfile @@ -48,7 +48,7 @@ def saveArtifacts() { archiveArtifacts artifacts: "${env.JOB_BASE_NAME}-*" } -def anySuccess = false +def successCount = 0 def buildChain = [ '86Box': '86Box-Dev', @@ -75,32 +75,40 @@ pipeline { stages { stage('Build Windows') { + agent { + node { + label 'windows' + } + } + steps { catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { - node('windows') { - gitClone() - windowsBuild() - saveArtifacts() - } + gitClone() + windowsBuild() + saveArtifacts() script { - anySuccess = true + successCount += 1 } } } } stage('Build Linux') { + agent { + node { + label 'debian' + } + } + steps { catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { - node('debian') { - gitClone() - unixBuild() - saveArtifacts() - } + gitClone() + unixBuild() + saveArtifacts() script { - anySuccess = true + successCount += 1 } } } @@ -110,11 +118,12 @@ pipeline { post { always { script { + /* Trigger next job is applicable. */ if (buildChain[env.JOB_BASE_NAME]) { def nextJob = buildChain[env.JOB_BASE_NAME] + /* Set next build number for the next job. */ try { - /* Set next build number for this job. */ def job = Jenkins.instance.getItem(nextJob) job.nextBuildNumber = env.BUILD_NUMBER as Integer 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" } - /* Trigger this job. */ + /* Trigger next job. */ build propagate: false, wait: false, job: nextJob, @@ -132,8 +141,8 @@ pipeline { ] } - if (!anySuccess) { - println "[!] Failing build because all build stages failed" + if (successCount < 2) { + println "[!] Failing build because a build stage failed" currentBuild.result = 'FAILURE' } diff --git a/.ci/build.sh b/.ci/build.sh index 63a758542..1fa2d29b6 100644 --- a/.ci/build.sh +++ b/.ci/build.sh @@ -122,6 +122,7 @@ build() { if [ -z "$job_name" ] then 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 fi @@ -145,11 +146,12 @@ build() { echo [-] Switching to MSYSTEM [$msys] 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" \ - 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 $? fi else echo [!] No MSYSTEM for architecture [$arch] + job_status=1 return 2 fi echo [-] Using MSYSTEM [$MSYSTEM] @@ -253,6 +255,7 @@ EOF if [ $? -gt 0 ] then echo [!] CMake failed with status [$status] + job_status=1 return 3 fi @@ -263,6 +266,7 @@ EOF if [ $status -gt 0 ] then echo [!] Make failed with status [$status] + job_status=1 return 4 fi @@ -273,6 +277,7 @@ EOF if [ ! -d "archive_tmp" ] then echo [!] Archive directory creation failed + job_status=1 return 5 fi @@ -337,6 +342,7 @@ EOF if [ $status -gt 0 ] then echo [!] Executable move failed with status [$status] + job_status=1 return 6 fi @@ -363,12 +369,12 @@ EOF if [ $status -gt 0 ] then echo [!] Artifact archive creation failed with status [$status] + job_status=1 return 7 fi # All good. echo [-] Build of [$job_name] [$build_type] [$build_qualifier] [$git_hash] for [$arch] with flags [$cmake_flags] successful - job_status=0 } tarball() { @@ -383,6 +389,7 @@ tarball() { if [ -z "$job_name" ] then echo [!] Missing environment variable: received JOB_BASE_NAME=[$JOB_BASE_NAME] + job_status=1 return 1 fi @@ -405,6 +412,7 @@ tarball() { if [ $? -gt 0 ] then echo [!] Tarball creation failed with status [$status] + job_status=1 return 2 fi @@ -415,7 +423,7 @@ tarball() { project=86Box cwd=$(pwd) first_build=1 -job_status=1 +job_status=0 # Parse arguments. single_build=0