44

Is there any way to import the changelog that is generated by Jenkins to the subject of an email (either through the default email, or the email-ext plugin)?

I am new to Jenkins configuration, so I apologize if this is a simple issue, but I was not able to find anything on the email-ext documentation.

Igor
  • 30,885
  • 14
  • 70
  • 107

4 Answers4

78

I configured my Email-ext plug-in to use the CHANGES Token (official documentation here):

Changes:
${CHANGES, showPaths=true, format="%a: %r %p \n--\"%m\"", pathFormat="\n\t- %p"}

That prints the following in my build notifications:

Changes:
Username: 123
    - Project/Filename1.m
    - Project/Filename2.m
    -- "My log message"

For HTML messages, I placed the same code within a div and added formatting:

<div style="padding-left: 30px; padding-bottom: 15px;">
${CHANGES, showPaths=true, format="<div><b>%a</b>: %r %p </div><div style=\"padding-left:30px;\"> &#8212; &#8220;<em>%m</em>&#8221;</div>", pathFormat="</div><div style=\"padding-left:30px;\">%p"}
</div>

Here's a sample screenshot of how it looks in the e-mails sent out by Jenkins now (this particular commit came from Subversion, but it works exactly the same for Git and other version control systems):

Change list for Jenkins

Russell Gallop
  • 1,411
  • 2
  • 15
  • 32
Steve HHH
  • 12,073
  • 5
  • 64
  • 68
  • 4
    How did you know to do this? I have looked at that official documentation page, and I don't see: 1) the list of usable tokens and how to use them, 2) where to put them ("Default Content" in the "Configure System" page of Jenkins?). – dfrankow Oct 03 '12 at 18:43
  • Looks like each Java class in https://github.com/hudson-plugins/email-ext-plugin/tree/master/src/main/java/hudson/plugins/emailext/plugins/content is a token. – dfrankow Oct 03 '12 at 20:00
  • Also, clicking the ? to the right of "Context Token Reference" below "Default Content" gives descriptions. – dfrankow Oct 03 '12 at 20:09
  • 9
    How did I know how to do that? To be honest, I didn't know, and spent many hours searching for examples, reading documentation, experimenting, and reverse-engineering. – Steve HHH Oct 03 '12 at 22:19
  • 1
    @SteveHHH where to add this thing?,i am adding it into content but result is blank – Ankit Gupta Dec 01 '14 at 09:36
  • 2
    If there are multiple lines of commit message does this display? I tried this. It just displays only first line of commit message. If commit message is of 4 lines this displays only first line. Any solution for this problem? – sharp Aug 18 '15 at 23:31
  • Worked for me. Thanks – Amna Feb 15 '17 at 06:34
  • This does not address longer commit messages. – user1567291 Feb 24 '17 at 17:17
  • A list of content tokens that can be used can be found on the Jenkins Configuration page. See https://stackoverflow.com/questions/10832486/jenkins-email-ext-plugin-tokens – Gayle Jul 03 '17 at 17:21
20

From original documentation: To see a list of all available email tokens and what they display, you can click the "?" (question mark) associated with the Content Token Reference at the bottom of the email-ext section on the project configuration screen.

Here is result:

${CHANGES}
Displays the changes since the last build.

showDependencies
    If true, changes to projects this build depends on are shown. Defaults to false
showPaths
    If true, the paths, modifued by a commit are shown. Defaults to false
format
    For each commit listed, a string containing %X, where %x is one of:

    %a
        author
    %d
        date
    %m
        message
    %p
        path
    %r
        revision

    Not all revision systems support %d and %r. If specified showPaths argument is ignored. Defaults to "[%a] %m\\n"
pathFormat
    A string containing %p to indicate how to print paths. Defaults to "\\t%p\\n"
1

Not in the subject of an email though you can send the change log to the recipient as an attachment in a mail using Git Changelog Plugin as post build action in Jenkins Job. Select Create a file checkbox, give a name to a file (CHANGELOG.md for me), as in below image:

enter image description here

Make sure you have configured Source Code Management as GIT in Jenkins JOB.

Then create Editable Email Notification post build action and copy the name of the git change log file as the value of Attachments, as in below image:

enter image description here

Arpit Aggarwal
  • 21,748
  • 13
  • 80
  • 99
1

From version 2.0 and later of Git Changelog Plugin, you can get the changelog as a string in a pipeline. And then just use that variable in the mail.

node {
 deleteDir()
 sh """
 git clone git@github.com:jenkinsci/git-changelog-plugin.git .
 """

 def changelogString = gitChangelog returnType: 'STRING',
  from: [type: 'REF', value: 'git-changelog-1.50'],
  to: [type: 'REF', value: 'master'],
  template: """
  <h1> Git Changelog changelog </h1>

<p>
Changelog of Git Changelog.
</p>

{{#tags}}
<h2> {{name}} </h2>
 {{#issues}}
  {{#hasIssue}}
   {{#hasLink}}
<h2> {{name}} <a href="{{link}}">{{issue}}</a> {{title}} </h2>
   {{/hasLink}}
   {{^hasLink}}
<h2> {{name}} {{issue}} {{title}} </h2>
   {{/hasLink}}
  {{/hasIssue}}
  {{^hasIssue}}
<h2> {{name}} </h2>
  {{/hasIssue}}


   {{#commits}}
<a href="https://github.com/tomasbjerre/git-changelog-lib/commit/{{hash}}">{{hash}}</a> {{authorName}} <i>{{commitTime}}</i>
<p>
<h3>{{{messageTitle}}}</h3>

{{#messageBodyItems}}
 <li> {{.}}</li> 
{{/messageBodyItems}}
</p>


  {{/commits}}

 {{/issues}}
{{/tags}}
  """

mail bcc: '', body: """Here is the changelog:

${changelogString}
""", cc: '', from: '', replyTo: '', subject: 'The Changelog', to: 'the@email'
}
Tomas Bjerre
  • 2,594
  • 18
  • 20