Files
msgexchange-api/jenkins/Jenkinsfile
T

102 lines
4.1 KiB
Groovy
Raw Normal View History

/*
* 依赖插件:
* SSH Pipeline Steps
* Pipeline Utility Steps
*
*/
2018-12-03 10:53:37 +08:00
pipeline {
agent any
2018-12-03 10:53:37 +08:00
environment {
credentialsId = 'e41b57a4-436a-4617-817c-9e3f25a8a47f'
deployName = 'airport-dev'
deployHost = '130.120.3.231'
mailto = '154707516@qq.com'
dockerProtocol = "http"
dockerRegistry = '130.120.3.193'
containerName="msgexchangeapi"
export=95
GROUP = readMavenPom().getGroupId()
ARTIFACTID = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
PACKAGING = readMavenPom(). getPackaging()
2018-12-03 10:53:37 +08:00
}
stages {
stage('maven build') {
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
reuseNode true
}
}
steps {
sh 'mvn clean package -Dmaven.test.skip=true'
}
}
stage('docker build And Push'){
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}" ] ,
2019-04-28 14:41:56 +08:00
command: "docker stop ${env.containerName}",sudo: true ,failOnError: false)
sshCommand(remote: [ name: "${env.deployName}" ,host: "${env.deployHost}" ,allowAnyHosts: true,user: "${userName}",identityFile: "${kfIdentity}" ] ,
2019-04-28 14:41:56 +08:00
command: "docker rm ${env.containerName}",sudo: true ,failOnError: false)
sshCommand(remote: [ name: "${env.deployName}" ,host: "${env.deployHost}" ,allowAnyHosts: true,user: "${userName}",identityFile: "${kfIdentity}" ] ,
2019-04-28 14:41:56 +08:00
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}" ] ,
2019-04-28 14:41:56 +08:00
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']]
)
}
}
2018-12-03 10:53:37 +08:00
}