【发布时间】:2017-04-11 12:28:57
【问题描述】:
如何获取上次在 Jenkins 中构建的更改文件中的提交差异并通过电子邮件发送? 在项目上使用的是 Bitbucket Mercurial。 我使用“Email-ext 插件”,但只能发送修改文件的路径。
但我需要获得提交的差异。 我想我需要一个脚本来获取最新的更改? 请帮忙。谢谢。
【问题讨论】:
如何获取上次在 Jenkins 中构建的更改文件中的提交差异并通过电子邮件发送? 在项目上使用的是 Bitbucket Mercurial。 我使用“Email-ext 插件”,但只能发送修改文件的路径。
但我需要获得提交的差异。 我想我需要一个脚本来获取最新的更改? 请帮忙。谢谢。
【问题讨论】:
您可以使用Last Changes plugin 生成差异并存储在工作区的文件中,最后使用 emailext 将差异作为附件发送,以下是使用声明性管道的示例:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git url: 'https://github.com/jenkinsci/last-changes-plugin'
}
}
stage("send diff") {
steps {
script {
def publisher = LastChanges.getLastChangesPublisher null, "SIDE", "LINE", true, true, "", "", "", "", ""
publisher.publishLastChanges()
def changes = publisher.getLastChanges()
def diff = changes.getDiff()
writeFile file: 'build.diff', text: diff
emailext (
subject: "Sending diff of Job '${env.JOB_NAME} #${env.BUILD_NUMBER}'",
attachmentsPattern: '**/*.diff',
mimeType: 'text/html',
body: """<p>See attached diff of '${env.JOB_NAME} [${env.BUILD_NUMBER}]'.:</p>
<p>Check rich diff at <a href="${env.BUILD_URL}/last-changes">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p>""",
to: "YOUR-EMAIL@XYZ.com"
)
}
}
}
}
}
可以在https://diffy.org/ 或任何可以读取差异的工具上查看生成的差异。
您也可以发送 html 格式的差异,请参阅示例管道:
pipeline {
agent any
stages {
stage('send html diff') {
steps {
git 'https://github.com/jenkinsci/last-changes-plugin.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@XYZ.com" )
} //end script
}
}
}
}
【讨论】:
您可以使用 Jenkins api 获取所需的数据 https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API.Use grrovy 脚本以文本格式存储文件。使用电子邮件插件将其作为电子邮件中的内联内容发送。
【讨论】: