首页 > 解决方案 > 如何在声明性 jenkins 电子邮件通知发布操作中添加 git 提交信息详细信息?

问题描述

我目前有这个帖子操作,通过电子邮件让我知道 jenkins 构建成功:

post {
            always {
            emailext body: "${env.SITE} was deployed to ${DEPLOY_TO} with the following results: ${currentBuild.currentResult} ", subject: "Deployment results for ${env.SITE}", to: 'user@host.com,user2@host.com'
                    }
            }
}

一旦有新的提交进入存储库,就会触发构建,所以我想知道如何包含提交哈希、提交消息,也许还有所有已更改行的提交差异。我真的不知道我可以在电子邮件中获得多详细的信息。我还没有找到任何可以让我在网上开始的东西。预先感谢您的帮助!

标签: gitjenkins

解决方案


您可以为此使用最后更改插件,安装插件后,您可以使用以下管道片段发送带有差异的电子邮件:

pipeline {
    agent any
    stages {
        stage('send html diff') {
            steps {
                git 'https://github.com/YOUR_REPO.git'
                script {
                  def publisher = LastChanges.getLastChangesPublisher "PREVIOUS_REVISION", "SIDE", "LINE", true, true, "", "", "", "", ""
                  publisher.publishLastChanges()
                  def htmlDiff = publisher.getHtmlDiff()
                  writeFile file: 'build-diff.html', text: htmlDiff
                    emailext (
                      subject: "Jenkins - changes of ${env.JOB_NAME} #${env.BUILD_NUMBER}",
                      attachmentsPattern: '**/*build-diff.html',
                      mimeType: 'text/html',
                      body: """<p>See attached diff of build <b>${env.JOB_NAME} #${env.BUILD_NUMBER}</b>.</p>
                        <p>Check build changes on Jenkins <b><a href="${env.BUILD_URL}/last-changes">here</a></b>.</p>""",
                      to: "YOUR-EMAIL@gmail.com" )
                } //end script
            }
        }
    }
}

在插件文档中有一个关于将差异作为电子邮件发送的部分。


推荐阅读