From 607232dd23f5924d221e0851993a45f76eaa7936 Mon Sep 17 00:00:00 2001 From: Dan Hawkins Date: Fri, 19 Nov 2021 23:45:34 +0000 Subject: [PATCH] Add failed build action --- build/postactions.groovy | 109 ++++++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 30 deletions(-) diff --git a/build/postactions.groovy b/build/postactions.groovy index fbf98b6..ac5eabd 100644 --- a/build/postactions.groovy +++ b/build/postactions.groovy @@ -1,35 +1,45 @@ /* Example post build info { - "Build outcomes": { - "Success": { - "Actions": { - "Deploy doxygen": { - "Description": "Can be empty - Name of doxygen deploy job to run if this build is successful", - "Job": "HMB_FD_HV_0.9.6/HMB_FD_HV_0.9.6_sg1024_doxygen_deploy" + "Build outcomes": { + "Success": { + "Actions": { + "Deploy doxygen": { + "Description": "Can be empty - Name of doxygen deploy job to run if this build is successful", + "Job": "HMB_FD_HV_0.9.6/HMB_FD_HV_0.9.6_sg1024_doxygen_deploy" + }, + "Static analysis": { + "Description": "Can be empty - Static analysis job to run when this build is successful", + "Job": "" + }, + "Unit test": { + "Description": "Can be empty - Unit test job to run when this build is successful", + "Job": "" + }, + "Regression test": { + "Description": "Can be empty - Regression test job to run when this build is successful", + "Job": "HMB_FD_HV_0.9.6/HMB_FD_HV_0.9.6_sg1024_run_tests" + }, + "Update differences": { + "Description": "Can be empty - Update differences job to run when this build is successful", + "Job": "HMB_FD_HV_diff_releases" + }, + "Release to git": { + "Description": "Can be empty - Name of release to git job to run if this build is successful", + "Job": "HMB_FD_HV_release_to_git" + } + } }, - "Static analysis": { - "Description": "Can be empty - Static analysis job to run when this build is successful", - "Job": "" - }, - "Unit test": { - "Description": "Can be empty - Unit test job to run when this build is successful", - "Job": "" - }, - "Regression test": { - "Description": "Can be empty - Regression test job to run when this build is successful", - "Job": "HMB_FD_HV_0.9.6/HMB_FD_HV_0.9.6_sg1024_run_tests" - }, - "Update differences": { - "Description": "Can be empty - Update differences job to run when this build is successful", - "Job": "HMB_FD_HV_diff_releases" - }, - "Release to git": { - "Description": "Can be empty - Name of release to git job to run if this build is successful", - "Job": "HMB_FD_HV_release_to_git" + "Failed": { + "Actions": { + "Email": { + "Description": "Can be empty - Name of release to git job to run if this build is successful", + "To": [ + "dan.hawkins@globalinkjetsystems.com" + ] + } + } } - } } - } } */ @@ -88,12 +98,18 @@ def deployjob(String buildselector, postbuildactions, String action, String alte } } +def readPostBuildInfo() { + def postbuildinfo = readJSON(text: POST_BUILD_INFO) + def postbuildjsontext = writeJSON(returnText: true, json: postbuildinfo, pretty: 1) + echo "Post build info:\n${postbuildjsontext}" + + return postbuildinfo +} + def performSuccessfulBuildActions() { def postbuildactions if (params.POST_BUILD_INFO != null) { - def postbuildinfo = readJSON(text: POST_BUILD_INFO) - def postbuildjsontext = writeJSON(returnText: true, json: postbuildinfo, pretty: 1) - echo "Post build info:\n${postbuildjsontext}" + def postbuildinfo = readPostBuildInfo() try { postbuildactions = postbuildinfo['Build outcomes']['Success']['Actions'] postbuildinfoavailable = true @@ -112,4 +128,37 @@ def performSuccessfulBuildActions() { deployjob(buildselector, postbuildactions, 'Release to git', 'RELEASE_TO_GIT_JOB') } +def sendemails(emailtolist) { + def emailbody = """ + Failed build: ${JOBNAME} ${BUILD_DISPLAY_NAME} : ${BUILD_URL} + """ + + for(emailto in emailtolist) { + mail( + subject: "Failed build: ${JOBNAME} ${BUILD_DISPLAY_NAME}", + body: emailbody, + to: emailto + ) + } +} + +def performFailedBuildActions() { + def postbuildactions = null + if (params.POST_BUILD_INFO != null) { + def postbuildinfo = readPostBuildInfo() + try { + postbuildactions = postbuildinfo['Build outcomes']['Failed']['Actions'] + } catch(Exception ex) { + echo "Post build info did not contain: Build outcomes/Failed/Actions" + postbuildactions = null + } + } + if (postbuildactions) { + def emailtolist = postbuildactions['Email']['To'] + if (emailtolist) { + sendemails(emailtolist) + } + } +} + return this;