jenkins pipeline docker 发布项目

环境:

192.168.100.148 开发环境

192.168.100.140 jenkins 服务器

harbor.zong.com 镜像仓库

 

1.拉取代码

2.构建

3.sonarqube 代码扫描质检

4.构建生成镜像推送仓库

5.部署到开发环境服务器

6.成功或失败钉钉通知群内

pipeline {
    agent any
    
    
    environment {
        _version = sh(script: "echo `date '+%Y%m%d%H%M%S'`", returnStdout: true).trim()
        DINGDING_ROBOT_URL = 'https://oapi.dingtalk.com/robot/send?access_token=aa67b55f25245c319aedd6efe5d20d81ac1cfe6e858c15d20914e8fd26a4e6ac'
        BUILD_USER_ID = ""
        BUILD_USER = ""
        BUILD_USER_EMAIL = ""
        ChangeLog = ""
        Docker_mg_Name = "47.99.1.15:81/192.168.100.148/"         //Harbor Image warehouse address 
        Esl_app_1 = "esl_app_1" 
        BRANCH = "dev_3.5"
    }



    stages {

       stage ("Time stamp") { //time stamp
            steps {
                echo "${_version}"

            }

        }
  
        


        stage('Pull code') {          //get project code from repo
            steps {
                  checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://192.168.100.140/esl_backend/zk.git']]])

            }
        }
    


        stage('structure') {        //exec mvn cmd
            steps {
                withMaven(jdk: 'jdk', maven: 'maven') {
                   sh 'mvn clean package -DskipTests ' 
                }
            }
        }
       stage('Code scanning') {
            steps {
                echo "starting codeAnalyze with SonarQube......"
                sh "mvn sonar:sonar -Dsonar.projectName=148-服务端  -Dsonar.host.url=http://192.168.100.118:9000 -Dsonar.login=e6e89488da1d3051006be436a4899d4209771851"
            }
        }



        stage('Building a mirror image') {         //move jar file into project_dir
            steps {
                  sh "docker build -t ${Docker_mg_Name}${Esl_app_1}:${BRANCH} ."  //Building a mirror image
                  sh "docker login 47.99.1.15:81 -uadmin -pZkongNB1234"                 //Log in to image warehouse
                  sh "docker push ${Docker_mg_Name}${Esl_app_1}:${BRANCH}"       // Push to remote image warehouse
                  sh "docker rmi ${Docker_mg_Name}${Esl_app_1}:${BRANCH}"         //Delete local mirror 
                  
            }
        }

        stage('Running image'){ //pull run docker images
           steps {
             sshPublisher(publishers: [sshPublisherDesc(configName: '148', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "bash /server/scripts/deploy.sh ${BRANCH}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])           }
       }


    }



post {
        //Build failure notification

        failure {
          script {
                 dingtalk (
                        robot: '31ca2fb0-964a-468f-81dd-edf521f78446',
                        type: 'MARKDOWN',
                        title: "项目构建信息",
                        text: [
                            '### 项目构建信息',
                            ">- job名称:    **${JOB_NAME}**",
                            ">- 持续时间:    **${currentBuild.durationString}**",
                            ">- 当前分支:    **${env.BRANCH}**",
                            ">- 构建发起:    **${BUILD_USER}**",
                            ">- 构建结果:   **构建失败❌*",
                            ">- 构建日志:    [点击查看详情](${env.BUILD_URL}console)",
                           // ">- 更新记录:    **${ChangeLog}**",
                             
                                   
                        ],
 
                        at: [
                          '所有人'
                        ]
                    )
          }
 
        }

        //Build success notification

        success {
        wrap([$class: 'BuildUser']) {
                   script {
                       BUILD_USER_ID = "${env.BUILD_USER_ID}"
                       BUILD_USER = "${env.BUILD_USER}"
                       BUILD_USER_EMAIL = "${env.BUILD_USER_EMAIL}"
                       ChangeLog = "${env.ChangeLog}"
                   }
                }
            dingtalk (
                robot: '31ca2fb0-964a-468f-81dd-edf521f78446',
                        type: 'MARKDOWN',
                        title: "项目构建信息",
                        text: [
                            '### 项目构建信息',
                            ">- job名称:    **${JOB_NAME}**",
                            ">- 持续时间:    **${currentBuild.durationString}**",
                            ">- 当前分支:    **${env.BRANCH}**",
                            ">- 构建发起:    **${BUILD_USER}**",
                            ">- 构建结果:   **构建成功 ✅**",
                            ">- 构建日志:    [点击查看详情](${env.BUILD_URL}console)",
                           // ">- 更新记录:    **${ChangeLog}**",
                             
                                   
                        ],
 
                        at: [
                          '所有人'
                        ]
                    )
                }
            }

}

  

上一篇:smtp-pipeline命令


下一篇:Redis实战(四):redis的消息订阅、pipeline、事务、modules、布隆过滤器、缓存LRU