1.邮件形式
1)引入gradle
2)在application 配置文件中添加配置信息
配置发邮件的服务器,此处使用qq邮箱发邮件
username:qq邮箱地址
password:为邮箱的授权码,不是qq密码
subprojects {
group = "com.tencent.bk.devops.ci.notify"
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
api("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.springframework.boot:spring-boot-starter-mail")
testImplementation("junit:junit")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
}
3)EmailServiceImpl 中加入自己的实现
kotlin语言:
private fun sendMsg(emailNotifyPost: EmailNotifyMessageWithOperation) {
val receivers = emailNotifyPost.getReceivers()
val message = SimpleMailMessage()
message.from = "1214000999@qq.com"
message.setTo(*receivers?.toTypedArray())
message.subject = emailNotifyPost.title
message.text = emailNotifyPost.body
val ccs = emailNotifyPost.getCc()
message.setCc(*ccs?.toTypedArray())
val bccs = emailNotifyPost.getBcc()
message.setBcc(*bccs?.toTypedArray())
try {
javaMailSender!!.send(message)
logger.info(this::class.toString() + "sendMsg 构建结果消息发送!")
} catch (e: Exception) {
logger.error(this::class.toString() + ".sendMsg 构建结果消息发送失败: " + e.printStackTrace())
}
}
4)BuildEndControl.finish() 构建完后的回调事件中加入调用邮件通知代码,可以通过rabbitmq 或者client发送http请求
2.企业微信发送消息
1)创建企业微信
2)配置apllication配置文件 分布式服务配置 notify项目的 ,单体配置 assembly项目的
wework: corpId: 企业id corpSecret: 秘钥 agentId: 应用id tempDirectory: # 保存待上传 wework 文件临时存储路径 apiUrl: https://qyapi.weixin.qq.com # 企业微信服务端host,用于调用企业微信api接口 safe: 0 # 表示是否是保密消息,0表示可对外分享,1表示不能分享且内容显示水印,默认为0 enableDuplicateCheck: 0 # 表示是否开启重复消息检查,0表示否,1表示是,默认0 duplicateCheckInterval: # 表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时 enableIdTrans: 0 # 表示是否开启id转译,0表示否,1表示是,默认0。仅第三方应用需要用到,企业自建应用可以忽略。
corpid:
agentId,corpSecret(点击查看):
3)企业微信 蓝鲸1.5已实现所以只需调用即可
在BuildEndControl.finnish() 中编写调用代码
var receivers:String
var receiverType=WeworkReceiverType.single
var textType= WeworkTextType.text
var message :String
var notifyMsg= pipelineSettingService.getSetting(pipelineId)!!
//获取构建成功通知模板
if (!buildStatus.isCancel() && !buildStatus.isFailure()) {
// receivers=notifyMsg?.successReceiver;
message=notifyMsg?.successContent;
}else{
// receivers=notifyMsg?.failReceiver;
message=notifyMsg?.failContent;
}
// pipelineRepositoryService.getPipelineInfo(projectId, pipelineId, buildInfo.channelCode)
val map: MutableMap = HashMap()
map["BK_CI_BUILD_NUM"] = buildInfo.buildNum?.toString()
map["BK_CI_BUILD_TOTAL_TIME"] = (buildInfo.endTime?:0-(buildInfo.startTime?:0)).toString()
map["BK_CI_PROJECT_NAME_CN"] = "xm"
map["BK_CI_PIPELINE_NAME"] = "12"
map["BK_CI_START_USER_NAME"] = buildInfo.startUser
val strSubstitute = StrSubstitutor(map)
message=strSubstitute.replace(message)
try {
val result =client.get(BuildNotifyResource::class).sendWeworkTextNotify(receivers?:"",receiverType,textType,message)
if (result.isOk() && result.data != null) {
LOG.info("SENT MSG TO WEWORD SUCCESS! ")
}
}catch (e:Exception){
LOG.error("${this::class.java.toString()} :SENT MSG TO WEWORD ERROR: ${e.localizedMessage}")
}



