调整构建脚本,动态pom获取名字及版本信息。

This commit is contained in:
zhouxiunai
2019-04-28 17:34:08 +08:00
parent 582208bdb4
commit dd26709ef0
+97 -60
View File
@@ -1,65 +1,102 @@
node { /*
def mailto="601470458@qq.com,505127606@qq.com,154707516@qq.com,444949396@qq.com,714943302@qq.com" //邮件接收人员列表 * 依赖插件:
def containerName="adminapi" //容器名称 * SSH Pipeline Steps
def imageName="130.120.3.193/com.gzzn.omms/admin-api:1.2.0-SNAPSHOT" //镜像名称 * Pipeline Utility Steps
def credentialsId="e41b57a4-436a-4617-817c-9e3f25a8a47f" //ssh证书id *
def deployHost="130.120.3.231" //发布到的主机 */
try { pipeline {
checkout scm agent any
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'){ environment {
def remote = [:] credentialsId = 'e41b57a4-436a-4617-817c-9e3f25a8a47f'
remote.name = "airport-dev" deployName = 'airport-dev'
remote.host = "${deployHost}" deployHost = '130.120.3.231'
remote.allowAnyHosts = true mailto = '154707516@qq.com'
dockerProtocol = "http"
dockerRegistry = '130.120.3.193'
containerName="adminapi"
export=92
withCredentials([sshUserPrivateKey(credentialsId: "${credentialsId}", keyFileVariable: 'kfIdentity', passphraseVariable: 'pVariable', usernameVariable: 'userName')]) { GROUP = readMavenPom().getGroupId()
remote.user = userName ARTIFACTID = readMavenPom().getArtifactId()
remote.identityFile = kfIdentity VERSION = readMavenPom().getVersion()
PACKAGING = readMavenPom(). getPackaging()
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)
{ stages {
emailext ( stage('maven build') {
subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新失败", agent {
body: """ docker {
详情: image 'maven:3-alpine'
FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' args '-v /root/.m2:/root/.m2'
状态:${env.JOB_NAME} jenkins 运行失败 reuseNode true
URL ${env.BUILD_URL} }
项目名称 ${env.JOB_NAME} }
项目更新进度:${env.BUILD_NUMBER} steps {
""", sh 'mvn clean package -Dmaven.test.skip=true'
to: "${mailto}", }
recipientProviders: [[$class: 'DevelopersRecipientProvider']] }
)
throw e stage('docker build And Push'){
}//end catch steps {
script{
docker.withRegistry("${env.dockerProtocol}://${env.dockerRegistry}", 'privateDockerId') {
docker.build("${env.dockerRegistry}/${env.GROUP}/${env.ARTIFACTID}:${env.VERSION}","--no-cache --build-arg JAR_FILE=target/${env.ARTIFACTID}-${env.VERSION}.${env.PACKAGING} .").push()
}
}
}
}
stage('deploy') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: "${env.credentialsId}", keyFileVariable: 'kfIdentity', passphraseVariable: 'pVariable', usernameVariable: 'userName')]) {
sshCommand(remote: [ name: "${env.deployName}" ,host: "${env.deployHost}" ,allowAnyHosts: true,user: "${userName}",identityFile: "${kfIdentity}" ] ,
command: "docker stop ${env.containerName}",sudo: true ,failOnError: false)
sshCommand(remote: [ name: "${env.deployName}" ,host: "${env.deployHost}" ,allowAnyHosts: true,user: "${userName}",identityFile: "${kfIdentity}" ] ,
command: "docker rm ${env.containerName}",sudo: true ,failOnError: false)
sshCommand(remote: [ name: "${env.deployName}" ,host: "${env.deployHost}" ,allowAnyHosts: true,user: "${userName}",identityFile: "${kfIdentity}" ] ,
command: "docker rmi ${env.dockerRegistry}/${env.GROUP}/${env.ARTIFACTID}:${env.VERSION}",sudo: true ,failOnError: false)
sshCommand(remote: [ name: "${env.deployName}" ,host: "${env.deployHost}" ,allowAnyHosts: true,user: "${userName}",identityFile: "${kfIdentity}" ] ,
command: "docker run -d -p ${env.export}:8080 --name ${env.containerName} ${env.dockerRegistry}/${env.GROUP}/${env.ARTIFACTID}:${env.VERSION}",sudo: true ,failOnError: true)
}
}
}
}
post {
success {
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: "${env.mailto}",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
}
failure {
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: "${env.mailto}",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
}
}
} }