如何在 Jenkins 电子邮件中包含 git changelog?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7773010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:04:35  来源:igfitidea点击:

How to include git changelog in Jenkins emails?

gitemailhudsonjenkinschangelog

提问by Igor

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)?

有什么方法可以将 Jenkins 生成的变更日志导入到电子邮件的主题中(通过默认电子邮件或email-ext 插件)?

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.

我是 Jenkins 配置的新手,所以如果这是一个简单的问题,我深表歉意,但我无法在 email-ext 文档中找到任何内容。

回答by Steve HHH

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

我将我的 Email-ext 插件配置为使用 CHANGES 令牌(此处为官方文档):

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:

对于 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>

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):

这是 Jenkins 现在发送的电子邮件中的示例屏幕截图(此特定提交来自 Subversion,但它对 Git 和其他版本控制系统的工作方式完全相同):

Change list for Jenkins

Jenkins 的更改列表

回答by Yan Oreshchenkov

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.

来自原始文档: 要查看所有可用电子邮件令牌及其显示内容的列表,您可以单击“?” (问号)与项目配置屏幕上 email-ext 部分底部的 Content Token Reference 相关联。

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"

回答by Arpit Aggarwal

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 Pluginas post build action in JenkinsJob. Select Create a filecheckbox, give a name to a file (CHANGELOG.mdfor me), as in below image:

尽管您可以使用Git Changelog Plugin作为JenkinsJob 中的后期构建操作,将更改日志作为邮件中的附件发送给收件人,但不在电子邮件的主题中。选中Create a file复选框,为文件命名(CHANGELOG.md对我而言),如下图所示:

enter image description here

在此处输入图片说明

Make sure you have configured Source Code Managementas GITin Jenkins JOB.

确保您已在 Jenkins JOB 中将Source Code Management配置为GIT

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

然后创建Editable Email Notificationpost build action 并将 git 更改日志文件的名称复制为 的值Attachments,如下图所示:

enter image description here

在此处输入图片说明

回答by Tomas Bjerre

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.

从版本 2.0 及更高版本的Git Changelog Plugin 开始,您可以在管道中以字符串形式获取变更日志。然后只需在邮件中使用该变量。

node {
 deleteDir()
 sh """
 git clone [email protected]: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'
}