From a60a8363dbd81e8fb3a412b71fe325487b2ef0ee Mon Sep 17 00:00:00 2001 From: zhouxiunai <154707516@qq.com> Date: Wed, 7 Nov 2018 10:41:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=82=AE=E4=BB=B6=E5=8F=91?= =?UTF-8?q?=E9=80=81=E5=8F=8A=E7=81=B5=E6=B4=BB=E5=AE=9A=E4=BD=8D=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins/Jenkinsfile | 84 +++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 22 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 1a7e555..5dc336c 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -1,25 +1,65 @@ node { - checkout scm - docker.image('maven:3-alpine').inside('-v /root/.m2:/root/.m2') { - stage('build'){ - sh 'mvn clean package dockerfile:build dockerfile:push -Dmaven.test.skip=true' - } - - stage('deploy'){ - def remote = [:] - remote.name = "airport-dev" - remote.host = "130.120.3.231" - remote.allowAnyHosts = true - - withCredentials([sshUserPrivateKey(credentialsId: 'e41b57a4-436a-4617-817c-9e3f25a8a47f', keyFileVariable: 'kfIdentity', passphraseVariable: 'pVariable', usernameVariable: 'userName')]) { - remote.user = userName - remote.identityFile = kfIdentity - - sshCommand remote: remote, command: "docker stop adminapi",sudo: true ,failOnError: false - sshCommand remote: remote, command: "docker rm adminapi",sudo: true ,failOnError: false - sshCommand remote: remote, command: "docker rmi 130.120.3.193/com.gzzn.omms/admin-api:0.0.1-SNAPSHOT",sudo: true ,failOnError: false - sshCommand remote: remote, command: "docker run -d -p 92:8080 --name adminapi 130.120.3.193/com.gzzn.omms/admin-api:0.0.1-SNAPSHOT",sudo: true - } - } + def mailto="601470458@qq.com,505127606@qq.com,154707516@qq.com,444949396@qq.com,714943302@qq.com" //邮件接收人员列表 + def containerName="adminapi" //容器名称 + def imageName="130.120.3.193/com.gzzn.omms/admin-api:0.0.1-SNAPSHOT" //镜像名称 + def credentialsId="e41b57a4-436a-4617-817c-9e3f25a8a47f" //ssh证书id + def deployHost="130.120.3.231" //发布到的主机 + + try { + checkout scm + docker.image('maven:3-alpine').inside('-v /root/.m2:/root/.m2') { + stage('build'){ + sh 'mvn clean package dockerfile:build dockerfile:push -Dmaven.test.skip=true' + } + + stage('deploy'){ + def remote = [:] + remote.name = "airport-dev" + remote.host = "${deployHost}" + remote.allowAnyHosts = true + + withCredentials([sshUserPrivateKey(credentialsId: ${credentialsId}, keyFileVariable: 'kfIdentity', passphraseVariable: 'pVariable', usernameVariable: 'userName')]) { + remote.user = userName + remote.identityFile = kfIdentity + + sshCommand remote: remote, command: "docker stop ${containerName}",sudo: true ,failOnError: false + sshCommand remote: remote, command: "docker rm ${containerName}",sudo: true ,failOnError: false + sshCommand remote: remote, command: "docker rmi ${imageName}",sudo: true ,failOnError: false + sshCommand remote: remote, command: "docker run -d -p 92:8080 --name ${containerName} ${imageName}",sudo: true + } + } + }//end docker + + //发送成功邮件 + emailext ( + subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新正常", + body: """ + 详情: + SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' + 状态:${env.JOB_NAME} jenkins 更新运行正常 + URL :${env.BUILD_URL} + 项目名称 :${env.JOB_NAME} + 项目更新进度:${env.BUILD_NUMBER} + """, + to: "${mailto}", + recipientProviders: [[$class: 'DevelopersRecipientProvider']] + ) } + catch(e) + { + emailext ( + subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新失败", + body: """ + 详情: + FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' + 状态:${env.JOB_NAME} jenkins 运行失败 + URL :${env.BUILD_URL} + 项目名称 :${env.JOB_NAME} + 项目更新进度:${env.BUILD_NUMBER} + """, + to: "${mailto}", + recipientProviders: [[$class: 'DevelopersRecipientProvider']] + ) + throw e + }//end catch } \ No newline at end of file