#74 Send message if pull-request build fails
Merged 4 years ago by mkovarik. Opened 4 years ago by pbortlov.
pbortlov/c3i-library build-fails-email  into  master

@@ -97,28 +97,20 @@ 

            // with special bash characters (like whitespaces, #, etc).

            // https://bugzilla.redhat.com/show_bug.cgi?id=1625518

            currentBuild.displayName = "PR#${env.PR_NO}"

-           // To enable HTML syntax in build description, go to `Jenkins/Global Security/Markup Formatter` and select 'Safe HTML'.

-           def pagureLink = """<a href="${env.PR_URL}">${currentBuild.displayName}</a>"""

-           try {

-             def prInfo = pagure.getPR(env.PR_NO)

-             pagureLink = """<a href="${env.PR_URL}">PR#${env.PR_NO}: ${escapeHtml(prInfo.title)}</a>"""

-             // set PR status to Pending

-             if (params.PAGURE_API_KEY_SECRET_NAME)

-               pagure.setBuildStatusOnPR(null, 'Building...')

-           } catch (Exception e) {

-             echo "Error using pagure API: ${e}"

+ 

+           def prInfo = pagure.getPR(env.PR_NO)

+           def pagureLink = """<a href="${env.PR_URL}">PR#${env.PR_NO}: ${escapeHtml(prInfo.title)}</a>"""

+           // set PR status to Pending

+           if (params.PAGURE_API_KEY_SECRET_NAME) {

+             pagure.setBuildStatusOnPR(null, 'Building...')

            }

            currentBuild.description = pagureLink

            {% else %}

            currentBuild.displayName = "${env.GIT_REPO_REF}: ${env.GIT_COMMIT.substring(0, 7)}"

            currentBuild.description = """<a href="${pagure_repo_home}/c/${env.GIT_COMMIT}">${currentBuild.displayName}</a>"""

            if (params.PAGURE_API_KEY_SECRET_NAME) {

-             try {

-               pagure.flagCommit('pending', null, 'Building...')

-               echo "Updated commit ${env.GIT_COMMIT} status to PENDING."

-             } catch (e) {

-               echo "Error updating commit ${env.GIT_COMMIT} status to PENDING: ${e}"

-             }

+             pagure.flagCommit('pending', null, 'Building...')

+             echo "Updated commit ${env.GIT_COMMIT} status to PENDING."

            }

            {% endif %}

            {% include "get_paas_domain.groovy" %}
@@ -132,32 +124,28 @@ 

      {% include "build-workflow/build-default.groovy" %}

      {% endif %}

  

-   } // stages

-   post {

-     success {

-       script {

-         // on pre-merge workflow success

-         if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == "premerge") {

-           try {

+     stage('Report results') {

+       steps {

+         script {

+           if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == "premerge") {

              pagure.setBuildStatusOnPR(100, 'Build passed.')

              echo "Updated PR #${env.PR_NO} status to PASS."

-           } catch (e) {

-             echo "Error updating PR #${env.PR_NO} status to PASS: ${e}"

            }

-         }

-         // on post-merge workflow success

-         if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == 'postmerge') {

-           try {

+           // on post-merge workflow success

+           if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == 'postmerge') {

              pagure.flagCommit('success', 100, 'Build passed.')

              echo "Updated commit ${env.GIT_COMMIT} status to PASS."

-           } catch (e) {

-             echo "Error updating commit ${env.GIT_COMMIT} status to PASS: ${e}"

            }

-         }

-       } // script

-     } // success

+         } // script

+       } // steps

+     } // stage

+   } // stages

+   post {

      failure {

        script {

+         def subject = "Jenkins job ${env.JOB_NAME} #${env.BUILD_NUMBER} failed."

+         def body = "Build URL: ${env.BUILD_URL}. "

+ 

          // on pre-merge workflow failure

          if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == 'premerge') {

            // updating Pagure PR flag
@@ -166,6 +154,9 @@ 

              echo "Updated PR #${env.PR_NO} status to FAILURE."

            } catch (e) {

              echo "Error updating PR #${env.PR_NO} status to FAILURE: ${e}"

+             if (params.MAIL_ADDRESS){

+               emailext to: params.MAIL_ADDRESS, subject: subject, body: body

+             }

            }

            // making a comment

            try {
@@ -176,6 +167,9 @@ 

              echo "Comment made."

            } catch (e) {

              echo "Error making a comment on PR #${env.PR_NO}: ${e}"

+             if (params.MAIL_ADDRESS){

+               emailext to: params.MAIL_ADDRESS, subject: subject, body: body

+             }

            }

          }

          // on post-merge workflow failure
@@ -187,18 +181,14 @@ 

                echo "Updated commit ${env.GIT_COMMIT} status to FAILURE."

              } catch (e) {

                echo "Error updating commit ${env.GIT_COMMIT} status to FAILURE: ${e}"

+               if (params.MAIL_ADDRESS){

+                 emailext to: params.MAIL_ADDRESS, subject: subject, body: body

+               }

              }

            }

            // sending email

            if (params.MAIL_ADDRESS){

-             try {

-               def recipient = params.MAIL_ADDRESS

-               def subject = "Jenkins job ${env.JOB_NAME} #${env.BUILD_NUMBER} failed."

-               def body = "Build URL: ${env.BUILD_URL}"

-               emailext to: recipient, subject: subject, body: body

-             } catch (e) {

-               echo "Error sending email: ${e}"

-             }

+             emailext to: params.MAIL_ADDRESS, subject: subject, body: body

            }

          }

        } // script

Email will be sent when the build fails

rebased onto 2712118

4 years ago

rebased onto 93b1075

4 years ago

rebased onto 671427b

4 years ago

rebased onto 1e623fa

4 years ago

rebased onto 0df6f78

4 years ago

Build 0df6f78 FAILED!
Rebase or make new commits to rebuild.

Pull-Request has been merged by mkovarik

4 years ago