jenkins构建邮件通知

简介: jenkins构建邮件通知

开启邮箱的SMTP服务,复制授权码

下载插件

然后点击Jenkins的设置,注意密码是填我们上面复制的授权码

e44f883aaca356d9

然后在我们的项目目录下编写email模板

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
</head>
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
      offset="0">
<table class="table" width="1000" border="1" cellspacing="0" cellpadding="5">
    <caption align="center"><h3>项目构建信息</h3></caption>
    <thead>
    <tr>
        <th>构建项目-<b>${PROJECT_NAME}</b></th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td><font color="#CC0000"><b>构建结果 - ${BUILD_STATUS}</b></font></td>
    </tr>
    <tr>
        <td>测试数量-${TEST_COUNTS, var="total"}</td>
    </tr>
    </tbody>
    <tr>
        本邮件由系统自动发出,无需回复!<br/>
        各位同事,大家好,以下为${PROJECT_NAME}项目构建信息</br>
    </tr>
    <tr>
        <td><br/>
            <b><font color="#0B610B">构建报告</font></b>
            <hr size="2" width="100%" align="center"/>
            <ul>
                <li>构建报告:<a href="${BUILD_URL}allure/">${PROJECT_URL}allure/</li>
            </ul>
        </td>
    </tr>
    <tr>
        <td><br/>
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center"/>
            <ul>
                <li>项目名称: ${PROJECT_NAME}</li>
                <li>构建编号: 第${BUILD_NUMBER}次构建</li>
                <li>触发原因:${CAUSE}</li>
                <li>构建状态:${BUILD_STATUS}</li>
                <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                <li>构建 Url: <a href="${BUILD_URL}">${BUILD_URL}</a></li>
                <li>工作目录: <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                <li>项目 Url: <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
            </ul>
        </td>
    </tr>
    <tr>
        <td>
            <b><font color="#0B610B">构建详细信息</font></b>
            <hr size="2" width="100%" align="center"/>
            <ul>
                <li>BRANCH_NAME: ${BRANCH_NAME}</li>
                <li>CHANGE_ID: ${CHANGE_ID}</li>
                <li>CHANGE_URL: ${CHANGE_URL}</li>
                <li>CHANGE_TITLE: ${CHANGE_TITLE}</li>
                <li>CHANGE_AUTHOR: ${CHANGE_AUTHOR}</li>
                <li>CHANGE_AUTHOR_DISPLAY_NAME: ${CHANGE_AUTHOR_DISPLAY_NAME}</li>
                <li>CHANGE_AUTHOR_EMAIL: ${CHANGE_AUTHOR_EMAIL}</li>
                <li>CHANGE_TARGET: ${CHANGE_TARGET}</li>
                <li>BUILD_NUMBER: ${BUILD_NUMBER}</li>
                <li>BUILD_DISPLAY_NAME: ${BUILD_DISPLAY_NAME}</li>
                <li>BUILD_ID: ${BUILD_ID}</li>
                <li>JOB_NAME: ${JOB_NAME}</li>
                <li>JOB_BASE_NAME: ${JOB_BASE_NAME}</li>
                <li>BUILD_TAG: ${BUILD_TAG}</li>
                <li>EXECUTOR_NUMBER: ${EXECUTOR_NUMBER}</li>
                <li>NODE_NAME: ${NODE_NAME}</li>
                <li>NODE_LABELS: ${NODE_LABELS}</li>
                <li>WORKSPACE: ${WORKSPACE}</li>
                <li>JENKINS_HOME: ${JENKINS_HOME}</li>
                <li>JENKINS_URL: ${JENKINS_URL}</li>
                <li>BUILD_URL: ${BUILD_URL}</li>
                <li>JOB_URL: ${JOB_URL}</li>
                <li>GIT_COMMIT: ${GIT_COMMIT}</li>
                <li>GIT_PREVIOUS_COMMIT: ${GIT_PREVIOUS_COMMIT}</li>
                <li>GIT_PREVIOUS_SUCCESSFUL_COMMIT: ${GIT_PREVIOUS_SUCCESSFUL_COMMIT}</li>
                <li>GIT_BRANCH: ${GIT_BRANCH}</li>
                <li>GIT_LOCAL_BRANCH: ${GIT_LOCAL_BRANCH}</li>
                <li>GIT_URL: ${GIT_URL}</li>
                <li>GIT_COMMITTER_NAME: ${GIT_COMMITTER_NAME}</li>
                <li>GIT_AUTHOR_NAME: ${GIT_AUTHOR_NAME}</li>
                <li>GIT_COMMITTER_EMAIL: ${GIT_COMMITTER_EMAIL}</li>
                <li>GIT_AUTHOR_EMAIL: ${GIT_AUTHOR_EMAIL}</li>
                <li>MERCURIAL_REVISION: ${MERCURIAL_REVISION}</li>
                <li>MERCURIAL_REVISION_SHORT: ${MERCURIAL_REVISION_SHORT}</li>
                <li>MERCURIAL_REVISION_NUMBER: ${MERCURIAL_REVISION_NUMBER}</li>
                <li>MERCURIAL_REVISION_BRANCH: ${MERCURIAL_REVISION_BRANCH}</li>
                <li>MERCURIAL_REPOSITORY_URL: ${MERCURIAL_REPOSITORY_URL}</li>
                <li>SVN_REVISION: ${SVN_REVISION}</li>
                <li>SVN_URL: ${SVN_URL}</li>
            </ul>
        </td>
    </tr>
    <tr>
        <td>
            <h4><font color="#0B610B">失败用例</font></h4>
            ${FAILED_TESTS}<br/>
        </td>
    </tr>
    <tr>
        <td>
            <h4><font color="#0B610B">最近提交(#$GIT_REVISION)</font></h4>
            <!--
            <ul>
                ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat="
                <li>%d [%a] %m</li>
                "}
            </ul>
            <-->
        </td>
    </tr>
    <tr>
        <td>
            <b><font color="#0B610B">变更信息:</font></b>
            <hr size="2" width="100%" align="center"/>
        </td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>
                    上次构建成功后变化 : ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"}
                </li>
            </ul>
        </td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>上次构建不稳定后变化 : ${CHANGES_SINCE_LAST_UNSTABLE, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"}
                </li>
            </ul>
        </td>
    <tr>
        <td>
            <ul>
                <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
            </ul>
        </td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>变更集:${JELLY_SCRIPT,template="html"}</li>
            </ul>
        </td>
    </tr>
    <tr>
        <td><b><font color="#0B610B">构建日志 (最后 200行):</font></b>
            <hr size="2" width="100%" align="center"/>
        </td>
    </tr>
    <tr>
        <td><textarea cols="120" rows="30" readonly="readonly"
                      style="font-family: Courier New">${BUILD_LOG, maxLines=200}</textarea>
        </td>
    </tr>
</table>
</body>
</html>

里面的KEY的含义可以在

展开看到

内容令牌参考
Arguments may be given for each token in the form name="value" for strings and in the form name=value for booleans and numbers. In string arguments, escape ", \, and line terminators (\n or \r\n) with a \, e.g. arg1="\"quoted\""; arg2="c:\\path"; and arg3="one\ntwo". The brackets may be omitted if there are no arguments.
Examples: $TOKEN, ${TOKEN}, ${TOKEN, count=100}, ${ENV, var="PATH"}
Project Tokens
${DEFAULT_SUBJECT}
This is the default email subject that is configured in Jenkinss system configuration page.
${DEFAULT_CONTENT}
This is the default email content that is configured in Jenkinss system configuration page.
${DEFAULT_PRESEND_SCRIPT}
This is the default pre-send script content that is configured in Jenkinss system configuration. This is the only token supported in the pre-send script entry field.
${DEFAULT_POSTSEND_SCRIPT}
This is the default post-send script content that is configured in Jenkinss system configuration. This is the only token supported in the post-send script entry field.
${PROJECT_DEFAULT_SUBJECT}
This is the default email subject for this project. The result of using this token in the advanced configuration is what is in the Default Subject field above. WARNING: Do not use this token in the Default Subject or Content fields. Doing this has an undefined result.
${PROJECT_DEFAULT_CONTENT}
This is the default email content for this project. The result of using this token in the advanced configuration is what is in the Default Content field above. WARNING: Do not use this token in the Default Subject or Content fields. Doing this has an undefined result.
Extended Email Publisher Specific Tokens
${JELLY_SCRIPT,template="TEMPLATE_NAME"}
Custom message content generated from a Jelly script template. There are two templates provided: "html" and "text". Custom Jelly templates should be placed in <tt>$JENKINS_HOME/email-templates</tt>. When using custom templates, the template filename without ".jelly" should be used for the "template" argument. You may also use the Config File Provider plugin to manage your templates. Prefix the managed file NAME with "managed:" for the template parameter. (Example: <tt>${JELLY_SCRIPT, template="managed:ManagedFileName"}</tt>)
${SCRIPT}
Custom message content generated from a Groovy script. Custom scripts should be placed in <tt>$JENKINS_HOME/email-templates</tt>. When using a custom script the plugin will look in the resources forthe email-ext plugin first, and then in the <tt>$JENKINS_HOME/email-templates</tt>directory. No other directories will be searched. You may also use the Config File Provider plugin to manage your scripts. Prefix the managed file NAME with <tt>managed:</tt> for the template or script parameter. (Example: <tt>${SCRIPT, template="managed:ManagedFileName"}</tt>)
script
When this is used, only the last value in the script will be used in the expansion (script and template can not be used together).
template
The template in Groovy's SimpleTemplateEngine format.
${TEMPLATE}
Custom message content generated from a template file. Custom templates should be placed in <tt>$JENKINS_HOME/email-templates</tt>. When using a custom script the plugin will look in the resources forthe email-ext plugin first, and then in the <tt>$JENKINS_HOME/email-templates</tt>directory. No other directories will be searched. You may also use the Config File Provider plugin to manage your templates. Use the "Custom File" config type for the template. Prefix the managed file NAME with "managed:" for the file parameter. (Example: <tt>${TEMPLATE, file="managed:ManagedFileName"}</tt>)
file
The template in plain text format.
Token Macro Plugin Tokens
${FAILED_TESTS}
Displays failing unit test information, if any tests failed.
showStack
Shows stack trace in failing test output. Defaults to true.
showMessage
Shows error message in failing test output. Defaults to true.
maxTests
Display at most this many tests. No limit is set by default.
onlyRegressions
Display only the failing tests that are different from previous builds. Defaults to false.
escapeHtml
If set to true escapes characters in errors details and stack traces using HTML entities. Defaults to false.
${TEST_COUNTS,var="TYPE"}
Displays the number of tests based on the type (var) passed in (total, pass, fail, skip). Defaults to total.
${GIT_BRANCH}
Expands to the name of the branch that was built.
Parameters
all
If specified, all the branches that point to the given commit is listed. By default, the token expands to just one of them.
fullName
If specified, this token expands to the full branch name, such as 'origin/master'. Otherwise, it only expands to the short name, such as 'master'.
${GIT_REVISION}
Expands to the Git SHA1 commit ID that points to the commit that was built.
Parameters
length=N (optional, default to 40)
Specify the commit ID length. Full SHA1 commit ID is 40 character long, but it is common to cut it off at 8 or 12 as that often provide enough uniqueness and is a lot more legible.
${ADMIN_EMAIL}
Displays the email address for the Jenkins administrator
${BUILD_CAUSE} ${CAUSE}
Displays the cause of the build.
${BUILD_LOG_EXCERPT}
Displays an excerpt from the build log.
start
Regular expression to match the excerpt starting line (matching line is excluded).
end
Regular expression to match the excerpt ending line (matching line is excluded).
See java.util.regex.Pattern
${BUILD_LOG}
Displays the end of the build log.
maxLines
Display at most this many lines of the log. Defaults to 250.
escapeHtml
If true, HTML is escape. Defaults to false.
${BUILD_LOG_MULTILINE_REGEX}
Displays build log segments that match the regular expression.
regex
java.util.regex.Pattern
Segments of the build log that match this regular expression are included. See also null. No default. Required parameter
maxMatches
The maximum number of matches to include. If 0, all matches will be included. Defaults to 0.
showTruncatedLines
If true, include [...truncated ### lines...] lines. Defaults to true.
substText
If non-null, insert this text into the email rather than the entire segment. Defaults to null.
escapeHtml
If true, escape HTML. Defaults to false.
matchedSegmentHtmlStyle
If non-null, output HTML. Matched lines will become <b style="your-style-value">html escaped matched lines</b>. Defaults to null.
${BUILD_LOG_REGEX}
Displays lines from the build log that match the regular expression.
regex
Lines that match this regular expression are included. See also java.util.regex.Pattern.Defaults to "(?i)\\b(error|exception|fatal|fail(ed|ure)|un(defined|resolved))\\b"
linesBefore
The number of lines to include before the matching line. Lines that overlap with another match or linesAfter are only inlcuded once. Defaults to 0.
linesAfter
The number of lines to include after the matching line. Lines that overlap with another match or linesBefore are only included once. Defaults to 0.
maxMatches
The maximum number of matches to include from the head of the log. If 0, all matches will be included. Defaults to 0.
maxTailMatches
The maximum number of matches to include from the tail of the log. When combined with maxMatches, it further limits the matches to the tail end of matched results. If 0, all matches will be included. Defaults to 0.
showTruncatedLines
If true, include [...truncated ### lines...] lines. Defaults to true.
substText
If non-null, insert this text into the email rather than the entire line. Defaults to null.
escapeHtml
If true, escape HTML. Defauts to false.
matchedLineHtmlStyle
If non-null, output HTML. Matched lines will become <b style="your-style-value"> html escaped matched line</b>. Defaults to null.
addNewline
If true, adds a newline after subsText. Defaults to true.
defaultValue
This value will be used if nothing is replaced.
greedy
When false and maxMatches is non-zero it causes more conservative addition of results when used with other parameters such as linesBefore and linesAfter
${BUILD_NUMBER}
Expands to the current build number, a sequential auto-incrementing unique number that identifies the build, such as "125"
${BUILD_STATUS}
Displays the status of the current build. (failing, success, etc...)
${BUILD_URL}
Displays the URL for the current build
${CHANGES_SINCE_LAST_BUILD} ${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, modified 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"
regex
A regular expression.
replace
A replacement for all sub-strings of the change message that match the given regular expression.
default
Message to use when no changes are detected. Defaults to "No changes\n"
${CHANGES_SINCE_LAST_SUCCESS}
Displays the changes since the last successful build.
reverse
If true, show most recent builds at the top instead of the bottom. Defaults to false.
format
For each build listed, a string containing %X, where %X is one of
%c
changes
%n
build number
Defaults to Changes for Build #%n\n%c\n
changesFormat
For each change in a build. See ${CHANGES_SINCE_LAST_BUILD} for placeholders.
Following Parameters are also supported: showPaths, pathFormat, showDependencies, dateFormat, regex, replace, default. See ${CHANGES_SINCE_LAST_BUILD} details.
${CHANGES_SINCE_LAST_UNSTABLE}
Expands to the changes since the last unstable or successful build.
reverse
If true, show most recent builds at the top instead of the bottom. Defaults to false.
format
For each build listed, a string containing %X, where %X is one of
%c
changes
%n
build number
Defaults to Changes for Build #%n\n%c\n
changesFormat
For each change in a build. See ${CHANGES_SINCE_LAST_BUILD} for placeholders.
Following Parameters are also supported: showPaths, pathFormat, showDependencies, dateFormat, regex, replace, default. See ${CHANGES_SINCE_LAST_BUILD} details.
${ENV,var="VARIABLENAME"}
Expands to an environment variable (specified here as VARIABLENAME) from the build environment. Note that this does not include any variables set by the build scripts themselves, only those set by Jenkins and other plugins.
${JENKINS_URL}
Displays the URL to the Jenkins server. (You can change this on the system configuration page.)
${JOB_DESCRIPTION}
Displays the description of the job.
${LOG_REGEX}
Uses a regular expression to find a single log entry and generates a new output using the capture groups from it. This is partially based on the description-setter plugin (https://github.com/jenkinsci/description-setter-plugin).
${PROJECT_NAME}
Displays the project's full name. (See AbstractProject.getFullDisplayName)
${PROJECT_DISPLAY_NAME}
Displays the project's display name. (See AbstractProject.getDisplayName)
${PROJECT_URL}
Displays a URL to the project's page.
${PROPFILE,file="FILENAME",property="PROPERTYNAME"}
Expands to the value of a property in a property file. The filename is relative to the build workspace root.
${FILE,path="PATH"}
Expands to the contents of a file. The file path is relative to the build workspace root.
${XML,file="FILE",xpath="XPATH"}
Expands to the result(s) of an XPath expression run against the given XML file.
If the XPath evaluates to more than one value, then a semicolon-separated string is returned.
The file path is relative to the build workspace root.
In addition to the tokens, you can modify the result of the token expansion using parameter expansions. They follow similar rules as bash Parameter Expansions. The supported expansions are: ${#TOKEN} which resolves to the length of the expanded token value, ${TOKEN:offset:length} which takes a substring of the token result (length is optional and offset and length can both be negative), ${TOKEN#pattern} which matches the pattern against the start of the expanded token and removes it if it, ${TOKEN%pattern} which matches the pattern against the end of the expanded token and removes it it if matches.

然后我们根据上面的字典继续来编写脚本

先去生成脚本

post {
  always {
    // One or more steps need to be included within each condition's block.
  }
}

然后是脚本(内容意义可以在上面找到)

emailext body: '${FILE,path="email.html"}', subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!', to: '1441470436@qq.com'

生成片段

pipeline {
    agent any
    stages {
        stage('pull') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/${branches}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '83039753-8e29-49c9-8e1a-f2c33c4cb847', url: 'git@121.89.163.191:root/rubenweicowbeer.git']]])
            }
        }
        stage('build') {
            steps {
                sh label: '', script: 'mvn clean package'
            }
        }
        stage('publish') {
            steps {
                deploy adapters: [tomcat8(credentialsId: '15efa4d4-9b32-4c8d-a0b6-2aa89b72191f', path: '', url: 'http://121.89.163.191:8080')], contextPath: null, war: 'target/*.war'
            }
        }
    }
    post {
        always {
            emailext(
                body: '${FILE,path="email.html"}',
                subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!', 
                to: '1441470436@qq.com,achao1441470436@gmail.com'
            )
        }
    }
}

放入项目

更改类型

勾选Allow sending to unregistered users

push后点击构建

顺带一提,可以安装一个workspace cleanup plugin插件

然后在构建后操作里勾选,可以清理缓存

收到邮件后就是这样啦

相关文章
|
6月前
|
jenkins 持续交付
Jenkins Pipeline 参数化构建
Jenkins Pipeline 参数化构建
246 0
|
3天前
|
安全 jenkins 持续交付
如何在 Jenkins 中配置邮件通知?
如何在 Jenkins 中配置邮件通知?
|
24天前
|
运维 监控 jenkins
运维自动化实战:利用Jenkins构建高效CI/CD流程
【10月更文挑战第18天】运维自动化实战:利用Jenkins构建高效CI/CD流程
|
6月前
|
jenkins Devops 机器人
【DevOps】(五)Jenkins构建给企业微信推送消息
【DevOps】(五)Jenkins构建给企业微信推送消息
267 1
|
3月前
|
Prometheus 监控 Cloud Native
使用 Jenkins 监控和优化构建性能
【8月更文第31天】在软件开发的过程中,构建性能直接影响着开发效率和团队的生产力。一个快速、可靠的构建流程可以显著加快迭代速度,减少等待时间,使团队能够更快地响应变化。Jenkins 作为一款广泛使用的持续集成/持续交付(CI/CD)工具,提供了丰富的功能来帮助开发者监控和优化构建性能。本文将探讨如何利用 Jenkins 的内置工具和外部工具来监控构建性能,并提出一些具体的优化方案。
288 0
|
3月前
|
jenkins 持续交付
Jenkins Pipeline 流水线 - Parameters 参数化构建
Jenkins Pipeline 流水线 - Parameters 参数化构建
114 0
|
3月前
|
jenkins 持续交付 网络安全
Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
65 0
|
5月前
|
敏捷开发 jenkins 测试技术
阿里云云效产品使用问题之如何进行类似于jenkins那样参数化构建
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
6月前
|
存储 jenkins 持续交付
Jenkins 构建环境指南
Delete workspace before build starts(常用) 在每次构建开始前清空工作空间。这确保了每次构建开始时都是在一个干净的环境下进行,避免了旧的文件或数据对新构建的影响。特别是对于需要频繁构建的项目,保持工作空间的干净能够确保构建过程的可靠性。 Use secret text(s) or file(s) (常用) 允许你在构建过程中使用加密的文本或文件作为凭证。这对于需要访问受保护资源的构建任务非常有用,可以安全地存储和使用密码、API 密钥等敏感信息,而不用担心泄漏。
362 2
|
5月前
|
Kubernetes Cloud Native jenkins
云原生时代:从Jenkins到Argo Workflows,构建高效CI Pipeline
基于Argo Workflows可以构建大规模、高效率、低成本的CI流水线