def buildService(serverName){ docker.withRegistry('https://registry.internal.jumaiyx.cn', 'docker-registry-internal-backend') { def img = docker.build("jm/$serverName:${env.BUILD_ID}",".") img.push() } } pipeline { agent any environment { // 服务名 SERVER_NAME = 'room' // 镜像名称,对应K8S_FILE里面Deployment.containers的name IMAGE_NAME = 'room' // git路径 GIT_URL = 'http://git.gz.internal.jumaiyx.cn/jm/room.git' // 分支 GIT_BRANCH = '*/master' // kubectl执行文件 K8S_FILE = 'room-dev.yaml' // k8s deployment名称,对应K8S_FILE里面Deployment的name K8S_DEPLOYMENT = 'room-deployment' } stages { stage('git代码拉取') { steps { checkout([$class: 'GitSCM', branches: [[name: "${env.GIT_BRANCH}"]], extensions: [], userRemoteConfigs: [[url: "${env.GIT_URL}"]]]) echo 'git代码拉取 成功' } } stage('docker镜像打包') { steps { echo "镜像版本:${env.BUILD_ID}" buildService("${env.SERVER_NAME}") echo 'docker镜像打包' } } stage('传入k8s-master') { steps { 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: "${env.K8S_FILE}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) echo '将yaml传入k8s-master' } } stage('部署服务') { steps { sh "ssh root@192.168.1.156 kubectl apply -f /usr/local/k8s/${env.K8S_FILE}" echo '部署服务' } } stage('更新image') { steps { sh "ssh root@192.168.1.156 kubectl set image Deployment/${env.K8S_DEPLOYMENT} ${env.IMAGE_NAME}=registry.internal.jumaiyx.cn/jm/${env.SERVER_NAME}:${env.BUILD_ID} -n dev" echo '部署服务' } } } }