Jenkins中配置邮件通知实例演示

前言:本文通过安装配置Jenkins实现邮件通知,告知一个C# Git Repo的build成功与否

一、预配条件

  1. 在windows上安装Jenkins和它推荐安装的Plugins
  2. 创建一个@163.com邮箱账号,用来发送邮件
  3. 准备一个测试用的git repo,此处我提供了 https://github.com/wenboyang214/jenkinsCsharpTest.git
  4. 运行下列脚本,Jenkins需要设置git、msbuild等的环境变量,请自行设置

二、对邮箱账号本身进行设置(此处以163邮箱为例)

Note: Jenkins中配置的邮箱secret需要授权码,而不是密码本身。因此我们需要获取授权码。

1081775-20181023125655798-1531628539.png

1081775-20181023125737348-1070500028.png

1081775-20181023125744044-489159832.png

1081775-20181023125747792-2035430739.png

三、配置Jenkins 的系统参数

1081775-20181023125841685-1732964156.png

你需要做如下截图的配置

1.最好更改一下Jenkins的Workspace Root Directory,避免符号的一些错误

1081775-20181023125851796-289556992.png

2.设置Jenkins的System Admin e-mail address,此处的邮箱要和你email notification的邮箱一致

1081775-20181023125856245-1862380468.png

3.在Extended E-mail Notification中进行如下设置,没设置的地方,保持默认

1081775-20181023125901445-903887164.png

四、测试邮箱提醒功能

1.简单创建一个新的Pipeline item 名称为”test”
2.将下列脚本copy到Pipeline script中
#!/usr/bin/env groovy Jenkinsfile
pipeline {
    agent any
    environment {
        def gitRepo="https://github.com/wenboyang214/jenkinsCsharpTest.git"
        def mailRecipients="wenbya@163.com"
    }
    stages {
        stage('1. check the git repo'){
        steps{
                git "${gitRepo}" 
            }
         }
        stage('3. msbuild the project'){
            steps{
                bat "msbuild .\\JenkinsTest\\JenkinsTest.sln"
            }
        }
    }
    post {
        always {
            echo 'testing email notification'
        }
        success {
            echo 'This will run only if successful'
            emailext( 
                body: '''${SCRIPT, template="groovy-html.template"}''',
                mimeType: 'text/html',
                subject: "[Jenkins]${currentBuild.fullDisplayName}",
                to: "${mailRecipients}",
                replyTo: "${mailRecipients}",
                recipientProviders: [[$class: 'CulpritsRecipientProvider'],[$class: 'DevelopersRecipientProvider']]
            )
        }
        failure {
            echo 'This will run only if failed'
            emailext( body: '''${SCRIPT, template="groovy-html.template"}''',
                mimeType: 'text/html',
                subject: "[Jenkins]${currentBuild.fullDisplayName}",
                to: "${mailRecipients}",
                replyTo: "${mailRecipients}",
                recipientProviders: [[$class: 'CulpritsRecipientProvider'],[$class: 'DevelopersRecipientProvider']]
            )
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

然后直接启动这个test item

五、进行对错改动,并检查邮箱是否收到邮件

1081775-20181023125909501-965054830.png

更改msbuild的路径,故意让其build出错,检查是否能收到报错邮件。

bat “msbuild .\JenkinsTest\JenkinsTest.sln” => bat “msbuild .\JenkinsTest1\JenkinsTest.sln” 

1081775-20181023125913900-807937308.png

转载于:https://www.cnblogs.com/yangwenbo214/p/9836021.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值