在CI系统中使用nodejs发送电子邮件

版权声明:本文为迟思堂主人李迟原创文章,版权所有。可随便任意使用(包括学习研究商用),但由此带来的成果或后果,概与作者无关。胡乱修改的,不注明出处的,概不负责。 https://blog.csdn.net/subfate/article/details/86491111

上文已经完成nodejs语言发送邮件功能,本文将其扩展一下,适用于提交代码自动发送邮件通知的场合,后续将测试不同的git仓库的使用情况。

实现

有了前文的基础,下面的代码就容易看懂了。值得注意的是,源码文件名为.gitemail.js,主要是为了与其它文件作区别。
使用方法很简单,安装依赖包,再运行:

npm i nodemailer
node .gitemail.js 收件人 仓库名称 仓库地址

源码如下:

/*
.gitemail文件

npm i nodemailer
使用:
node .gitemail文件.js 收件人,仓库名称,仓库地址
示例:
node .gitemail文件.js "[email protected], [email protected]" "自动化CI测试" https://github.com/latelee/testci
*/
const nodemailer  = require("nodemailer");

// 参数:发件人,收件人,主题,正文(支持html格式)
function sendMail(from, aliasName, tos, subject, msg)
{
    const smtpTransport = nodemailer.createTransport({
    host: 'smtp.exmail.qq.com',
    secureConnection: true, // use SSL
    secure: true,
    port: 465,
    auth: {
        user: from,
        pass: '1qaz@WSX',
    }
    });

    smtpTransport.sendMail({
        //from    : '标题别名 <[email protected]>',
        from    : aliasName + ' ' + '<' + from + '>',
        //'[email protected], [email protected]',//收件人邮箱,多个邮箱地址间用英文逗号隔开
        to      : tos,
        subject : subject,//邮件主题
        //text    : msg,
        html    : msg
    }, function(err, res) {
        if (err)
        {
            console.log('error: ', err);
        }
    });
}

function nl2br(str, isXhtml) {
    var breakTag = (isXhtml || typeof isXhtml === 'undefined') ? '<br />' : '<br>';
    var str = (str + '').replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
};

function main()
{
    var args = process.argv.splice(2); // 似乎是从第2个参数开始
    //console.log(args[0], args[1], args[2]);
    //return;

    require('child_process').exec('git log -1 --stat', function(err, stdout) {
        console.log('Last commit message on this branch is:\n', stdout);
        stdout = nl2br(stdout)
        sendMail('[email protected]', 'CI自动邮件通知', args[0], 
            args[1] + '仓库代码更新',
            '<h2>' + args[1] + '仓库代码有提交,请及时更新。</h2>地址为:<a href=\"' + args[1] + '\"target=\"_blank\">' + args[2] + '</a></br>' + '<h3>commit log:</h3>' + stdout);
    });
}

// call main
main();

使用

免费CI系统将在后续文章中讲述,这里先给出配置文件示例。

github+travis

配置文件.travis.yml内容如下:

language: node_js
node_js: stable
cache:
    apt: true
    directories:
        - node_modules
script:
    - npm install nodemailer
    - pwd
    - ls -a
    - echo "running mail.js"
    - node .gitemail.js "[email protected], [email protected]" "自动化CI测试" https://github.com/latelee/testci

gitlab

配置文件.gitlab-ci.yml内容如下:

send_job:
  script:
    - apt-get update -qq && apt-get install -y -qq software-properties-common git
    - curl -sL https://deb.nodesource.com/setup_10.x | bash -
    - apt-get update -qq && apt-get install -y -qq nodejs
    - npm install nodemailer
    - node .gitemail.js "[email protected], [email protected]" "自动化CI测试" https://gitlab.com/latelee/testci

  only:
    - master

以github+travis为例,当提交代码后,大约1~3分钟,可以收到邮件通知,如图1所示:

图1 收到邮件

小结

本文所有源码码托管于github上,地址:https://github.com/latelee/testci。
欢迎使用。

李迟 2019.1.15 中午

猜你喜欢

转载自blog.csdn.net/subfate/article/details/86491111