def label = "jenkins-slave-yunti_web" podTemplate(label: label, cloud: 'kubernetes', imagePullSecrets:['registryharbor'], idleMinutes:'1440', nodeSelector:'kubernetes.io/hostname=gke-yunti-cluster-1-pool-2-7499bd72-b5x7', slaveConnectTimeout:'600', containers: [ containerTemplate(name: 'docker', image: 'docker:20.10.3-git', command: 'cat', ttyEnabled: true), containerTemplate(name: 'helm', image: 'registry-platform.linkerplus.com/google/cloud-sdk-helm:378.0.0-alpine', command: 'cat', ttyEnabled: true)], serviceAccount: 'jenkins', volumes: [ hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')] ) { node(label) { container('docker') { stage('Prepare') { echo '1.Prepare Stage' checkout scm docker_host = 'registry-platform.linkerplus.com' script { build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() if (env.BRANCH_NAME != null) { build_tag = "${env.BRANCH_NAME}-${build_tag}" } } docker_img_name = "${docker_host}/${env.JOB_NAME}:${build_tag}" echo "********** docker-img-name: ${docker_img_name} **********" } stage('Build') { echo '2.Build Docker Image Stage' sh "docker build -t ${docker_img_name} --target prod -f Dockerfile-gcp ." } stage('Push') { echo '3.Push Docker Image Stage' withCredentials([usernamePassword(credentialsId: 'ba682ee1-c90b-4248-843b-b6f5277104a3', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUser')]) { sh "docker login -u ${dockerUser} -p ${dockerPassword} ${docker_host}" sh "docker push ${docker_img_name}" } } stage('clone k8s deploy') { echo '4.clone k8s deploy' branch_name = "v2.0.0-gcp" withCredentials([sshUserPrivateKey(credentialsId: '6984acee-7988-4e00-9768-b4af0c69ff0f', keyFileVariable: 'GITHUB_KEY')]) { sh 'echo ssh -i $GITHUB_KEY -l git -o StrictHostKeyChecking=no \\"\\$@\\" > run_ssh.sh' sh 'chmod +x run_ssh.sh' withEnv(["GIT_SSH=${WORKSPACE}/run_ssh.sh"]) { sh """ cd .. rm -rf k8s git clone git@122.112.209.52:YunTi/k8s.git cd k8s git checkout ${branch_name} sed -i "s/tag:.*\\".*\\"/tag: \\"${build_tag}\\"/g" ./yunti/charts/dove/values.yaml git add -A if ! git diff-index --quiet HEAD; then git config --global user.email "leo@amtxts.com" git config --global user.name "leo" git commit -m "docker image [${build_tag}] build" git push origin ${branch_name} fi """ } } } } container('helm'){ stage('k8s helm upgrade') { echo '5.k8s helm upgrade' withCredentials([file(credentialsId: 'yunti-20220127', variable: 'GC_KEY')]) { sh """ gcloud auth activate-service-account --key-file=${GC_KEY} gcloud container clusters get-credentials yunti-cluster-1 --zone asia-east1-a --project yunti-20220127 cd ${WORKSPACE}/../k8s helm upgrade -i yunti yunti """ } } } } }