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
    • git-plugin, scm-api-plugin
    • 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

          This is not actually a fault with emailext its with the SCM api. First build has no build to compare it to so no way to determine what has happened since. emailext purely asks the SCM api for this info

          David van Laatum added a comment - This is not actually a fault with emailext its with the SCM api. First build has no build to compare it to so no way to determine what has happened since. emailext purely asks the SCM api for this info

          Zack Snyder added a comment -

          But it has the info who has committed the changes.
          So how can it be fixed? How can I get the developer of the current commit which is been building?

          Zack Snyder added a comment - But it has the info who has committed the changes. So how can it be fixed? How can I get the developer of the current commit which is been building?

          Determining who is the developer of a build is done by jenkins and the SCM api not emailext. If you look at the build details and you see no changelog emailext has no hope

          David van Laatum added a comment - Determining who is the developer of a build is done by jenkins and the SCM api not emailext. If you look at the build details and you see no changelog emailext has no hope

          Zack Snyder added a comment - - edited


          You are right, the git change log looks like its missing the important details...strange.
          davidvanlaatum
          Can you please reopen and change the assignee?

          Zack Snyder added a comment - - edited You are right, the git change log looks like its missing the important details...strange. davidvanlaatum Can you please reopen and change the assignee?

          Mark Waite added a comment -

          The changelog of the first build of a branch is always empty. That is how it has always been.

          The changelog reports changes since the preceding build. There is no preceding build on the first build and no changelog for the first build.

          Mark Waite added a comment - The changelog of the first build of a branch is always empty. That is how it has always been. The changelog reports changes since the preceding build. There is no preceding build on the first build and no changelog for the first build.

          Zack Snyder added a comment -

          markewaite
          Okay, but how can I get what I want?
          When I make a new branch, push this new branch to origin a new build is triggered (code was changed, a commit is associated with my email address)
          How can the user gets notified that the build was successful?

          Zack Snyder added a comment - markewaite Okay, but how can I get what I want? When I make a new branch, push this new branch to origin a new build is triggered (code was changed, a commit is associated with my email address) How can the user gets notified that the build was successful?

          Mark Waite added a comment -

          zack I think there are several different ways to get the information you want.

          You could save the return value of the checkout step and use the GIT_* entries in that map (though see JENKINS-45489 for a bug which may affect that technique for your case). That is described in a Jenkins Minute video and in the documentation

          You could call git log -n 1 and read the output of git log for the author name.

          Mark Waite added a comment - zack I think there are several different ways to get the information you want. You could save the return value of the checkout step and use the GIT_* entries in that map (though see JENKINS-45489 for a bug which may affect that technique for your case). That is described in a Jenkins Minute video and in the documentation You could call git log -n 1 and read the output of git log for the author name.

          I can see the point that when a new multi-branch job is created by the creation of a new branch it does make sense that the commit(s) that created the branch should really be counted as a change. Not sure how feasible this is though.

          David van Laatum added a comment - I can see the point that when a new multi-branch job is created by the creation of a new branch it does make sense that the commit(s) that created the branch should really be counted as a change. Not sure how feasible this is though.

          Mark Waite added a comment - - edited

          I can think of many different ways to define the set of changes which should be displayed in job creation. Some examples that immediately come to mind include:

          • No changes, this is the first job on the branch so there can't be any changes (current behavior)
          • Tip of the new branch (as requested by zack)
          • Chain of changes on the branch compared to a defined base branch (use double dot comparison notation)
          • Chain of changes on the branch since it diverged from a defined base branch (use triple dot comparison notation as described on stackoverflow) - matches a common technique of creating a new branch and adding several commits to the branch before I push it to the remote git server
          • Fixed number of changes on the branch (branch created which was created as an orphan)

          I'm sure there are other scenarios as well. Unfortunately, I don't intend to change the current behavior. I believe zack can get the behavior he wants from within Pipeline, and others could do the same, without requiring changes to the git plugin (and the 130000 installations of the git plugin).

          Mark Waite added a comment - - edited I can think of many different ways to define the set of changes which should be displayed in job creation. Some examples that immediately come to mind include: No changes, this is the first job on the branch so there can't be any changes (current behavior) Tip of the new branch (as requested by zack ) Chain of changes on the branch compared to a defined base branch (use double dot comparison notation) Chain of changes on the branch since it diverged from a defined base branch (use triple dot comparison notation as described on stackoverflow ) - matches a common technique of creating a new branch and adding several commits to the branch before I push it to the remote git server Fixed number of changes on the branch (branch created which was created as an orphan ) I'm sure there are other scenarios as well. Unfortunately, I don't intend to change the current behavior. I believe zack can get the behavior he wants from within Pipeline, and others could do the same, without requiring changes to the git plugin (and the 130000 installations of the git plugin).

          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: