Skip to content

Commit

Permalink
feat($module=convention/core): 🎉 Jenkins/Deploy
Browse files Browse the repository at this point in the history
- ref #2
  • Loading branch information
o-w-o committed Nov 10, 2019
1 parent 04910da commit 1bfe588
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
pipeline {
agent any

environment {
DOCKER_REGISTRY_NAMESPACE = 'registry.cn-beijing.aliyuncs.com/o-w-o'
}

parameters {
string(name: 'TAGGED_DOCKER_IMG', defaultValue: "${DOCKER_REGISTRY_NAMESPACE}/api:latest", description: '已经 build 并 push 成功的项目镜像名')
}

stages {
stage('Prepare') {
agent {
Expand All @@ -21,12 +29,16 @@ pipeline {
JAR_FILENAME = sh(returnStdout: true, script:"mvn -q -N -Dexec.executable='echo' -Dexec.args='\${project.build.finalName}' exec:exec").trim()
JAR_PATH = "target/${JAR_FILENAME}.jar"

println IMG_BUILD_TAG
println JAR_FILENAME
println JAR_PATH
println "【 IMG_BUILD_TAG 】-> " + IMG_BUILD_TAG
println "【 JAR_FILENAME 】-> " + JAR_FILENAME
println "【 JAR_PATH 】-> " + JAR_PATH
}

input('构建版本号为【' + IMG_BUILD_TAG + '】, 确定吗?')

script {
environment TAGGED_DOCKER_IMG = "${DOCKER_REGISTRY_NAMESPACE}/api:${IMG_BUILD_TAG}"
}
}
}
stage('Test') {
Expand Down Expand Up @@ -57,17 +69,40 @@ pipeline {
sh "docker login -u ${dockerHubUser} -p ${dockerHubPassword} registry.cn-beijing.aliyuncs.com"

echo "4.2 构建 Image"
sh "docker build --build-arg JAR_PATH=${JAR_PATH} --tag registry.cn-beijing.aliyuncs.com/o-w-o/api:${IMG_BUILD_TAG} ."
sh "docker build --build-arg JAR_PATH=${JAR_PATH} --tag ${TAGGED_DOCKER_IMG} ."

echo "4.3 发布 Image"
sh "docker push registry.cn-beijing.aliyuncs.com/o-w-o/api:${IMG_BUILD_TAG}"
sh "docker push ${TAGGED_DOCKER_IMG}"
}
}
}
stage('Deploy') {
steps {
echo "5. Deploy Stage"
input "确认要部署线上环境吗?"

withCredentials([usernamePassword(credentialsId: 'sshKey', passwordVariable: 'username', usernameVariable: 'password')]) {

script {
def remote = [:]
remote.name = "o-w-o"
remote.host = "o-w-o.ink"
remote.allowAnyHosts = true
remote.user = username
remote.password = password



try {
sshCommand remote: remote, command: "docker stop api"
sshCommand remote: remote, command: "docker rm api"
} catch (e) {
echo '部署预处理异常 -> ${e.message}'
}

sshCommand remote: remote, command: "docker run -it --rm --net=host --name=api -e JAVA_OPTS='-Xms128m -Xmx256m' ${TAGGED_DOCKER_IMG}"
}
}
}
}
}
Expand Down

0 comments on commit 1bfe588

Please sign in to comment.