Jenkinsfile通用脚本部署springcloud微服务
#!groovy @Library('jenkinslib') _ def tools = new org.devops.tools() pipeline { agent { label "boot" } parameters { //git动态分支 gitParameter name: 'BRANCH_TAG', type: 'PT_BRANCH_TAG', branchFilter: 'origin.*/(.*)', defaultValue: 'master', useRepository: 'http://192.168.2.109:30000/kkbsaas/kkb-saas-platform.git' //模块名 hidden defaultValue: 'springcloud-gateway', name: 'moduleName' //模块路径 hidden defaultValue: 'springcloud-gateway/target/springcloud-gateway.jar', name: 'modulePath' //部署环境选择 choice choices: ['dev', 'test', 'production'], description: '''请选择要部署的环境 dev:开发环境 test:测试环境 production:生产环境''', name: 'DeployEnv' choice choices: ['Deploy', 'Rollback'], description: '''请选择部署方式 Deploy 发布 Rollback 回滚''', name: 'Status' //K8S命名空间选择,关联部署环境 DeployEnv reactiveChoice choiceType: 'PT_SINGLE_SELECT', description: 'k8s命名空间', filterLength: 1, filterable: false, name: 'containerNamespace', randomName: 'choice-parameter-450509040370658', referencedParameters: 'DeployEnv', script: groovyScript(fallbackScript: [classpath: [], oldScript: '', sandbox: false, script: ''], script: [classpath: [], oldScript: '', sandbox: true, script: '''if(DeployEnv.equals("dev")){ return ["c2c-dev"] } if(DeployEnv.equals("test")){ return ["c2c-test"] } if(DeployEnv.equals("production")){ return ["prod-c2c"] }''']) //K8S集群 reactiveChoice choiceType: 'PT_SINGLE_SELECT', description: 'k8s集群,返回的是证书凭据名称,引用关联参数:部署环境DeployEnv', filterLength: 1, filterable: false, name: 'k8s-cluster', randomName: 'choice-parameter-451081864328805', referencedParameters: 'DeployEnv', script: groovyScript(fallbackScript: [classpath: [], oldScript: '', sandbox: false, script: ''], script: [classpath: [], oldScript: '', sandbox: true, script: '''if(DeployEnv.equals("dev")){ return ["k8s-test"] } if(DeployEnv.equals("test")){ return ["k8s-test"] } if(DeployEnv.equals("production")){ return ["k8s-production"] }''']) //服务启动环境 reactiveChoice choiceType: 'PT_SINGLE_SELECT', description: '用于服务启动区分环境,与后端代码强关联', filterLength: 1, filterable: false, name: 'ENVIRONMENT', randomName: 'choice-parameter-451177269999208', referencedParameters: 'DeployEnv', script: groovyScript(fallbackScript: [classpath: [], oldScript: '', sandbox: false, script: ''], script: [classpath: [], oldScript: '', sandbox: true, script: '''if(DeployEnv.equals("dev")){ return ["dev"] } if(DeployEnv.equals("test")){ return ["Test"] } if(DeployEnv.equals("production")){ return ["Pro"] }''']) } // //环境变量 environment { //job名称 def webName = "${containrName}" //构建的序号 def BuildNumber = "${BUILD_NUMBER}" def GitURL = "${gitUrl}" //docker镜像仓库地址 def REPOSITORY="harbor.devops.70mail.cn:9001/c2c/${webName}:${BRANCH_TAG}-${BuildNumber}".toLowerCase() //容器环境变量 def ENVIRONMENT="${ENVIRONMENT}" //K8S集群连接地址 //K8S命名空间 def K8S_NAMESPACE = "${containerNamespace}" //模块名字 def ModuleName = "${moduleName}" } stages { stage('pull code'){ steps{ script{ tools.PrintMes("获取代码","green") } checkout([$class: 'GitSCM', branches: [[name: "${BRANCH_TAG}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab', url: "${gitUrl}"]]]) sh 'ls -lh' } } stage('Build') { when{expression{ "${Status}" == 'Deploy' }} steps { script{ tools.PrintMes("开始构建","green") } configFileProvider([configFile(fileId: 'springboot-DockerFile', targetLocation: 'Dockerfile', variable: 'content')]) { script{ template = readFile encoding: 'UTF-8', file: "${content}" sh 'cat $template' // sh 'echo "模块名:${ModuleName}"' // sh "sed -i 's#<sourceJar>#${modulePath}#g' Dockerfile" // sh "sed -i 's#<dstJar>#/app/$(basename ${path})#g' Dockerfile" // sh "cat Dockerfile" // sh "mvn clean package -pl $ModuleName -am" sh """ echo "模块名:${ModuleName}" sed -i 's#<sourceJar>#${modulePath}#g' Dockerfile sed -i 's#<dstJar>#/app/${modulePath}#g' Dockerfile cat Dockerfile mvn clean package -pl $ModuleName -am """ } } sh ''' docker build -t $REPOSITORY . docker login https://harbor.devops.70mail.cn:9001 -u admin -p t5gF7FZK0YDlcsxr docker push $REPOSITORY for i in `docker images|grep $REPOSITORY|awk -F " " '{print $3}'`; do docker rmi $i; done ''' } } stage('编排'){ when{expression{ "${Status}" == 'Deploy' }} steps{ script{ tools.PrintMes("开始编排yaml","green") } configFileProvider([configFile(fileId: 'a96bb0ea-93cd-4aea-94a3-68662f85f5f8', targetLocation: 'deploy/deployment.yaml', variable: 'content')]) { script{ sh "sed -i 's#<K8S_NAMESPACE>#${K8S_NAMESPACE}#g' deploy/deployment.yaml" sh "sed -i 's#<webName>#${webName}#g' deploy/deployment.yaml" sh "sed -i 's#<ENVIRONMENT>#${ENVIRONMENT}#g' deploy/deployment.yaml" sh "sed -i 's#<REPOSITORY>#${REPOSITORY}#g' deploy/deployment.yaml" } } configFileProvider([configFile(fileId: '803f4298-89aa-4454-a42b-51bdfb37f072', targetLocation: 'deploy/service.yaml', variable: 'content')]) { script{ sh "sed -i 's#<K8S_NAMESPACE>#${K8S_NAMESPACE}#g' deploy/service.yaml" sh "sed -i 's#<webName>#${webName}#g' deploy/service.yaml" } } } } //发布 stage('发布'){ when{expression{ "${Status}" == 'Deploy' }} steps{ script{ tools.PrintMes("开始部署","green") if(DeployEnv.equals("dev")||DeployEnv.equals("test")){ env.credentialsName="k8s-test" env.k8sClusterAddr="https://192.168.2.107:6443" tools.PrintMes("测试环境部署","green") println(env.credentialsName) println(env.k8sClusterAddr) } if(DeployEnv.equals("production")){ env.credentialsName="k8s-prod" env.k8sClusterAddr="https://192.168.2.107:6443" tools.PrintMes("生产环境部署","green") println(env.k8sClusterAddr) } } withKubeConfig([credentialsId: "${env.credentialsName}", serverUrl: "${env.k8sClusterAddr}"]) { sh "/usr/bin/kubectl apply -f deploy/deployment.yaml" sh "/usr/bin/kubectl apply -f deploy/service.yaml" sh "/usr/bin/kubectl get pods -n ${K8S_NAMESPACE}" } } } //回滚 stage('回滚'){ when{expression{ "${Status}" == 'Rollback' }} steps{ script{ tools.PrintMes("开始回滚","green") if(DeployEnv.equals("dev")||DeployEnv.equals("test")){ env.credentialsName="k8s-test" env.k8sClusterAddr="https://192.168.2.107:6443" tools.PrintMes("开发/测试环境回滚","green") println(env.credentialsName) println(env.k8sClusterAddr) } if(DeployEnv.equals("production")){ env.credentialsName="k8s-prod" env.k8sClusterAddr="https://192.168.2.107:6443" tools.PrintMes("生产环境部署","green") println(env.k8sClusterAddr) } } withKubeConfig([credentialsId: "${env.credentialsName}", serverUrl: "${env.k8sClusterAddr}"]) { sh "kubectl rollout undo deploy -n ${K8S_NAMESPACE} ${webName} --to-revision=1" sh "kubectl get deploy -n ${K8S_NAMESPACE} ${webName} -o yaml |grep -i image:" } } } // } }
阅读剩余
网站名称: DevOps运维技术栈
本站网址:www.Linux66.cn
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!邮件:pengch1611@gmail.com
网站部分内容来源于网络,版权争议与本站无关。请在下载后的24小时内从您的设备中彻底删除上述内容。
如无特别声明本文即为原创文章仅代表个人观点,版权归《DevOps运维技术栈》所有,欢迎转载,转载请保留原文链接。
本站网址:www.Linux66.cn
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!邮件:pengch1611@gmail.com
网站部分内容来源于网络,版权争议与本站无关。请在下载后的24小时内从您的设备中彻底删除上述内容。
如无特别声明本文即为原创文章仅代表个人观点,版权归《DevOps运维技术栈》所有,欢迎转载,转载请保留原文链接。
THE END