1. 程式人生 > 其它 >微服務持續整合--專案程式碼上傳到Gitlab;從Gitlab拉取專案原始碼;提交到SonarQube程式碼審查

微服務持續整合--專案程式碼上傳到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="[email protected]: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="[email protected]: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、隨後把服務閘道器、認證中心和活動微服務構建,都進行檢查