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

Groovy function to send notifications in Jenkinsfiles / pipelines

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Major Major
    • notification-plugin
    • None

      It appears that the notification plugin doesnt have the functionality to send a notification from a pipeline or a Jenkinsfile via a groovy command. That's pretty important especially when Jenkinsfiles overwrites configuration data for jenkins jobs, basically making it impossible to send notifications for any jobs (with this plugin) to a http endpoint.

      Ideal functionality would be something like being able to write something like this in a pipeline:

      pipeline {
        agent any
        post {
          always {
            notify( endpoint: "myUrl", message: "optional: job completed" )
          }
        }
      }
      

          [JENKINS-41939] Groovy function to send notifications in Jenkinsfiles / pipelines

          Ryan Jones added a comment -

          Yes, Please add.

          Ryan Jones added a comment - Yes, Please add.

          Mark R added a comment -

          As a workaround you can manually invoke the JobProperty using the properties block and raw class instantiation.

          To support pipelines I think the plugin just needs to add some descriptors on some of the models. example

          properties([
              [$class: 'HudsonNotificationProperty',
                  endpoints: [
                      [
                          event: "all",
                          urlInfo: [urlType: "PUBLIC", urlOrId: 'url1']
                      ],
                      [
                          event: "completed",
                          urlInfo: [urlType: "PUBLIC", urlOrId: 'url2']
                      ]
                  ]
              ]
          ])
          
          
          pipeline {
              agent any
          
              stages {
                  stage('Build') {
                      steps {
                          echo 'Hello World'
                      }
                  }
              }
          }
          

          Mark R added a comment - As a workaround you can manually invoke the JobProperty using the properties block and raw class instantiation. To support pipelines I think the plugin just needs to add some descriptors on some of the models. example properties([ [$class: 'HudsonNotificationProperty' , endpoints: [ [ event: "all" , urlInfo: [urlType: "PUBLIC" , urlOrId: 'url1' ] ], [ event: "completed" , urlInfo: [urlType: "PUBLIC" , urlOrId: 'url2' ] ] ] ] ]) pipeline { agent any stages { stage( 'Build' ) { steps { echo 'Hello World' } } } }

          Ryan Jones added a comment -

          Hmmm this does send the notifications, but is it possible to set the message within the pipeline? I cant figure out how. I really want to send a custom message depending on the outcome of my job. For example, when my job is finished Id like to be able to send a message that says something like:

                    Smoke Tests: All Passed

                    Model Guidelines: All Passed

                    Integration Tests: X tests Failed

           

          I see that endpoint has some methods such as setBuildNotes and ive tried to use it, but unfortunately I'm not well versed enough to know how to call this method or if its even accessible within the pipeline body

           

          Ryan Jones added a comment - Hmmm this does send the notifications, but is it possible to set the message within the pipeline? I cant figure out how. I really want to send a custom message depending on the outcome of my job. For example, when my job is finished Id like to be able to send a message that says something like:           Smoke Tests: All Passed           Model Guidelines: All Passed           Integration Tests: X tests Failed   I see that endpoint has some methods such as setBuildNotes and ive tried to use it, but unfortunately I'm not well versed enough to know how to call this method or if its even accessible within the pipeline body  

            Unassigned Unassigned
            gaieges Evin Callahan
            Votes:
            7 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: