237 lines
10 KiB
Plaintext
237 lines
10 KiB
Plaintext
pipeline {
|
|
agent any
|
|
options {
|
|
buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '2', numToKeepStr: '2'))
|
|
}
|
|
stages {
|
|
stage('Setup Parameters & ENV') {
|
|
options {
|
|
skipDefaultCheckout()
|
|
}
|
|
agent {
|
|
label "${env.BRANCH_NAME}"
|
|
}
|
|
steps {
|
|
checkout scmGit([
|
|
branches: [[name: '*/master']],
|
|
userRemoteConfigs: [[
|
|
credentialsId: 'c9b5eae2-5df5-485f-bebd-9cd5393b03e1',
|
|
url: 'http://192.168.10.202:3000/cicd/pipeline-files.git'
|
|
]],
|
|
extensions: [[
|
|
$class: 'RelativeTargetDirectory',
|
|
relativeTargetDir: '.pipeline-files'
|
|
]],
|
|
])
|
|
script {
|
|
def settingsJson = readJSON file: ".pipeline-files/jenkins-file/xiaomayi.k8s.JsonConfig.json"
|
|
// def config = readJSON file:"$settingsFile"
|
|
def jobNameStr = JOB_NAME.tokenize('/') as String[]
|
|
def projName = jobNameStr[0]
|
|
def branchConfig = settingsJson."branchConfig"."${env.BRANCH_NAME}"
|
|
def serviceConfig = settingsJson."serviceConfig"."${projName}"
|
|
|
|
MAVEN_SET = branchConfig.MAVEN_SET
|
|
registry = branchConfig.registry
|
|
registryCredential = branchConfig.registryCredential
|
|
registry_project = branchConfig.registry_project
|
|
jenkinsAgent = branchConfig.jenkinsAgent
|
|
|
|
echo "projName: ${projName}"
|
|
echo "The Maven Set XML for the ${env.BRANCH_NAME} branch is: ${MAVEN_SET}"
|
|
|
|
containerName = serviceConfig.containerName
|
|
servicePath = serviceConfig.servicePath
|
|
baseImage = "${registry}/xiaomayi-base/jdk:17.0.11-centos8"
|
|
servicePort = serviceConfig."port"
|
|
jarFile = "${containerName}.jar"
|
|
jarFilePath = "${servicePath}/target"
|
|
serverHost01 = serviceConfig.serverHost01
|
|
|
|
echo "jarFile: ${jarFile}"
|
|
echo "jarFilePath: ${jarFilePath}"
|
|
|
|
if ("${env.BRANCH_NAME}" == "master" && "${params.PROFILES}" != "null") {
|
|
k8s_namespace = branchConfig."${params.PROFILES}".k8s_namespace
|
|
k8s_cluster = branchConfig."${params.PROFILES}".k8s_cluster
|
|
serverIP01 = branchConfig."${params.PROFILES}"."${serverHost01}"
|
|
} else {
|
|
k8s_namespace = branchConfig.k8s_namespace
|
|
k8s_cluster = branchConfig.k8s_cluster
|
|
serverIP01 = branchConfig."${serverHost01}"
|
|
}
|
|
echo "k8s_cluster: ${k8s_cluster}"
|
|
echo "k8s_namespace: ${k8s_namespace}"
|
|
echo "serverIP01: ${serverIP01}"
|
|
|
|
if ("${serviceConfig."nodePort"}" != 'null') {
|
|
nodePort = serviceConfig."nodePort"."${env.BRANCH_NAME}"
|
|
echo "nodePort: ${nodePort}"
|
|
} else {
|
|
nodePort = "null"
|
|
echo "nodePort: ${nodePort}"
|
|
}
|
|
|
|
if ("${serviceConfig."pvcName"}" != 'null') {
|
|
pvcName = serviceConfig."pvcName"
|
|
echo "PVC Name: ${pvcName}"
|
|
} else {
|
|
pvcName = "null"
|
|
echo "PVC Name: ${pvcName}"
|
|
}
|
|
|
|
if ("${env.BRANCH_NAME}" != "master") {
|
|
properties([
|
|
parameters([
|
|
string(
|
|
defaultValue: "${env.BRANCH_NAME}",
|
|
description: '编译参数',
|
|
name: 'PROFILES',
|
|
trim: true
|
|
),
|
|
choice (
|
|
choices: ['service','api','all'],
|
|
description: '编译参数: api:附带deploy api, all: 包含子路径pom',
|
|
name: 'APICHOICE'
|
|
)
|
|
])
|
|
])
|
|
} else if ("${env.BRANCH_NAME}" == "master") {
|
|
properties([
|
|
parameters([
|
|
choice (
|
|
choices: ['','uat', 'prod'],
|
|
description: '编译参数: uat-uat环境,prod-生产环境',
|
|
name: 'PROFILES'
|
|
),
|
|
choice (
|
|
choices: ['service','api','all'],
|
|
description: '编译参数: api:附带deploy api, all: 包含子路径pom',
|
|
name: 'APICHOICE'
|
|
)
|
|
])
|
|
])
|
|
} else {
|
|
error("Build failed because failed to fetch settings for branch")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
stage('Build') {
|
|
agent {
|
|
// docker {
|
|
// image "maven:3.8.6-openjdk-8"
|
|
// args '-v /etc/hosts:/etc/hosts:ro'
|
|
// label jenkinsAgent
|
|
// }
|
|
label {
|
|
label jenkinsAgent
|
|
}
|
|
}
|
|
steps {
|
|
configFileProvider([configFile(fileId: MAVEN_SET, variable: 'MAVEN_SETTINGS_XML')]) {
|
|
sh "/usr/local/maven/bin/mvn --settings ${MAVEN_SETTINGS_XML} dependency:purge-local-repository -Dinclude=com.xiaomayi -fpom.xml clean package -P${PROFILES} -Dmaven.test.skip=true"
|
|
sh "ls -lsh ${jarFilePath}/${jarFile}"
|
|
sh "md5sum ${jarFilePath}/${jarFile}"
|
|
}
|
|
}
|
|
}
|
|
stage('Container Build') {
|
|
when {
|
|
expression { params.APICHOICE == "service" || params.APICHOICE == "all" }
|
|
}
|
|
options {
|
|
skipDefaultCheckout()
|
|
}
|
|
agent {
|
|
label {
|
|
label jenkinsAgent
|
|
}
|
|
}
|
|
steps {
|
|
script {
|
|
echo "VAR_BASE_IMAGE: ${baseImage}"
|
|
echo "VAR_PORT: ${servicePort}"
|
|
echo "VAR_JARFILE: ${jarFile}"
|
|
echo "VAR_JARFILE_PATH: ${jarFilePath}"
|
|
|
|
def dockerImage = docker.image("${registry_project}/${containerName}:${PROFILES}-${env.BUILD_ID}")
|
|
sh """
|
|
docker build -t ${dockerImage.imageName()} \
|
|
-f Dockerfile \
|
|
--build-arg VAR_BASE_IMAGE=${baseImage} \
|
|
--build-arg VAR_PROFILES=${PROFILES} \
|
|
--build-arg VAR_PORT=${servicePort} \
|
|
--build-arg VAR_JARFILE_PATH=${jarFilePath} \
|
|
--build-arg VAR_JARFILE=${jarFile} .
|
|
"""
|
|
|
|
docker.withRegistry("https://"+registry, registryCredential) {
|
|
dockerImage.push()
|
|
}
|
|
sh """
|
|
docker rmi -f ${registry_project}/${containerName}:${PROFILES}-${env.BUILD_ID}
|
|
docker rmi -f ${registry}/${registry_project}/${containerName}:${PROFILES}-${env.BUILD_ID}
|
|
"""
|
|
}
|
|
cleanWs()
|
|
}
|
|
}
|
|
stage("Deploy") {
|
|
options {
|
|
skipDefaultCheckout()
|
|
}
|
|
when {
|
|
allOf {
|
|
expression { params.PROFILES != "uat" }
|
|
anyOf {
|
|
expression { params.APICHOICE == "service" || params.APICHOICE == "all" }
|
|
}
|
|
}
|
|
}
|
|
agent {
|
|
label {
|
|
label jenkinsAgent
|
|
}
|
|
}
|
|
steps {
|
|
checkout scmGit([
|
|
branches: [[name: '*/master']],
|
|
userRemoteConfigs: [[
|
|
credentialsId: 'c9b5eae2-5df5-485f-bebd-9cd5393b03e1',
|
|
url: 'http://192.168.10.202:3000/cicd/pipeline-files.git'
|
|
]],
|
|
extensions: [[
|
|
$class: 'RelativeTargetDirectory',
|
|
relativeTargetDir: '.pipeline-files'
|
|
]],
|
|
])
|
|
script {
|
|
sh """
|
|
sed -i 's#REGISTRY_URL#${registry}#g' .pipeline-files/k8s-file/${containerName}.yaml
|
|
sed -i 's#NAMESPACE#${k8s_namespace}#g' .pipeline-files/k8s-file/${containerName}.yaml
|
|
sed -i 's#VERSION#${PROFILES}-${env.BUILD_ID}#g' .pipeline-files/k8s-file/${containerName}.yaml
|
|
sed -i 's#SERVICE_PORT#${servicePort}#g' .pipeline-files/k8s-file/${containerName}.yaml
|
|
"""
|
|
if ("${nodePort}" != "null") {
|
|
sh """
|
|
sed -i 's#NODE_PORT#${nodePort}#g' .pipeline-files/k8s-file/${containerName}.yaml
|
|
"""
|
|
}
|
|
if ("${pvcName}" != "null") {
|
|
sh """
|
|
sed -i 's#PVC_NAME#${pvcName}#g' .pipeline-files/k8s-file/${containerName}.yaml
|
|
"""
|
|
}
|
|
sshagent(['jenkins-ssh-207']) {
|
|
sh """
|
|
ssh ${k8s_cluster} 'mkdir -p /home/esxi/k8s-yaml'
|
|
scp .pipeline-files/k8s-file/${containerName}.yaml ${k8s_cluster}:/home/esxi/k8s-yaml
|
|
ssh ${k8s_cluster} 'cd /home/esxi/k8s-yaml && kubectl apply -f ${containerName}.yaml'
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |