持续集成 jenkins pipeline 邮件包含 git commit 信息

jon · 2018年10月09日 · 2890 次阅读

背景

vue 前端项目每次 RD 编译差不多 1 分钟,导致时间浪费。当前编译的版本修复了哪些功能还要通知 QA;
能否通过 jenkins 解决此问题?

解决方案

pipeline 模式

jenkins2.0 开始,提供了 pipeline 模式。个人认为 pipeline 模式的优点:

1、pipeline 定义多个步骤,日志比较清晰;

2、pipeline 脚本,可以编写比较复杂的构建脚本;

3、pipeline libary 共享库,可将 pipeline 脚本 push 到 gitlab 中,统一维护。用户只需要调用其方法,不需要关心具体的实现;

libary 配置:jenkins 配置共享的 pipeline 脚本

脚本调用:job 中直接调用 pipeline 脚本方法

获取 git commit 信息

currentBuild.changeSets 变量中存储了 git 的 commit 的信息,通过解析该参数能获取到每次 commit 的用户以及 commit 信息;具体可查看代码中的 getChangeString 函数(此段代码 copy 自网络)

脚本说明

脚本主要内容:

  1. 代码拉取
    1. checkout developer 分支,当分支有变化时触发构建;
  2. vue 依赖包下载
    1. npm install 安装依赖;
    2. 若使用了 sass,需要重新 rebuild node-sass;
    3. 如果 install、rebuild 日志中包含 ERROR 时,认为当前步骤失败,并退出构建;
  3. 打包项目
    1. npm run build,日志中包含 ERROR 时,认为当前步骤失败,并退出构建;
  4. 部署测试环境
    1. 直接通过 scp 进行版本拷贝;
  5. 发送邮件
    1. 获取 commit 提交人,并且发送邮件;

注意事项:

npm install 后,不要使用 cnpm build
npm install/build ,失败,此时该命令执行仍然是成功的,可对 install、build 日志进行判断当前步骤是否失败;

(以上内容纯属个人理解,如有问题方便指正)

node {
        stage('GitCheckout'){
            script{
                checkout([$class: 'GitSCM', branches: [[name: '*/developer']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CheckoutOption']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'demo.jin', url: 'http://git.demo.com/qa_dev/demo.git']]])
            }

        }
        stage('Build') { 
            script{
                   def installStatus = sh (returnStatus: true, script: 'cnpm install || exit 1')
                   sh 'cnpm rebuild node-sass'
                   if(installStatus !=0){
                     echo 'npm install 异常,请检查日志'
                     sendEmail("构建npm install失败")
                        sh 'exit 1'
                   }
                    def buildLog = sh (returnStdout: true, script: 'cnpm run build || echo "ERROR"')
                    echo 'build fineshed '+buildLog
                    if(buildLog.contains('ERROR')){
                        echo 'npm build异常,请检查日志'
                        sendEmail("构建npm build失败")
                        sh 'exit 1'
                    }
                }
        }
        stage('Deploy') { 
            script{
                def scpStatus = sh (returnStatus: true, script: 'scp -r -P 22 ./dist/* root@192.168.231.100:/home/work/matrix-web ')
                if(scpStatus != 0){
                    echo 'scp 失败'
                    sendEmail("构建scp失败")

                        sh 'exit 1'
                }
                sendEmail("部署成功")
            }
        }

}

@NonCPS
def getChangeString() {
 MAX_MSG_LEN = 100
 def changeString = ""
 def sendMail="liukang.deme@demo.com,demo.jin@demo.com,"
 echo "Gathering SCM changes"
 def changeLogSets = currentBuild.changeSets
 for (int i = 0; i < changeLogSets.size(); i++) {
 def entries = changeLogSets[i].items
 for (int j = 0; j < entries.length; j++) {
 def entry = entries[j]
 truncated_msg = entry.msg.take(MAX_MSG_LEN)
 sendMail = sendMail+"${entry.author}@demo.com,"
 changeString += "--${truncated_msg}  [${entry.author}]\n"
 }
 }

 if (!changeString) {
 changeString = " - 无"
 }
 return [sendMail,changeString]
}

def sendEmail(status) {
 def sendObject = getChangeString()
 echo "${sendObject[0]}" 
 echo "${sendObject[1]}"

 emailext attachLog: true, body: "更新记录:\n " + "${sendObject[1]}" + "\n\n 构建日志: $BUILD_URL/console" + "\n",
 subject: "${JOB_NAME}  (${BUILD_NUMBER})-"+status,to: "${sendObject[0]}"
}

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册