Jenkins+Kubernetes+Docker完成微服务持续集成

2022/3/10 23:14:47

本文主要是介绍Jenkins+Kubernetes+Docker完成微服务持续集成,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Jenkins+Kubernetes+Docker完成微服务持续集成

 

拉取代码,构建镜像

创建NFS共享目录

让所有Jenkins-Slave构建指向NFS的Maven的共享仓库目录

vi /etc/exports
/opt/nfs/jenkins   *(rw,no_root_squash)
/opt/nfs/maven    *(rw,no_root_squash)
systemctl restart nfs	重启NFS

记得重启nfs!

 

然后

注意:在构建过程会发现无法创建仓库目录,是因为NFS共享目录权限不足,需更改权限

 

 

 

然后就是去创建一个新项目:

 

创建项目,编写构建Pipeline

 

 

 

 

配置一下harbor凭证

 

 

然后再去编写代码:

def git_address = "http://20.0.0.20:82/root/tensquare_back.git" 
def git_auth = "904eff5d-41c8-44ad-ba24-7f539a0edb96"
//构建版本的名称
def tag = "latest"
//Harbor私服地址
def harbor_url = "20.0.0.50:85"
//Harbor的项目名称
def harbor_project_name = "tensquare"
//Harbor的凭证
def harbor_auth = "427399a8-db35-48e8-b5cf-a1ea63f10cc5"



podTemplate(label: 'jenkins-slave', cloud: 'kubernetes', containers: [ 
        containerTemplate(
            name: 'jnlp',
            image: "20.0.0.50:85/library/jenkins-slave-maven:latest"
        ),
        containerTemplate( 
            name: 'docker',
            image: "docker:stable",
            ttyEnabled: true,
            command: 'cat'
        ),
    ],
    volumes: [
        hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
        nfsVolume(mountPath: '/usr/local/apache-maven/repo', serverAddress: '20.0.0.10' , serverPath: '/opt/nfs/maven'),
    ],
)
{
node("jenkins-slave"){
    // 第一步
    stage('pull code'){
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
    }
    // 第二步
    stage('make public sub project'){
        //编译并安装公共工程
        sh "mvn -f tensquare_common clean install"
    }
    // 第三步
    stage('make image'){
        //把选择的项目信息转为数组
        def selectedProjects = "${project_name}".split(',')


        for(int i=0;i<selectedProjects.size();i++){
            //取出每个项目的名称和端口
            def currentProject = selectedProjects[i];
            //项目名称
            def currentProjectName = currentProject.split('@')[0]
            //项目启动端口
            def currentProjectPort = currentProject.split('@')[1]

            //定义镜像名称
            def imageName = "${currentProjectName}:${tag}"

            //编译,构建本地镜像
            sh "mvn -f ${currentProjectName} clean package dockerfile:build"
            container('docker') {

                //给镜像打标签
                sh "docker tag ${imageName} ${harbor_url}/${harbor_project_name}/${imageName}"

                //登录Harbor,并上传镜像
                withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')])
                {
                    //登录
                    sh "docker login -u ${username} -p ${password} ${harbor_url}"
                    //上传镜像
                    sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}"
                }

            //删除本地镜像
            sh "docker rmi -f ${imageName}" 
            sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}"
            }
        }
    }
}
}

  

要修改地方:

 

 

然后去构建看结果!!

 



这篇关于Jenkins+Kubernetes+Docker完成微服务持续集成的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程