Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-42688

Provide a way to enable a post/* to run only under some context

XMLWordPrintable

      Problem:

      If you want to only send out notifications in a post/failure block for non pull-requests, you have currently to use the script block.

      Maybe a solution could be to allow when inside post/* (in the example below, having a way to express a default when would probably help make this more concise)

      Example:

      post {
          failure {
              script {
                  if (env.CHANGE_ID == null) {
                      mail to: NOTIFICATION_TARGET, subject: "FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed. $BUILD_URL"
                  }
              }
          }
          unstable {
              script {
                  if (env.CHANGE_ID == null) {
                      mail to: NOTIFICATION_TARGET, subject: "UNSTABLE: ${currentBuild.fullDisplayName}", body: "Huh, we're unstable. $BUILD_URL"
                  }
              }
          }
          changed {
              script {
                  if (env.CHANGE_ID == null && currentBuild.result == null) { // and JENKINS-41060
                      mail to: NOTIFICATION_TARGET, subject: "BACK TO SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay! $BUILD_URL"
                  }
              }
          }
      }
      

      Proposals (WARNING: do not copy paste below if you just found that JIRA )

      Expressing the when each time

      post {
          failure {
              mail to: NOTIFICATION_TARGET, subject: "FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed. $BUILD_URL"
              when { expression { env.CHANGE_ID == null } }
          }
          unstable {
              mail to: NOTIFICATION_TARGET, subject: "UNSTABLE: ${currentBuild.fullDisplayName}", body: "Huh, we're unstable. $BUILD_URL"
              when { expression { env.CHANGE_ID == null } }
          }
          changed {
              mail to: NOTIFICATION_TARGET, subject: "BACK TO SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay! $BUILD_URL"
              when { expression { env.CHANGE_ID == null && currentBuild.result == null } }
          }
      }
      

      Expressing the when as a default for every post/* blocks

      post {
          failure {
              mail to: NOTIFICATION_TARGET, subject: "FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed. $BUILD_URL"
          }
          unstable {
              mail to: NOTIFICATION_TARGET, subject: "UNSTABLE: ${currentBuild.fullDisplayName}", body: "Huh, we're unstable. $BUILD_URL"
          }
          changed {
              mail to: NOTIFICATION_TARGET, subject: "BACK TO SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay! $BUILD_URL"
              when { expression { currentBuild.result == null } } // question arises then: should this expression be cumulative, or overriding the default below
          }
          when { expression { env.CHANGE_ID == null } }
      }
      

            Unassigned Unassigned
            batmat Baptiste Mathus
            Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: