Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jenkins 邮件发送「可修改模版」 #110

Open
reng99 opened this issue Feb 10, 2022 · 0 comments
Open

Jenkins 邮件发送「可修改模版」 #110

reng99 opened this issue Feb 10, 2022 · 0 comments
Labels
blog a single blog jenkins

Comments

@reng99
Copy link
Owner

reng99 commented Feb 10, 2022

上一篇,Jenkins Pipeline 结合 Gitlab 实现 Node 项目自动构建 我们已经实现了自动构建的功能。

在团队合作中,项目在构建异常「或成功」时候,需要邮件通知相关的人员,这就涉及到 Jenkins 邮件发送的功能了。

安装 Email Extension

Email Extension 插件允许你配置邮件通知。通过此插件,你可以自定义邮件的发送人,接受者和发送的内容。

Manage Jenkins -> Manage Plugins -> Available 。搜索 Email Extension 安装

install_email_extension.png

配置 Email Extension 参数

Manage Jenkins -> Configure System -> Extended E-mail Notification

extended_email_parameter.png

第 1 点: Simple Mail Transfer Protocol (SMTP) 自己申请,如果不知道如何操作,请网上找答案。(应该也许不难吧...)

fear.jpeg

第 2 点: 邮件默认的接收邮件地址。

构建脚本

使用Jenkins Pipeline 结合 Gitlab 实现 Node 项目自动构建Jenkinsfile脚本补充下。

pipeline {
    agent any
    
    tools { 
        nodejs "nodejs" 
    }
    
    stages {
        stage('Dependency') {
            steps {
                sh 'npm install'
            }
        }
        stage('Build') { 
            steps {
                sh 'npm run clean' 
                sh 'npm run build' 
            }
        }
    }
    
    // 添加的脚本内容
    post {
        always {
            emailext body: '${DEFAULT_CONTENT}',
                subject: '${DEFAULT_SUBJECT}',
                to: '${DEFAULT_RECIPIENTS}',
                from: '${env.DEFAULT_FROM_EMAIL}'
        }
    }
}

这就很明显了:默认发送邮箱默认接收邮箱默认的发送主题默认的发送内容。都是获取我们上一步设定的值。效果如下:

构建失败:

build_failure.png

构建成功:

build_success.png

对比上面的两张图,构建失败和构建成功,的构建结果字体竟然是不同的颜色,神奇...

如果你看上一节配置 Email Extension 参数中的截图,你可能会留意到:

<h2><font color="${BUILD_STATUS}">构建结果 - ${BUILD_STATUS}</font></h2>

color="${BUILD_STATUS}" 嗯,加上这个,不同状态返回的构建结果字体不同颜色了?我们带动下构建pipelinealways -> changed。触发构建信息由失败 -> 成功,得到下面的结果:

build_failure_to_success.png

看来是想多了啊,并不是不同的状态不同颜色啊。不过,如果你不介意这个bug,可以使用...

尴尬.jpeg

升级下需求

发送邮件需要根据不同状态,使用特定的颜色区分。比如:成功 -> #27AE60 , 失败 -> #E74C3C , 其他颜色 -> #F4E242

咦,这还不简单?在默认的发送模版上添加判断条件就行了。

然而,不支持,太惨了,得另辟蹊径~(大雾)

惨猫.jpeg

经过一番搜索,Groovy Template 能够解决这个问题。

我们更改下 pipeline 脚本:

# ...
post {
    always {
        emailext body: '''${SCRIPT, template="my-email-template"}''',
            subject: '${DEFAULT_SUBJECT}',
            to: '${DEFAULT_RECIPIENTS}',
            from: "${env.DEFAULT_FROM_EMAIL}"
    }
}
# ...

我们构建下。构建是成功了,但是邮箱收到的却是:

first_time_groovy.png

Groovy Template file [my-email-template] was not found in $JENKINS_HOME/email-templates.

邮件提示,机器中 $JENKINS_HOME/email-templates 下并不存在 my-email-template 模版。(笑哭)

没有权限怎么办呢?咱可以通过其他方法不?

再查找资料,咦,有了 - Email Extension Plugin is not loading groovy template added via Config File Provider Plugin。通过配置文件解决,我们来见证下:

步骤 1 :jenkins 平台上操作,Manage Jenkins -> Managed Files

步骤 2 : 点击 Add a new Config

步骤 3 : 点选类型 Extended Email Publisher Groovy Template, 点击 Submit 按钮提交

步骤 4 : 输入名称和相关的内容。请记住名称,后续使用到。

configuration_file.png

因为截图篇幅问题,这里的 Content 内容并没有截取完整,完整的内容请戳Groovy_template.html,其中的关键代码如下:

  .tr-title {
    background-color: <%= (build.result == null || build.result.toString() == 'SUCCESS') ? '#27AE60' : build.result.toString() == 'FAILURE' ? '#E74C3C' : '#F4E242' %>;
  }

Groovy 官方完整模版请见官方模版Groovy Email Template

扎心了啊,老铁(尬笑)

步骤 5 : 修正你的 pipeline 文件,请确保你输入和正确的名字"managed:Groovy Email Template"

# ...
post {
    changed {
        emailext body: '''${SCRIPT, template="managed:Groovy Email Template"}''',
            subject: '${DEFAULT_SUBJECT}',
            to: '${DEFAULT_RECIPIENTS}',
            from: "${env.DEFAULT_FROM_EMAIL}"
    }
}
# ...

一切就绪,我们重新构建。结果如下:

构建状态 邮箱收到内容截图
成功 build_color_success.png
失败 build_color_failure.png
中止 build_color_abort.png

嗯~效果不错(撒花🎉🎉🎉)完结

读者有啥其他的方法呢,可以留言交流下。更多的内容,请戳Jimmy Github Blog

@reng99 reng99 added blog a single blog jenkins labels Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blog a single blog jenkins
Projects
None yet
Development

No branches or pull requests

1 participant