• Jenkins:容器化微服务持续集成-低配版


    前提知识:maven,gitlab,sonarqube,Docker,Harbor,java微服务,Jenkins,Nginx

    基本流程

    image-20210314213223299

    项目代码上传到gitlab

    创建代码仓库

    image-20210314160042770

    image-20210314160123605

    后端代码提交

    加入版本控制:

    image-20210314160200347

    git:

    image-20210314160217329

    提交:

    image-20210314160248062

    image-20210314160320494

    点击commit:

    image-20210314160350003

    添加远程仓库:

    image-20210314160455299

    image-20210314160557938

    推送到远程仓库:

    image-20210314161543367

    image-20210314161624599

    前端代码提交

    先安装TortoiseGit软件

    创建本地仓库:

    image-20210314163631679

    image-20210314163814880

    image-20210314163905303

    push:

    image-20210314164127212

    image-20210314164148123

    代码提交成功:

    image-20210314164211426

    从Gitlab拉取项目源码

    jenkins创建流水线项目:

    image-20210314164408696

    设置流水线:

    image-20210314164549845

    生成checkout语法:

    image-20210314164828004

    参数化构建:

    image-20210314165105427

    项目创建jenkinsfile并提交:

    image-20210314165450969

    def git_auth = "ee475541-cfd3-42a8-b8eb-4e8f9ac5c5b7"
    def git_url = "http://192.168.1.50:82/root/tensquare_back.git"
    node {
       stage('拉取代码') {
            checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
       }
    }
    

    部署:

    image-20210314170536331

    查看项目代码:

    ll /var/lib/jenkins/workspace/tensquare_back
    

    image-20210314170616151

    拉取代码完毕

    SonarQube代码审查

    添加Choice Parameter

    image-20210314171120662

    每个子模块添加sonar-project.properties到根目录

    image-20210314171327840

    # must be unique in a given SonarQube instance
    sonar.projectKey=tensquare_eureka_server
    # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
    sonar.projectName=tensquare_eureka_server
    sonar.projectVersion=1.0
    
    # Path is relative to the sonar-project.properties file. Replace "" by "/" on Windows.
    # This property is optional if sonar.modules is set.
    sonar.sources=.
    sonar.exclusions=**/test/**,**/target/**
    sonar.java.binaries=.
    
    sonar.java.source=1.8
    sonar.java.target=1.8
    #sonar.java.libraries=**/target/classes/**
    
    # Encoding of the source code. Default is default system encoding
    sonar.sourceEncoding=UTF-8
    

    jenkeinsfile添加一个stage 代码审查,注意这里的sonar-scanner和sonarqube不是瞎写的,是根据jenkins配置有关

    def git_auth = "ee475541-cfd3-42a8-b8eb-4e8f9ac5c5b7"
    def git_url = "http://192.168.1.50:82/root/tensquare_back.git"
     node {
        stage('拉取代码') {
             checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
        }
        stage('代码审查') {
                 //引入全局工具配置中的sonarqube-scanner名
             def scannerHome = tool 'sonar-scanner'
             //引入系统配置中Sonarqube server的名
             withSonarQubeEnv('sonarqube'){
                 sh """
                     cd ${project_name}
                     ${scannerHome}/bin/sonar-scanner
                 """
             }
        }
     }
    

    这里我们就可以通过选择指定的项目来审查项目了:

    image-20210314172518513

    image-20210314172804375

    编译打包微服务

    先编译,安装公共子工程

    添加一个stage

    stage('编译,安装公共子工程') {
    	sh "mvn -f tensquare_common clean install"
    }
    

    image-20210314173400951

    image-20210314173424619

    Jenkinsfile中再添加一个stage,这样就可以打包指定微服务了。

        stage('编译,打包微服务工程') {
            sh "mvn -f ${project_name} clean install"
        }
    

    我这里打包注册中心微服务:

    image-20210314174336855

    image-20210314174409224

    有可能打包子工程失败:因为本地环境缺少父工程依赖

    image-20210314181625743

    我们先手动install父工程

    image-20210314181715244

    再把本地仓库中的父工程依赖移动到服务器对应目录下,再重新打包就好了。

    image-20210314181727186

    使用Dockerfile编译、生成镜像

    利用dockerfile-maven-plugin插件构建docker镜像

    每个微服务项目的pom.xml加入该插件

    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <version>1.3.6</version>
        <configuration>
            <repository>${project.artifactId}</repository>
            <buildArgs>
                <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
            </buildArgs>
        </configuration>
    </plugin>
    

    每个微服务项目根目录下面建立Dockerfile文件

    #FROM java:8
    FROM openjdk:8-jdk-alpine
    # 传入的参数
    ARG JAR_FILE
    COPY ${JAR_FILE} app.jar
    EXPOSE 10020
    ENTRYPOINT ["java","-jar","/app.jar"]
    

    注意每个项目开放端口不一样,注意修改。

    修改jenkinsfile:

    stage('编译,打包微服务工程') {
    	sh "mvn -f ${project_name} clean install dockerfile:build"
    }
    

    打包结果:

    image-20210314183026095

    查看本地镜像:

    docker images
    

    image-20210314184105314

    镜像构建成功

    上传镜像到Harbor仓库

    添加harbor凭证:(我这里账号记错了,harbor默认账号是admin,后面我改回来了)

    image-20210314185246070

    拿到凭证id:

    image-20210314185350017

    生成流水线代码片段:

    image-20210314185811786

    修改Jenkinsfile:

    def git_auth = "ee475541-cfd3-42a8-b8eb-4e8f9ac5c5b7"
    def git_url = "http://192.168.1.50:82/root/tensquare_back.git"
    def tag = "latest"
    //harbor的url
    def harbor_url = "192.168.1.52:85"
    def harbor_project = "tensquare"
    //harbor凭证id
    def harbor_auth = "25ca1748-2a03-4acb-a828-863fe805165c"
     node {
        stage('拉取代码') {
             checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
        }
        stage('代码审查') {
                 //引入全局工具配置中的sonarqube-scanner名
             def scannerHome = tool 'sonar-scanner'
             //引入系统配置中Sonarqube server的名
             withSonarQubeEnv('sonarqube'){
                 sh """
                     cd ${project_name}
                     ${scannerHome}/bin/sonar-scanner
                 """
             }
        }
        stage('编译,安装公共子工程') {
            sh "mvn -f tensquare_common clean install"
        }
        stage('编译,打包微服务工程, 上传镜像') {
            sh "mvn -f ${project_name} clean install dockerfile:build"
            def imageName = "${project_name}:${tag}"
            //镜像打上标签
            sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${imageName}"
            //推送到Harbor
            withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
                //登陆到harbor
                sh "docker login -u ${username} -p ${password} ${harbor_url}"
                //镜像上传
                sh "docker push ${harbor_url}/${harbor_project}/${imageName}"
                echo "镜像推送到harbor成功"
            }
        }
    
     }
    

    部署结果:

    image-20210314191146832

    查看harbor仓库:

    image-20210314191224304

    拉取镜像和发布应用

    把192.168.1.51的公钥发送给192.168.1.73(这里图里面是53服务器,是因为我后来改了ip地址)

    #在192.168.1.51执行
    ssh-copy-id 192.168.1.73
    

    image-20210314194450763

    jenkins安装publish over ssh插件,可以远程发送shell命令,安装完该插件后,配置Publish over SSH

    点击测试

    image-20210314213315112

    jenkins添加输入端口选项:

    image-20210314195932565

    在流水线语法出生成流水线片段:要选择sshPublisher

    image-20210314195056719

    jenkinsfile配置:

    def git_auth = "ee475541-cfd3-42a8-b8eb-4e8f9ac5c5b7"
    def git_url = "http://192.168.1.50:82/root/tensquare_back.git"
    def tag = "latest"
    //harbor的url
    def harbor_url = "192.168.1.52:85"
    def harbor_project = "tensquare"
    //harbor凭证id
    def harbor_auth = "25ca1748-2a03-4acb-a828-863fe805165c"
     node {
        stage('拉取代码') {
             checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
        }
        stage('代码审查') {
                 //引入全局工具配置中的sonarqube-scanner名
             def scannerHome = tool 'sonar-scanner'
             //引入系统配置中Sonarqube server的名
             withSonarQubeEnv('sonarqube'){
                 sh """
                     cd ${project_name}
                     ${scannerHome}/bin/sonar-scanner
                 """
             }
        }
        stage('编译,安装公共子工程') {
            sh "mvn -f tensquare_common clean install"
        }
        stage('编译,打包微服务工程, 上传镜像') {
            def imageName = "${project_name}:${tag}"
            //打包、构建镜像
            sh "mvn -f ${project_name} clean install dockerfile:build"
            //镜像打上标签
            sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${imageName}"
            //推送到Harbor
            withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
                //登陆到harbor
                sh "docker login -u ${username} -p ${password} ${harbor_url}"
                //镜像上传
                sh "docker push ${harbor_url}/${harbor_project}/${imageName}"
                echo "镜像推送到harbor成功"
            }
            sh "docker rmi ${harbor_url}/${harbor_project}/${imageName}"
            sh "docker rmi ${imageName}"
            sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deploy.sh $harbor_url $harbor_project $project_name $tag $port", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
        }
     }
    

    192.168.1.73服务器上:

    mkdir /opt/jenkins_shell
    vi deploy.sh
    chmod 755 deploy.sh
    

    脚本内容如下:

    #! /bin/sh
    #接收外部参数
    harbor_url=$1
    harbor_project_name=$2
    project_name=$3
    tag=$4
    port=$5
    imageName=$harbor_url/$harbor_project_name/$project_name:$tag
    echo "$imageName"
    #查询容器是否存在,存在则删除
    containerId=`docker ps -a | grep -w ${project_name}:${tag}  | awk '{print $1}'`
    if [ "$containerId" !=  "" ] ; then
        #停掉容器
        docker stop $containerId
        #删除容器
        docker rm $containerId
            echo "成功删除容器"
    fi
    
    #查询镜像是否存在,存在则删除
    imageId=`docker images | grep -w $project_name  | awk '{print $3}'`
    if [ "$imageId" !=  "" ] ; then
        #删除镜像
        docker rmi -f $imageId
            echo "成功删除镜像"
    fi
    
    # 登录Harbor
    docker login -u admin -p Harbor12345 $harbor_url
    # 下载镜像
    docker pull $imageName
    # 启动容器
    docker run -di -p $port:$port $imageName
    echo "容器启动成功"
    

    部署eureka:

    image-20210314200258282

    image-20210314212359426

    部署成功后,访问eureka主页成功

    image-20210314215204843

    #依次手动部署完所有的服务后
    docker images
    

    image-20210314221827128

    部署前端静态网站

    192.168.1.73服务器,安装nginx:

    yum install -y epel-release
    yum install -y nginx
    

    修改nginx默认端口为9090

    vi /etc/nginx/nginx.conf
    

    image-20210314222425528

    关闭selinux:

    #临时更改
    setenforce 0
    #永久更改
    vi /etc/selinux/config
    
    SELINUX=disabled
    

    启动nginx:

    systemctl enable nginx
    systemctl start nginx
    

    访问9090,安装成功

    image-20210314222736760

    jenkins安装nodejs插件后,在全局插件配置nodejs

    image-20210314223831069

    创建前端项目流水线(tensquare_front),参数化项目

    image-20210314224317961

    pipeline script:

    def git_auth = "ee475541-cfd3-42a8-b8eb-4e8f9ac5c5b7"
    def git_url = "http://192.168.1.50:82/root/tensquare_front.git"
    node {
        stage('拉取代码'){
            checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
        }
        stage('打包,部署网站'){
            nodejs('nodejs12') {
               sh '''
                   npm install
                   npm run build
                  '''
           }
            
           sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/usr/share/nginx/html', remoteDirectorySDF: false, removePrefix: 'dist', sourceFiles: 'dist/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
        }
         
    }
    

    第一次部署,会有点慢,最后部署成功!

    image-20210314225505812

    image-20210314230305049

  • 相关阅读:
    vmware fusion vmware tools灰的
    Android Studio Flutter开发测试碰到的问题 Running Gradle task assembleDebug
    eclipse 连接mysql表生成实体类
    png2ico小工具使用
    ionic+vue+capacitor系列笔记常见报错以及解决
    Strapi入门记01创建项目,账户,测试表,测试接口
    ionic+vue+capacitor系列笔记01项目初始化
    ionic+vue+capacitor系列笔记03项目使用Native插件
    ionic+vue+capacitor系列笔记02项目中集成Capacitor,添加android,ios平台,真机运行项目
    微信图片缓存中的 dat 文件处理
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/14534847.html
Copyright © 2020-2023  润新知