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

EMail-Ext does not send email, because or empty recipients

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Critical Critical
    • None
    • master node on Ubuntu x64
      slave node on Windows

      I send my mail via groovy script in a multibranch-pipeline (blue ocean)

      script 
                  {
                      def currResult = getResultAsString(currentBuild)
                      def prevResult = getResultAsString(currentBuild.getPreviousBuild())
                      if (currResult != "SUCCESS" || prevResult != "SUCCESS") 
                      {
                           echo 'send mail'
                           emailext body: '''${SCRIPT, template="my-html.template"}''',
                                  recipientProviders: [[$class: 'DevelopersRecipientProvider'],
                                                       [$class: 'CulpritsRecipientProvider'],
                                                       [$class: 'UpstreamComitterRecipientProvider'],
                                                      ],
                                  subject: '[Jenkins]: ${JOB_NAME} ${BUILD_DISPLAY_NAME} - ' + currResult,
                                  mimeType: 'text/html'
                      }
                      
                  }
      
      def getResultAsString(build)
      {
          def result = build?.result
          if (result == null) {
              result = "SUCCESS"
          }
          return result;
      }
      

      When creating a new branch and adding a commit which will fail this build,
      jenkins will not send a email, with following message:

      An attempt to send an e-mail to empty list of recipients, ignored.
      

      However, when adding another commit to the same branch, jenkins, will now surprisingly have a commiter email address and can send the email.
      So always the first build of a new branch, email-ext will not retrieve a correct email from the commit.

      Can you please fix it?

          [JENKINS-51218] EMail-Ext does not send email, because or empty recipients

          I just think it logically makes sense when a new branch is created in a multi branch style job that the creation of the branch should be classified as a change which in turn would cause every other plugin that uses the list of developers/commiters to do something would pick this up. I think most users would expect this to be the case in multi branch. Not sure if the responsibility to do this lives in the multi branch plugin, scm api, the scm plugin/driver or a combination, I just think its what users would expect, I know I would, not that I have actually tested with SVN (which I use I work).  I could code a special case in emailext but I think it would be better if its handled globally

          David van Laatum added a comment - I just think it logically makes sense when a new branch is created in a multi branch style job that the creation of the branch should be classified as a change which in turn would cause every other plugin that uses the list of developers/commiters to do something would pick this up. I think most users would expect this to be the case in multi branch. Not sure if the responsibility to do this lives in the multi branch plugin, scm api, the scm plugin/driver or a combination, I just think its what users would expect, I know I would, not that I have actually tested with SVN (which I use I work).  I could code a special case in emailext but I think it would be better if its handled globally

          Zack Snyder added a comment - - edited

          markewaite
          Okay, you say I can get the email address of the user who changed the code with git log -n 1
          I am new to jenkins and do not understand how to do it properly.

          Can you please show me the complete code, see the pipeline script above how I send the email, can you adjust it?

          PS: IMHO, I think the git scm plugin should return this info, creating a branch, with no changes is one thing, but creating a branch with changes, it is clear for me that this are changes.

          Zack Snyder added a comment - - edited markewaite Okay, you say I can get the email address of the user who changed the code with git log -n 1 I am new to jenkins and do not understand how to do it properly. Can you please show me the complete code, see the pipeline script above how I send the email, can you adjust it? PS: IMHO, I think the git scm plugin should return this info, creating a branch, with no changes is one thing, but creating a branch with changes, it is clear for me that this are changes.

          Mark Waite added a comment -

          zack I think you can get the e-mail address of the author of the most recent commit with the Pipeline command:

          author_email = sh returnStdout:true,  script:'git log -n 1 --pretty=format:%ae'
          echo "author E-mail is ${author_email}"
          

          The git log documentation provides more details if you need to extract other information from that commit and the sh step documentation can provide additional pointers for its use.

          If you're on Windows, replace sh with bat

          Mark Waite added a comment - zack I think you can get the e-mail address of the author of the most recent commit with the Pipeline command: author_email = sh returnStdout:true, script:'git log -n 1 --pretty=format:%ae' echo "author E-mail is ${author_email}" The git log documentation provides more details if you need to extract other information from that commit and the sh step documentation can provide additional pointers for its use. If you're on Windows, replace sh with bat

          Zack Snyder added a comment -

          markewaite
          Okay, with some minor changes I can retrive the mail address.
          However, how can I use it with ext-mail?

          def author_email = bat(returnStdout:true,  script:'git log -n 1 --pretty=format:%%ae')
                          echo "author E-mail is ${author_email}"
                          
                          def currResult = getResultAsString(currentBuild)
                          def prevResult = getResultAsString(currentBuild.getPreviousBuild())
                          if (currResult != "SUCCESS" || prevResult != "SUCCESS") 
                          {
                               echo 'send mail'
                               emailext body: '''${SCRIPT, template="xion-html.template"}''',
                                      recipientProviders: [[$class: 'DevelopersRecipientProvider'],
                                                           [$class: 'CulpritsRecipientProvider'],
                                                           [$class: 'UpstreamComitterRecipientProvider']
                                                          ] ${author_email},
                                      subject: '[Jenkins]: ${JOB_NAME} ${BUILD_DISPLAY_NAME} - ' + currResult,
                                      mimeType: 'text/html'
                          }
          

          I got following error:

          WorkflowScript: 74: expecting '}', found ',' @ line 74, column 66.
          
                          ] ${author_email},
          

          Zack Snyder added a comment - markewaite Okay, with some minor changes I can retrive the mail address. However, how can I use it with ext-mail? def author_email = bat(returnStdout:true, script:'git log -n 1 --pretty=format:%%ae') echo "author E-mail is ${author_email}" def currResult = getResultAsString(currentBuild) def prevResult = getResultAsString(currentBuild.getPreviousBuild()) if (currResult != "SUCCESS" || prevResult != "SUCCESS") { echo 'send mail' emailext body: '''${SCRIPT, template="xion-html.template"}''', recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'CulpritsRecipientProvider'], [$class: 'UpstreamComitterRecipientProvider'] ] ${author_email}, subject: '[Jenkins]: ${JOB_NAME} ${BUILD_DISPLAY_NAME} - ' + currResult, mimeType: 'text/html' } I got following error: WorkflowScript: 74: expecting '}', found ',' @ line 74, column 66. ] ${author_email},

          Mark Waite added a comment -

          zack sorry, but I don't use the email-ext plugin. I would assume that the block in your example which says

                                      recipientProviders: [[$class: 'DevelopersRecipientProvider'],
                                                           [$class: 'CulpritsRecipientProvider'],
                                                           [$class: 'UpstreamComitterRecipientProvider']
                                                          ] ${author_email},
          

          should instead say:

                                      recipientProviders: [[$class: 'DevelopersRecipientProvider'],
                                                           [$class: 'CulpritsRecipientProvider'],
                                                           [$class: 'UpstreamComitterRecipientProvider']
                                                          ] author_email,
          

          In this case, author_email is a variable.

          Mark Waite added a comment - zack sorry, but I don't use the email-ext plugin. I would assume that the block in your example which says recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'CulpritsRecipientProvider'], [$class: 'UpstreamComitterRecipientProvider'] ] ${author_email}, should instead say: recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'CulpritsRecipientProvider'], [$class: 'UpstreamComitterRecipientProvider'] ] author_email, In this case, author_email is a variable.

          Zack Snyder added a comment -

          markewaite
          It still does not work. The other error is:

          WorkflowScript: 75: expecting '}', found ',' @ line 75, column 100.
             DISPLAY_NAME} - ' + currResult,
          

          The retrieve of the email address does not work perfectly, I get in fact the whole command back:

          def author_email = bat(returnStdout:true,  script:'git log -n 1 --pretty=format:%%ae')
                          echo "author E-mail is ${author_email}"
          

          Leads to following output:

          author E-mail is 
          G:\jenkins\workspace\app_test-email-branch-27DKLR7UW4OLTKKVCAORJQDJDVIHVIRGU2SWE3JRP7LEG3QEVBWQ>git log -n 1 --pretty=format:%ae 
          user@mail.de
          

          What do you think?

          Zack Snyder added a comment - markewaite It still does not work. The other error is: WorkflowScript: 75: expecting '}', found ',' @ line 75, column 100. DISPLAY_NAME} - ' + currResult, The retrieve of the email address does not work perfectly, I get in fact the whole command back: def author_email = bat(returnStdout:true, script:'git log -n 1 --pretty=format:%%ae') echo "author E-mail is ${author_email}" Leads to following output: author E-mail is G:\jenkins\workspace\app_test-email-branch-27DKLR7UW4OLTKKVCAORJQDJDVIHVIRGU2SWE3JRP7LEG3QEVBWQ>git log -n 1 --pretty=format:%ae user@mail.de What do you think?

          Mark Waite added a comment -

          In the case of the unexpected extra output in the stdout of the bat step, that is apparently JENKINS-44569. I confirmed with my test job that the technique described in JENKINS-44569 works. I disagree with the resolution of JENKINS-44569 as "Not a defect", but that's a different topic.

          I'm no help on the Pipeline scripting syntax error. I've used the "Pipeline Syntax" link on the page to assist with generating the correct syntax for Pipeline steps.

          Mark Waite added a comment - In the case of the unexpected extra output in the stdout of the bat step, that is apparently JENKINS-44569 . I confirmed with my test job that the technique described in JENKINS-44569 works. I disagree with the resolution of JENKINS-44569 as "Not a defect", but that's a different topic. I'm no help on the Pipeline scripting syntax error. I've used the "Pipeline Syntax" link on the page to assist with generating the correct syntax for Pipeline steps.

          you need to add a to parameter, recipientProviders is a list of classes that generate recipients

          emailext body: '', subject: '', to: 'abc@example.com'

          David van Laatum added a comment - you need to add a to parameter, recipientProviders is a list of classes that generate recipients emailext body: '', subject: '', to: 'abc@example.com'

          Zack Snyder added a comment -

          davidvanlaatum
          This works. Thanks.
          The thing is, I always cleanup my build dir when the develop branch is build.

          when { branch 'develop' }
                       steps {
                          echo 'Clean up workspace...'
                          deleteDir()
                      }
          

          This will lead to the behaviour that the complete git dir is removed and git does not work anymore.
          So my post script will try to perfom a git log and I get this:

          fatal: Not a git repository (or any of the parent directories): .git
          
          G:\jenkins\workspace\app_develop-NBDLPDI4IZVRV4I2HQPT77BU7XOUGHLE5BQFTJBAVDWCAQXXBCVQ>git log -n 1 --pretty=format:%ae 
          
          script returned exit code 128
          

          Any Idea how I can fix this?

          I need something like this:

          If (branch('develop') {
          author_email = bat(script: 'git log -n 1 --pretty=format:%%ae', returnStdout:true).trim().split("\n")
           }
          

          Do you know how I can check in the post step for the current branch name?

          Zack Snyder added a comment - davidvanlaatum This works. Thanks. The thing is, I always cleanup my build dir when the develop branch is build. when { branch 'develop' } steps { echo 'Clean up workspace...' deleteDir() } This will lead to the behaviour that the complete git dir is removed and git does not work anymore. So my post script will try to perfom a git log and I get this: fatal: Not a git repository (or any of the parent directories): .git G:\jenkins\workspace\app_develop-NBDLPDI4IZVRV4I2HQPT77BU7XOUGHLE5BQFTJBAVDWCAQXXBCVQ>git log -n 1 --pretty=format:%ae script returned exit code 128 Any Idea how I can fix this? I need something like this: If (branch('develop') { author_email = bat(script: 'git log -n 1 --pretty=format:%%ae', returnStdout:true).trim().split("\n") } Do you know how I can check in the post step for the current branch name?

          Dmitrii Lapin added a comment -

          Dmitrii Lapin added a comment - Same older ticket:  https://issues.jenkins-ci.org/browse/JENKINS-24638

            Unassigned Unassigned
            zack Zack Snyder
            Votes:
            1 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: