Docker+Jenkins+Pipeline实现持续集成-模板

这里记录一些当前使用的pipeline模板和邮件模板

Java项目模板

// java项目

// 需要解析http返回结果时使用
import groovy.json.JsonSlurperClassic
import groovy.json.JsonOutput

// 按需求更改变量的值
node {
    // 参数设置
    def repoUrl = 'git@*****.git'
    def repoBranch = 'dev'
    def gitDir = ''
    def workspace = ''

    // docker镜像信息
    def imageName = "*****/demo"
    def imageTag = "dev"

    // rancher1.X部署所需变量
    def rancher1Url = 'https://<rancher1_service>/v2-beta'  // rancher1.X地址 
    def rancher1Env = ''  // rancher1.X需部署服务所在环境的ID
    def rancher1Service = '<stack>/<service>'   // rancher1.X需部署服务的 栈/服务名

    // rancher2.X部署所需变量
    def rancher2Url = "https://<rancher2_service>/v3/project/<cluster_id>:<project_id>" // rancher2.X地址+project
    def rancher2Namespace = ""  // rancher2.X需部署服务的命名空间
    def rancher2Service = "bookkeeper"  // rancher2.X需部署服务的服务名

    def recipients = ''    // 收件人
    def jenkinsHost = ''    // jenkins服务器地址

    // 认证ID
    def gitCredentialsId = 'aa651463-c335-4488-8ff0-b82b87e11c59'
    def settingsConfigId = '3ae4512e-8380-4044-9039-2b60631210fe'
    def rancherAPIKey = 'd41150be-4032-4a53-be12-3024c6eb4204'
    def soundsId = '69c344f1-8b11-47a1-a3b6-dfa423b94d78'

    // 工具配置
    def mvnHome = 'maven3.5.2'

    env.SONAR_HOME = "${tool 'sonarscanner'}"
    env.PATH="${env.SONAR_HOME}/bin:${env.PATH}"

    try {
        // 正常构建流程
        stage("Preparation"){
            // 拉取需要构建的代码
            git_maps = checkout scm: [$class: 'GitSCM', branches: [[name: "*/${repoBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${gitDir}"]], userRemoteConfigs: [[credentialsId: "${gitCredentialsId}", url: "${repoUrl}"]]]
            env.GIT_REVISION = git_maps.GIT_COMMIT[0..8]
        }
        try {
            stage('BackEndBuild') {
                // maven构建生成二进制包
                dir("${workspace}"){
                    withMaven(
                        maven: "${mvnHome}", 
                        mavenSettingsConfig: "${settingsConfigId}",
                        options: [artifactsPublisher(disabled: true)]) {
                        sh "mvn -U clean package -Dmaven.test.skip=true"
                    }
                }
            }
        }finally {
            stage('ArchiveJar') {
                // 获取二进制包产物
                archiveArtifacts allowEmptyArchive: true, artifacts: "${workspace}target/surefire-reports/TEST-*.xml"
                archiveArtifacts allowEmptyArchive: true, artifacts: "${workspace}target/*.jar"
            }
        }
        stage('CodeCheck'){
            // sonar_scanner进行代码静态检查
            withSonarQubeEnv('sonar') {
                sh """
                sonar-scanner -X -Dsonar.language=java \
                -Dsonar.projectKey=$JOB_NAME \
                -Dsonar.projectName=$JOB_NAME \
                -Dsonar.projectVersion=$GIT_REVISION \
                -Dsonar.sources=src/ \
                -Dsonar.sourceEncoding=UTF-8 \
                -Dsonar.java.binaries=target/ \
                -Dsonar.exclusions=src/test/**
                """
            }
        }
        stage("QualityGate") {
            // 获取sonar检查结果
            timeout(time: 1, unit: "HOURS") {
                def qg = waitForQualityGate()
                if (qg.status != 'OK') {
                    error "Pipeline aborted due to quality gate failure: ${qg.status}"
                }
            }
        }
        stage('DockerBuild'){
            // docker生成镜像并push到远程仓库中 
            sh """
                rm -f ${workspace}src/docker/*.jar
                cp ${workspace}target/*.jar ${workspace}src/docker/
            """
            dir("${workspace}src/docker/"){
                def image = docker.build("${imageName}:${imageTag}")
                image.push()
                sh "docker rmi ${imageName}:${imageTag}"
            }
        }
        stage('Rancher1Deploy'){
            // Rancher1.X上进行服务的更新部署
            rancher confirm: false, credentialId: "${rancherAPIKey}", endpoint: "${rancher1Url}", environmentId: "${rancher1Env}", environments: '', image: "${imageName}:${imageTag}", ports: '', service: "${rancher1Service}"
        }
        stage('Rancher2Deploy') {
            // Rancher2.X上更新容器
            // 获取服务信息
            def response = httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'GET', responseHandle: 'LEAVE_OPEN', timeout: 10, url: "${rancher2Url}/workloads/deployment:${rancher2Namespace}:${rancher2Service}"
            def serviceInfo = new JsonSlurperClassic().parseText(response.content)
            response.close()
            
            def dockerImage = imageName+":"+imageTag
            if (dockerImage.equals(serviceInfo.containers[0].image)) {
                // 如果镜像名未改变,直接删除原容器
                // 查询容器名称
                response = httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'GET', responseHandle: 'LEAVE_OPEN', timeout: 10, url: "${rancher2Url}/pods/?workloadId=deployment:${rancher2Namespace}:${rancher2Service}"
                def podsInfo = new JsonSlurperClassic().parseText(response.content)
                def containerName = podsInfo.data[0].name
                response.close()
                // 删除容器
                httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'DELETE', responseHandle: 'NONE', timeout: 10, url: "${rancher2Url}/pods/${rancher2Namespace}:${containerName}"
                
            } else {
                // 如果镜像名改变,使用新镜像名更新服务
                serviceInfo.containers[0].image = dockerImage
                def updateJson = new JsonOutput().toJson(serviceInfo)
                httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'PUT', requestBody: "${updateJson}", responseHandle: 'NONE', timeout: 10, url: "${rancher2Url}/workloads/deployment:${rancher2Namespace}:${rancher2Service}"
            }
        }

        // 构建成功:声音提示,邮件发送
        httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/4579.wav'
        emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
    } catch(err) {
        // 构建失败:声音提示,邮件发送
        currentBuild.result = 'FAILURE'
        httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/8923.wav'
        emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
    }
}

php+js的项目模板

与java不同的是,我们php+js的服务使用了3个容器,php+nginx+code的方式进行部署,每次只需要更新服务中的code容器即可;另外,为了防止php和js的构建污染jenkins服务,我们利用了官方的node镜像和一个自定义已安装好php构建环境的composer镜像,并使用docker.image('').inside{}代码块将构建过程放置到了docker容器当中。

// php+js项目

import groovy.json.JsonSlurperClassic
import groovy.json.JsonOutput

node {
    def repoUrl = 'git@******.git'
    def repoBranch = 'develop'
    def imageName = '***/code'
    def imageTag = 'dev'
    def buildEnv = 'develop'

    // rancher2.X部署所需变量
    def rancherUrl = 'https://<rancher2_service>/v3/project/<cluster_id>:<project_id>'
    def rancherNamespace = ''
    def rancherService = ''
    def recipients = ''
    def jenkinsHost = ''    // jenkins服务器地址

    // 认证ID
    def gitCredentialsId = 'aa651463-c335-4488-8ff0-b82b87e11c59'
    def settingsConfigId = '3ae4512e-8380-4044-9039-2b60631210fe'
    def rancherAPIKey = 'd41150be-4032-4a53-be12-3024c6eb4204'
    def soundsId = '69c344f1-8b11-47a1-a3b6-dfa423b94d78'
    
    env.SONAR_HOME = "${tool 'sonarscanner'}"
    env.PATH="${env.SONAR_HOME}/bin:${env.PATH}"
    
    try {
        stage("prepare") {
            // 拉取需要构建的代码
            git_maps = checkout scm: [$class: 'GitSCM', branches: [[name: "*/${repoBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], userRemoteConfigs: [[credentialsId: "${gitCredentialsId}", url: "${repoUrl}"]]]
            env.GIT_REVISION = git_maps.GIT_COMMIT[0..8]
        }
        stage('CodeCheck'){
            // sonar_scanner进行代码静态检查
            withSonarQubeEnv('sonar') {
                sh """
                sonar-scanner -X \
                -Dsonar.projectKey=$JOB_NAME \
                -Dsonar.projectName=$JOB_NAME \
                -Dsonar.projectVersion=$GIT_REVISION \
                -Dsonar.sourceEncoding=UTF-8 \
                -Dsonar.modules=php-module,javascript-module \
                -Dphp-module.sonar.projectName=PHP-Module \
                -Dphp-module.sonar.language=php \
                -Dphp-module.sonar.sources=. \
                -Dphp-module.sonar.projectBaseDir=backend/app \
                -Djavascript-module.sonar.projectName=JavaScript-Module \
                -Djavascript-module.sonar.language=js \
                -Djavascript-module.sonar.sources=. \
                -Djavascript-module.sonar.projectBaseDir=front/src
                """
            }
        }
        stage("QualityGate") {
            // 获取sonar检查结果
            timeout(time: 1, unit: "HOURS") {
                def qg = waitForQualityGate()
                if (qg.status != 'OK') {
                    error "Pipeline aborted due to quality gate failure: ${qg.status}"
                }
            }
        }
        stage("frontBuild") {
            // 前端构建
            docker.image("node").inside() {
                sh """
                cd front
                npm install
                npm run build ${buildEnv}
                """
            }
        }
        stage("back-build") {
            // 后端构建
            docker.image("<docker_registry>/composer:v0").inside() {
                sh """
                cd backend
                composer install
                """
            }
        }
        stage("docker-code-build") {
            // 代码镜像构建
            sh "cp -r front/build docker/code/build"
            sh "cp -r backend docker/code/api"
            dir("docker/code") {
                docker.build("${imageName}:${imageTag}").push()
                sh "docker rmi ${imageName}:${imageTag}"
            }
        }
        stage('rancherDeploy') {
            // Rancher上更新容器
            // 查询容器名称
            def response = httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'GET', responseHandle: 'LEAVE_OPEN', timeout: 10, url: "${rancherUrl}/pods/?workloadId=deployment:${rancherNamespace}:${rancherService}"
            def podsInfo = new JsonSlurperClassic().parseText(response.content)
            def containerName = podsInfo.data[0].name
            print(containerName)
            response.close()
        
            // 删除容器
            httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'DELETE', responseHandle: 'NONE', timeout: 10, url: "${rancherUrl}/pods/${rancherNamespace}:${containerName}"
        }
        stage("tear-down") {
            sh "rm -rf docker"
        }
        httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/4579.wav'
        emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
    } catch(err) {
        currentBuild.result = 'FAILURE'
        httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/8923.wav'
        emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
    }
}

Android项目模板

这里android项目只是进行了单元测试和构建过程,保证本次构建能够成功进行

// android项目

// 按需求更改变量的值

node {
    // 参数设置
    def repoUrl = 'git@*****.git'
    def repoBranch = 'develop'
    def gitDir = ''
    def workspace = 'Demo/'
    def version = ''
    def recipients = ''
    def jenkinsHost = ''    // jenkins服务器地址

    // 认证ID
    def gitCredentialsId = 'aa651463-c335-4488-8ff0-b82b87e11c59'
    def soundsId = '69c344f1-8b11-47a1-a3b6-dfa423b94d78'

    // 工具配置
    def gradleTool = './gradlew'
    // env.GRADLE_HOME = "${tool 'Gradle3.3'}"
    // env.PATH="${env.GRADLE_HOME}/bin:${env.PATH}"

    try {
        // 正常构建流程
        stage("Preparation"){
            // 拉取需要构建的代码
            checkout scm: [$class: 'GitSCM', branches: [[name: "*/${repoBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${gitDir}"]], userRemoteConfigs: [[credentialsId: "${gitCredentialsId}", url: "${repoUrl}"]]]
        }
        try {
            stage('UnitTest') {
                // 在清空前一次构建的结果后进行单元测试,并获取测试报告
                dir("${workspace}"){
                    sh "${gradleTool} clean"
                    sh "${gradleTool} testReleaseUnitTest --stacktrace"
                }
            }
            stage('Build') {
                // 使用Gradlew进行sdk的构建
                dir("${workspace}"){
                    sh "${gradleTool} makeJar"
                }
            }
        }
        finally {
            stage('Results'){
                // 获取单元测试报告
                sh """
                cd ${workspace}librasdk/build/reports/tests/
                tar -zcvf test_reports.tar.gz ./test*/
                """
                archiveArtifacts allowEmptyArchive: true, artifacts: "${workspace}librasdk/build/reports/tests/test_reports.tar.gz"
                // 获取sdk构建生成的jar包
                archive "${workspace}librasdk/build/libs/*/*.jar"
            }
        }
        httpRequest authentication:"${soundsId}", url:'http://10.38.162.13:8081/sounds/playSound?src=file:///var/jenkins_home/sounds/4579.wav'
        emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
    } catch(err) {
        // 构建失败
        currentBuild.result = 'FAILURE'
        httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/8923.wav'
        emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
    }
}

邮件模板

标题

构建通知:${BUILD_STATUS} - ${PROJECT_NAME} -  # ${BUILD_NUMBER}!

内容

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
</head>

<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
    offset="0">
    <table width="95%" cellpadding="0" cellspacing="0"
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        <tr>
            <td>(本邮件是程序自动下发的,请勿回复!)</td>
        </tr>
        <tr>
            <td><h2>
                    <font color="#0000FF">构建结果 - ${BUILD_STATUS}</font>
                </h2></td>
        </tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>项目名称&nbsp;:&nbsp;${PROJECT_NAME}</li>
                    <li>构建编号&nbsp;:&nbsp;第${BUILD_NUMBER}次构建</li>
                    <li>触发原因:&nbsp;${CAUSE}</li>
                    <li>构建日志:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>构建&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li>
                    <li>项目&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">Changes Since Last
                        Successful Build:</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%m</pre>",pathFormat="&nbsp;&nbsp;&nbsp;&nbsp;%p"}
             <br>
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">Failed Test Results</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><pre
                    style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
                <br></td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">构建日志 (最后 100行):</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><textarea id="test" cols="80" rows="30" readonly="readonly"
                    style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
            </td>            
        </tr>
    </table>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容