文章目录
- 一、项目代码上传到 Gitlab
- 1. 创建前后端 Gitlab 项目
- 2. 提交后端代码
- 3. 提交前端代码
- 二、从 Gitlab 拉取项目源码
- 1. Jenkins 创建 project
- 2. 修改原来的 Jenkinsfile 文件
- 3. 开始构建
- 三、提交到 SonarQube 代码审查
- 1. tensquare_back 项目添加一个选择参数
- 2. 每个项目的根目录下添加 sonar-project.properties
- 3. 修改 Jenkinsfile
- 4. 构建
一、项目代码上传到 Gitlab
微服务分为前端后端,分别提交到两个 Gitlab 项目。
1. 创建前后端 Gitlab 项目
2. 提交后端代码
新定义远程仓库地址,如果有老地址,先删除
提交完成后,查看 gitlab
3. 提交前端代码
- TortoiseGit-2.13.0.1-64bit.msi(小乌龟)
- TortoiseGit-LanguagePack-2.13.0.0-64bit-zh_CN.msi(汉化工具)
Windows 本地安装 TortoiseGit(小乌龟)用来提交前端项目代码
安装中文汉化工具
小乌龟修改为中文
小乌龟 git 打开设置提交远程 URL(前端项目的 URL)
在 windows 本地找到前端项目
右击选择 Git 同步
复制项目地址
查看前端项目
二、从 Gitlab 拉取项目源码
1. Jenkins 创建 project
2. 修改原来的 Jenkinsfile 文件
当然也可以删除重建。
这里只演示拉取代码步骤。
//git 的凭证
def git_auth="0b127895-eb97-4f8f-b471-1277e5549b54"
//git 的 URL
def git_url="git@192.168.10.20:test-group/tensquare_back.git"
node {
stage('pull code') {
//切换成变量,字符串符号使用双引号
checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
}
}
提交 Jenkinsfile 代码
3. 开始构建
在 Jenkins 服务器上查看构建项目
[root@c7-2 ~]#cd /var/lib/jenkins/workspace/
[root@c7-2 /var/lib/jenkins/workspace]#ls
pre_project tensquare_back tensquare_back@tmp web_demo_freestyle web_demo_freestyle@tmp web_demo_pipeline web_demo_pipeline@tmp
三、提交到 SonarQube 代码审查
1. tensquare_back 项目添加一个选择参数
2. 每个项目的根目录下添加 sonar-project.properties
eureka
# 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
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
admin
# 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
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
提交。
3. 修改 Jenkinsfile
//git 的凭证
def git_auth="0b127895-eb97-4f8f-b471-1277e5549b54"
//git 的 URL
def git_url="git@192.168.10.20:test-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. 构建
4 个分别构建一遍,查看 sonarqube。