jenkins+gitlab+微服务发布+k8s发布

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jenkins+gitlab+微服务发布+k8s发布相关的知识,希望对你有一定的参考价值。

背景:jenkins+gitlab+微服务发布+k8s发布实现自动更新
1.配置jenkins pipeline
技术图片

  1. pipeline语法
    `node {
    try {
    stage(‘代码拉取‘) {
    git credentialsId: ‘xiongxj‘, url: ‘[email protected]:xinjiang.xiong/oam.git‘

    }
    stage(‘项目构建‘) {
       sh " /opt/software/apache-maven-3.6.0/bin/mvn clean package" 
    }
    def regPrefix = ‘k8s.harbor.maimaiti.site/oam/‘ 
    stage(‘镜像构建‘) {
    docker.withRegistry(‘http://k8s.harbor.maimaiti.site/‘,‘k8sharbor‘){
         if ("${MODULE}".contains(‘oamboot-eurekaServer‘)){
           dir(‘oamboot-eureka-server‘) {
                def imageName = docker.build("${regPrefix}oam-eurekaserver:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}oam-eurekaserver:V1.0-${env.BUILD_ID}"                
            }
            }
         if ("${MODULE}".contains(‘oambootActiviti‘)){
           dir(‘oamboot-activiti‘) {
                def imageName = docker.build("${regPrefix}oam-activiti:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}oam-activiti:V1.0-${env.BUILD_ID}"                
            }
            }       
         if ("${MODULE}".contains(‘oamboot-applyBasicOperation‘)){
           dir(‘oamboot-applyBasicOperation‘) {
                def imageName = docker.build("${regPrefix}apply-basicoperation:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}apply-basicoperation:V1.0-${env.BUILD_ID}"                
            }
            }                   
         if ("${MODULE}".contains(‘oamboot-applyDB‘)){
           dir(‘oamboot-applyDB‘) {
                def imageName = docker.build("${regPrefix}oam-applydb:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}oam-applydb:V1.0-${env.BUILD_ID}"                
            }
            }                   
         if ("${MODULE}".contains(‘oamboot-applyOperation‘)){
           dir(‘oamboot-applyOperation‘) {
                def imageName = docker.build("${regPrefix}apply-operation:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}apply-operation:V1.0-${env.BUILD_ID}"                
            }
            }                   
         if ("${MODULE}".contains(‘oamboot-basic‘)){
           dir(‘oamboot-basic‘) {
                def imageName = docker.build("${regPrefix}oam-basic:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}oam-basic:V1.0-${env.BUILD_ID}"                
            }
            }                   
         if ("${MODULE}".contains(‘oamboot-cmdb‘)){
           dir(‘oamboot-cmdb‘) {
                def imageName = docker.build("${regPrefix}oamboot-cmdb:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}oamboot-cmdb:V1.0-${env.BUILD_ID}"                
            }
            }   
         if ("${MODULE}".contains(‘oamboot-email‘)){
           dir(‘oamboot-email‘) {
                def imageName = docker.build("${regPrefix}oam-email:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}oam-email:V1.0-${env.BUILD_ID}"                
            }
            }   
         if ("${MODULE}".contains(‘oamboot-file‘)){
           dir(‘oamboot-file‘) {
                def imageName = docker.build("${regPrefix}oam-file:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}oam-file:V1.0-${env.BUILD_ID}"                
            }
            }   
        if ("${MODULE}".contains(‘oamboot-zuul‘)){
           dir(‘oamboot-zuul‘) {
                def imageName = docker.build("${regPrefix}oam-api-getaway-zuul:V1.0-${env.BUILD_ID}")
                imageName.push("V1.0-${env.BUILD_ID}")
                //imageName.push("latest")
                sh "/usr/bin/docker rmi ${regPrefix}oam-api-getaway-zuul:V1.0-${env.BUILD_ID}"                
            }
            }
           }                
        }
    stage(‘重启应用‘){
         if ("${MODULE}".contains(‘oamboot-eurekaServer‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/oam-EurekaServer.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/oam-EurekaServer.yml  --record "    
            }
         if ("${MODULE}".contains(‘oambootActiviti‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/oam-activiti.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/oam-activiti.yml  --record "                  
            }       
         if ("${MODULE}".contains(‘oamboot-applyBasicOperation‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/apply-basicoperation.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/apply-basicoperation.yml --record " 
            }                   
         if ("${MODULE}".contains(‘oamboot-applyDB‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/oam-applydb.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/oam-applydb.yml  --record " 
            }                   
         if ("${MODULE}".contains(‘oamboot-applyOperation‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/apply-operation.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/apply-operation.yml  --record " 
            }                   
         if ("${MODULE}".contains(‘oamboot-basic‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/oam-basic.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/oam-basic.yml  --record "   
            }                   
         if ("${MODULE}".contains(‘oamboot-cmdb‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/oamboot-cmdb.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/oamboot-cmdb.yml  --record "    
            }   
         if ("${MODULE}".contains(‘oamboot-email‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/oamboot-email.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/oamboot-email.yml  --record "   
            }   
         if ("${MODULE}".contains(‘oamboot-file‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/oam-file.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/oam-file.yml  --record "    
            }   
        if ("${MODULE}".contains(‘oamboot-zuul‘)){
            sh "sed -i \‘s/latest/V1.0-${env.BUILD_ID}/g\‘ yaml/oamboot-zuul.yml"
            sh "/usr/local/bin/kubectl   apply  -f         yaml/oamboot-zuul.yml  --record "                
        }            
    
    }       

}catch (any) {
currentBuild.result = ‘FAILURE‘
throw any}

}
`

以上是关于jenkins+gitlab+微服务发布+k8s发布的主要内容,如果未能解决你的问题,请参考以下文章

gitlab + jenkins + docker + k8s

第四十三章 微服务CICD- gitlab + jenkins + docker

jenkins+docker+gitlab+harbor+pipeline快速部署发版流程

Jenkins-k8s-helm-harbor-githab-mysql-nfs微服务发布平台实战

阿里云-容器服务之集群服务 k8s(Jenkins+gitlab+k8s的devops)- 01

k8s的持续集成(jenkins+gitlab+k8s)