如何在Jenkins邮件中包含Git变更日志?

44

有没有办法将Jenkins生成的更改日志导入电子邮件主题(通过默认电子邮件或email-ext插件)?

我对Jenkins配置还很陌生,所以如果这是一个简单的问题,我很抱歉,但我在email-ext文档中找不到任何信息。

4个回答

78

我配置了我的Email-ext插件以使用CHANGES Token(官方文档在此):

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

这将在我的构建通知中打印以下内容:
Changes:
Username: 123
    - Project/Filename1.m
    - Project/Filename2.m
    -- "My log message"

对于 HTML 消息,我将相同的代码放在一个 div 中,并添加了格式:
<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>

这是 Jenkins 现在发送的电子邮件中显示的样例截图(该提交来自 Subversion,但 Git 和其他版本控制系统也完全相同): Jenkins 的变更列表

4
你是怎么知道这个的?我看了官方文档页面,但没有看到可用令牌列表及其使用方法,也不知道该把它们放在哪里(是在Jenkins的“配置系统”页面中的“默认内容”中吗?)。请问你如何了解这些信息? - dfrankow
9
我怎么知道该怎么做呢?说实话,我不知道,花了很多时间搜索示例,阅读文档,进行实验和逆向工程。 - Steve HHH
1
@SteveHHH 这个东西要加到哪里?我加到内容里但结果是空白的。 - Ankit Gupta
2
如果提交信息有多行,会显示吗?我尝试过了,它只显示提交信息的第一行。如果提交信息有4行,它只会显示第一行。有没有解决这个问题的方法? - sharp
可以在Jenkins配置页面上找到可用的内容令牌列表。请参见https://dev59.com/8mgv5IYBdhLWcg3wHdN_。 - Gayle
显示剩余4条评论

20

根据原始文档:

要查看所有可用的电子邮件令牌及其显示内容列表,您可以单击项目配置屏幕上的电子邮件扩展部分底部与内容令牌参考相关联的“?”(问号)。

这里是结果:

${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
Git Changelog Plugin 2.0版本及更高版本开始,您可以在流水线中将变更日志作为字符串获取,然后在邮件中使用该变量。
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'
}

1

虽然不能在邮件主题中添加更改日志,但是您可以使用Git Changelog Plugin作为Jenkins作业的后续构建操作,在邮件中以附件的形式将更改日志发送给收件人。选择创建文件复选框,为文件命名(例如CHANGELOG.md),如下图所示:

enter image description here

请确保您已在Jenkins JOB中配置了源代码管理GIT

然后创建可编辑邮件通知构建后操作,并将git更改日志文件的名称复制为Attachments的值,如下图所示:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接