微服务持续集成--项目代码上传到Gitlab;从Gitlab拉取项目源码;提交到SonarQube代码审查

一、微服务持续集成(1)——项目代码上传到 Gitlab

在IDEA操作即可,参考之前的步骤。包括后台微服务和前端web网站代码

1、登录到 Gitlab,创建两个新项目:前、后端项目

 

2、先做后端提交,后端项目先提交给本地 git

3、新定义远程仓库地址

4、复制 gitlab中tensquare_back 项目中 http 的 URL

5、提交代码

6、提交完成后,查看 gitlab

之前创建过的 Dockerfile(之前导入进来的) 删掉,重新创建

 

Windows 本地安装 TortoiseGit(小乌龟)用来提交前端项目代码

TortoiseGit(小乌龟)+ 中文汉化包
     TortoiseGit-2.13.0.1-64bit.msi
     TortoiseGit-LanguagePack-2.13.0.0-64bit-zh_CN.msi
链接:https://pan.baidu.com/s/1rY4emhv2JOSwP_OkXPdyPw
提取码:09ho

1、安装 TortoiseGit(小乌龟)

2、安装中文汉化包

桌面鼠标右击会出现

3、右击小乌龟 git 打开设置提交远程URL(前端项目的URL)

 

4、把前端项目 tensquare 推送到 gitlab

5、查看 gitlab 的 tensquare_front 项目

二、微服务持续集成(2)——从Gitlab拉取项目源码

1、新建 item

 Pipeline script:脚本放在Jenkins本地

 Pipeline script from SCM:脚本放在 idea 项目下以一个 Jenkinsfile 文件形式去存在

(1)下面演示一下脚本式的 Jenkinsfile 文件

2、创建 Jenkinsfile 文件

 

把上面复制的模板粘贴Jenkinsfile文件中,不要的内容删掉

点击配置

 

点击配置→流水线语法(在最底部)

点击生成流水线脚本

Jenkinsfile 脚本内容

//git的凭证
def git_auth="015f1ee4-73cf-4d51-95b0-2954fc32aadb"
//git的URL
def git_url="git@192.168.10.10:xxx_group/tensquare_back.git"
node {
    stage('pull code') {
        checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
    }
}

3、提交

4、在 Gitlab上查看

5、开始构建项目

6、在 Jenkins 服务器上查看构建的项目

三、微服务持续集成(3)——提交到SonarQube代码审查

1、创建项目,并设置参数

(1)创建 tensquare_back 项目,添加一个选择参数

配置 → 添加参数 → Choice Parameter

 

project_name

tensquare_eureka_server tensquare_zuul tensquare_admin_service tensquare_gathering 注册中心 服务网关 认证中心 活动微服务

2、每个项目的根目录下添加 sonar-project.properties

这个 sonar-project.properties 脚本文件(导入时就有),它本身是需要自己新建的

tensquare_eureka_server:

# 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

tensquare_zuul

# must be unique in a given SonarQube instance
sonar.projectKey=tensquare_zuul
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=tensquare_zuul
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

 tensquare_admin_service

# must be unique in a given SonarQube instance
sonar.projectKey=tensquare_admin_service
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=tensquare_admin_service
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

tensquare_gathering

# must be unique in a given SonarQube instance
sonar.projectKey=tensquare_gathering
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=tensquare_gathering
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

注意:修改 sonar.projectKey 和 sonar.projectName ,一定要和所在的项目名称一致

3、修改 Jenkinsfile 构建脚本

//git的凭证
def git_auth="015f1ee4-73cf-4d51-95b0-2954fc32aadb"
//git的URL
def git_url="git@192.168.10.10:xxx_group/tensquare_back.git"
node {
    stage('pull code') {
        checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
    }
    stage('check code') {
                //定义SonarQubeScanner工具
                def scannerHome = tool 'sonar-scanner'
                //引用SonarQube系统环境
                withSonarQubeEnv('sonarqube') {
                sh """
                    cd ${project_name}
                    ${scannerHome}/bin/sonar-scanner
                   """
                   }
        }
}

4、提交到 gitlab

5、构建 tensquare_eureka_server 项目进行代码检查

检查结果

6、随后把服务网关、认证中心和活动微服务构建,都进行检查

 

posted @ 2022-03-03 15:31  DoYouWantMore  阅读(487)  评论(0编辑  收藏  举报