// 获取当前的git分支 def branch(){ switch("${env.GIT_BRANCH}") { case "origin/dev": return "dev" case "origin/release": return "release" case "origin/beta": return "beta" case "origin/master": return "master" case "*/dev": return "dev" case "*/release": return "release" case "*/beta": return "beta" case "*/master": return "master" } return "" } // 同步yaml部署文件 def yaml(){ switch("${env.Branch}") { case "dev": sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: "${NAME}-job-${env.BRANCH}.yaml")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) break case "release": break case "beta": echo "yaml beta" break case "master": sshPublisher(publishers: [sshPublisherDesc(configName: 'master-k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: "${NAME}-job-${env.BRANCH}.yaml")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) break } } // 执行yaml部署 def apply(){ switch("${env.Branch}") { case "dev": sh "ssh root@192.168.1.156 kubectl apply -f /usr/local/k8s/${NAME}-job-${env.BRANCH}.yaml" break case "release": echo "apply release" break case "beta": echo "apply beta" break case "master": sshagent(['test-k8s-login-key']) { sh "ssh root@193.112.79.170 kubectl apply -f /usr/local/k8s/${NAME}-job-${env.BRANCH}.yaml" } break } } def image(){ switch("${env.Branch}") { case "dev": sh "ssh root@192.168.1.156 kubectl set image Deployment/${env.K8S_DEPLOYMENT} ${env.NAME}=192.168.1.131:8888/job/${env.BRANCH}/${env.NAME}:${env.VERSION} -n ${env.BRANCH}" break case "release": echo "image release" break case "beta": echo "image beta" break case "master": sshagent(['test-k8s-login-key']) { sh "ssh root@193.112.79.170 kubectl set image Deployment/${env.K8S_DEPLOYMENT} ${env.NAME}=192.168.0.19:5000/job/${env.BRANCH}/${env.NAME}:${env.VERSION} -n ${env.BRANCH}" } break } } pipeline { agent any environment { // 服务名 NAME = 'hotspot-clear' // 镜像名称,对应K8S_FILE里面Deployment.containers的name // git路径 // GIT_URL = 'http://git.gz.internal.jumaiyx.cn/job/hotspot-clear.git' // k8s deployment名称,对应K8S_FILE里面Deployment的name K8S_DEPLOYMENT = 'hotspot-clear-job' // 分支 BRANCH = branch() // 更新的版本 VERSION = "${env.GIT_COMMIT}" // 自动生成版本号,仅限dev和release使用,master按照tag版本 IMAGE_VERSION = UUID.randomUUID().toString() } stages { // 首次部署需要使用,后期可以直接注释掉此步骤 stage('传入yaml') { steps { // yaml() echo '传入yaml 完成' } } // 首次部署需要使用,后期可以直接注释掉此步骤 stage('apply') { steps { // apply() echo '执行apply 完成' } } stage('更新image') { steps { image() echo '更新image 完成' } } } post { always { echo 'I will always say Hello again!' } success { // FeiShu(webhook:fei(),proxy:'',msg:'',atAll:false,type:'') echo '部署完成' } failure { // FeiShu(webhook:fei(),proxy:'',msg:'',atAll:false,type:'') echo 'I will always say Hello again!' } } }