def POD_LABEL = "jnlp-agent-genoany-service-${UUID.randomUUID().toString()}"
def BUILD_VERSION = ''
def CAN_DEPLOY = "YES"
def IS_SUCCESS = true
podTemplate (
label: POD_LABEL, cloud: 'kubernetes',
containers: [
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-agent-docker', args: '${computer.jnlpmac} ${computer.name}'),
//containerTemplate(name: 'sonarscanner', image: 'sonarsource/sonar-scanner-cli:latest', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'maven', image: 'maven:3.6.0-jdk-8-alpine', ttyEnabled: true, command: 'cat')
],
volumes: [
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath:'/var/run/docker.sock'),
persistentVolumeClaim(mountPath: '/root/.m2/repository', claimName: 'maven-repo-pvc', readOnly: false),
configMapVolume(mountPath: '/root/.m2', configMapName: 'settings.xml')
]
) {
node(POD_LABEL) {
try{
stage('code-checkout') {
container('maven') {
checkout scm
}
build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
stage('settings'){
moduleName = "genoany-crud-service"
// 读取 Pom.xml 参数
pom = readMavenPom file: "./${moduleName}/pom.xml"
// 设置 appName 和 appVersion 两个全局参数
appName = "${pom.artifactId}"
def timeStamp = Calendar.getInstance().getTime().format('YYMMddHHmmss',TimeZone.getTimeZone('Asia/Shanghai'))
appVersion = "${pom.version}"+"-"+timeStamp
echo "服务名:${appName}"
echo "版本号:${appVersion}"
}
stage('maven-package'){
container('maven') {
sh "cd ${moduleName}"
sh "mvn -pl ${moduleName} -am -B clean install package -U -DskipTests=true"
}
}
stage('docker-build') {
sh """
cd ${moduleName}
docker build -t 192.168.1.207:6543/com.newpwr/${appName}:${appVersion} .
"""
}
stage('docker-push') {
withCredentials([usernamePassword(credentialsId: 'localDocker', passwordVariable: 'localDockerPassword', usernameVariable: 'localDockerUser')]) {
sh "docker login 192.168.1.207:6543 -u ${localDockerUser} -p ${localDockerPassword}"
sh "docker push 192.168.1.207:6543/com.newpwr/${appName}:${appVersion}"
}
}
stage('deploy') {
if(CAN_DEPLOY == "YES"){
sh """
curl -X PATCH \
-H "content-type: application/strategic-merge-patch+json" \
-H "Authorization:Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkY5NmtWZjVTbWNLdjZ5TWpzZUpPSlJaQjJ4bTBjV1FPY3RFTFNOZDBzczAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJvYXJkLXVzZXItdG9rZW4tcGc1enAiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoia3Vib2FyZC11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiYjA0ZmM5NDMtYmE3NS00NWY2LTlmZmYtYmM2ZmQyMjhlN2RjIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmt1Ym9hcmQtdXNlciJ9.AEmK0-j_xi_Go4xVPLmtZa587C2WUEWcib1UjXgWO4ZoRB_4cRPBiDzmU8kcxKl8-39OY8mg-DQ5yuygB7biDO9RNlPwHGCNcYe63bhaFjp5W3zUq51TDx57r7VKkOAwor9nFwxNEYBauTmA_0_jnloCUMLmKRLlMsttQ_QumPdDFuHruC56yioAQfHv7h9EDQioiGVOO8ng4xW0goutsUOpQbRkQ4TbLck3_oGX7zX7QOazBDHxo-x73TJ6VgsrNuCnSTgKYQNiWAhNYnySLZ5THuT2agol1mkSgAp6B9gIZOL_2zYiOER_an3B9-GSRHIxuYj_ouxBL19T-m1SvQ" \
-d '{"spec":{"template":{"spec":{"containers":[{"name":"genoany-crud-service","image":"192.168.1.207:6543/com.newpwr/${appName}:${appVersion}"}]}}}}' \
"http://192.168.1.18:32567/k8s-api/apis/apps/v1/namespaces/genoany-dev/deployments/${appName}"
"""
}
}
} catch (Exception ex) {
IS_SUCCESS= false
throw ex
} finally {
if(IS_SUCCESS){
echo "success"
} else {
echo "not success"
}
}
}
}