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

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

      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 } }
      }
      

          [JENKINS-42688] Provide a way to enable a post/* to run only under some context

          Baptiste Mathus created issue -
          Baptiste Mathus made changes -
          Description Original: h2. 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)

          h2. Example:

          {code:java}
          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"
                      }
                  }
              }
          }
          {code}

          h3. Illustration of what could be used (WARNING: do not copy paste below if you just found that JIRA :-))

          h4. Expressing the {{when}} each time

          {code:java}
          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 } }
              }
          ...
          }
          {code}

          h4. Expressing the {{when}} as a default for every {{post/*}} blocks
          New: h2. 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)

          h2. Example:

          {code:java}
          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"
                      }
                  }
              }
          }
          {code}

          h3. Illustration of what could be used (WARNING: do not copy paste below if you just found that JIRA :-))

          h4. Expressing the {{when}} each time

          {code:java}
          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 } }
              }
          }
          {code}

          h4. Expressing the {{when}} as a default for every {{post/*}} blocks
          Baptiste Mathus made changes -
          Description Original: h2. 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)

          h2. Example:

          {code:java}
          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"
                      }
                  }
              }
          }
          {code}

          h3. Illustration of what could be used (WARNING: do not copy paste below if you just found that JIRA :-))

          h4. Expressing the {{when}} each time

          {code:java}
          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 } }
              }
          }
          {code}

          h4. Expressing the {{when}} as a default for every {{post/*}} blocks
          New: h2. 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)

          h2. Example:

          {code:java}
          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"
                      }
                  }
              }
          }
          {code}

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

          h3. Expressing the {{when}} each time

          {code:java}
          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 } }
              }
          }
          {code}

          h3. Expressing the {{when}} as a default for every {{post/*}} blocks

          {code:java}
          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 { env.CHANGE_ID == null } }
          }
          {code}
          Baptiste Mathus made changes -
          Description Original: h2. 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)

          h2. Example:

          {code:java}
          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"
                      }
                  }
              }
          }
          {code}

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

          h3. Expressing the {{when}} each time

          {code:java}
          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 } }
              }
          }
          {code}

          h3. Expressing the {{when}} as a default for every {{post/*}} blocks

          {code:java}
          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 { env.CHANGE_ID == null } }
          }
          {code}
          New: h2. 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)

          h2. Example:

          {code:java}
          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"
                      }
                  }
              }
          }
          {code}

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

          h3. Expressing the {{when}} each time

          {code:java}
          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 } }
              }
          }
          {code}

          h3. Expressing the {{when}} as a default for every {{post/*}} blocks

          {code:java}
          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 } }
          }
          {code}
          Baptiste Mathus made changes -
          Link New: This issue relates to JENKINS-40898 [ JENKINS-40898 ]
          Baptiste Mathus made changes -
          Link New: This issue relates to JENKINS-41060 [ JENKINS-41060 ]
          Andrew Bayer made changes -
          Status Original: Open [ 1 ] New: In Progress [ 3 ]
          Andrew Bayer made changes -
          Link New: This issue is duplicated by JENKINS-54602 [ JENKINS-54602 ]
          Andrew Bayer made changes -
          Assignee Original: Andrew Bayer [ abayer ]

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

              Created:
              Updated: