Quantcast
Channel: 扛一肩记忆 » Jenkins-CI
Viewing all articles
Browse latest Browse all 17

使用Pre-send Script决定是否发送Email邮件通知

$
0
0

本文介绍的是使用插件的方式,利用脚本来决定是否需要发送邮件通知,如果不想使用插件,那么很简单,在Build Step中使用脚本实现吧!

因此你需要先安装 Email-ext 插件,Pre-send Script 是其内置的功能,使用的是 Groovy 脚本。

判断逻辑有很多,比如使用环境变量,预定义变量,参数变量等等,这里就介绍比较简单的:使用参数变量吧。

1. 定义一个参数(比如 boolean value 类型),如图

2. 在 Editable Email Notification -> Advanced -> Pre-send Script 中加入以下代码(视情况自己编写,我这里只是做了最简单的逻辑判断,另外,记得在 Add Trigger中加入相应的发送状态):

def env = build.getEnvironment();
String isSend = env['sendEmail'];
logger.println "sendEmail="+ isSend;
if ( isSend == null || isSend.equals('false')) {
    logger.println "cancel=true;";
    cancel=true;
}

如图:

3. 保存后 Build 一下试试,发送和不发送的log信息如下:

发送成功:

Email was triggered for: Success
Sending email for trigger: Success
Sending email to: xxx@yyy.com
sendEmail=true

发送失败:

Email was triggered for: Success
Sending email for trigger: Success
Sending email to: xxx@yyy.com
sendEmail=false
cancel=true;
Email sending was cancelled by user script.


Viewing all articles
Browse latest Browse all 17

Latest Images

Vimeo 10.7.0 by Vimeo.com, Inc.

Vimeo 10.7.0 by Vimeo.com, Inc.

HANGAD

HANGAD

MAKAKAALAM

MAKAKAALAM

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC

Vimeo 10.6.2 by Vimeo.com, Inc.

Vimeo 10.6.2 by Vimeo.com, Inc.

Vimeo 10.6.1 by Vimeo.com, Inc.

Vimeo 10.6.1 by Vimeo.com, Inc.



Latest Images

Vimeo 10.7.0 by Vimeo.com, Inc.

Vimeo 10.7.0 by Vimeo.com, Inc.

HANGAD

HANGAD

MAKAKAALAM

MAKAKAALAM

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC

Vimeo 10.6.1 by Vimeo.com, Inc.

Vimeo 10.6.1 by Vimeo.com, Inc.