二.Jenkins項(xiàng)目構(gòu)建

1. Jenkins項(xiàng)目構(gòu)建類型

  • 自由風(fēng)格軟件項(xiàng)目
  • Maven項(xiàng)目
  • 流水線項(xiàng)目

1.1 自由風(fēng)格軟件項(xiàng)目構(gòu)建

下面演示創(chuàng)建一個(gè)自由風(fēng)格項(xiàng)目來(lái)完成項(xiàng)目的集成過(guò)程:拉取代碼->編譯->打包

  1. 創(chuàng)建項(xiàng)目
image-20210815075032183.png
  1. 配置源碼管理,從gitlab拉取代碼
image-20210815075126202.png
  1. 構(gòu)建->添加構(gòu)建步驟->Executor Shell
echo "開始編譯和打包"
mvn clean package
echo "編譯和打包結(jié)束"
image-20210815075404912.png

1.2 Maven項(xiàng)目構(gòu)建

  1. 安裝 Maven Integration插件
  2. 創(chuàng)建Maven項(xiàng)目
image-20210815075800267.png
  1. 配置項(xiàng)目

拉取代碼的過(guò)程和自由風(fēng)格項(xiàng)目一樣,只是"構(gòu)建"部分不同

image-20210815075922118.png
image-20210815080032305.png

1.3 流水線項(xiàng)目構(gòu)建

  1. 安裝Pipeline插件

  2. 創(chuàng)建流水線任務(wù)

image-20210815080527461.png
  1. pipeline配置

方法一:Pipeline Script

流水線->選擇HelloWorld模板,修改

//stages:代表整個(gè)流水線的所有執(zhí)行階段。通常stages只有1個(gè),里面包含多個(gè)stage
//stage:代表流水線中的某個(gè)階段,可能出現(xiàn)n個(gè)。一般分為拉取代碼,編譯構(gòu)建,部署等階段。
//steps:代表一個(gè)階段內(nèi)需要執(zhí)行的邏輯。steps里面是shell腳本,git拉取代碼,ssh遠(yuǎn)程發(fā)布等任意內(nèi)容。
pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'a8cfc168-b4d4-42f5-a5fc-88ab0101de8f', url: 'git@10.211.55.8:lab202/hello-world-demo.git']]])
            }
        }
        stage('bulid code') {
            steps {
                sh 'mvn clean package'
            }
        }
    }
}

方法二:Pipeline Script from SCM

  1. 在項(xiàng)目根目錄建立Jenkins?le文件,把內(nèi)容復(fù)制到該文件中,上傳gitlab倉(cāng)庫(kù)
image-20210815081518403.png
  1. 在項(xiàng)目中引用該文件
image-20210815082023344.png
image-20210815082038186.png

2. 項(xiàng)目構(gòu)建觸發(fā)器

2.1 常用內(nèi)置項(xiàng)目構(gòu)建觸發(fā)器

  • 觸發(fā)遠(yuǎn)程構(gòu)建
  • 其他工程構(gòu)建后觸發(fā)(Build after other projects are build)
  • 定時(shí)構(gòu)建(Build periodically)
  • 輪詢SCM(Poll SCM)
# 定時(shí)任務(wù)的表達(dá)式
每30分鐘構(gòu)建一次:H代表形參 H/30 * * * * 10:02 10:32

每2個(gè)小時(shí)構(gòu)建一次: H H/2 * * *

每天的8點(diǎn),12點(diǎn),22點(diǎn),一天構(gòu)建3次: (多個(gè)時(shí)間點(diǎn)中間用逗號(hào)隔開) 0 8,12,22 * * *

每天中午12點(diǎn)定時(shí)構(gòu)建一次 H 12 * * *

每天下午18點(diǎn)定時(shí)構(gòu)建一次 H 18 * * *

在每個(gè)小時(shí)的前半個(gè)小時(shí)內(nèi)的每10分鐘 H(0-29)/10 * * * *

每?jī)尚r(shí)一次,每個(gè)工作日上午9點(diǎn)到下午5點(diǎn)(也許是上午10:38,下午12:38,下午2:38,下午4:38) H H(9-16)/2 * * 1-5

2.2 Git hook 自動(dòng)觸發(fā)構(gòu)建

說(shuō)明:利用Gitlab的webhook實(shí)現(xiàn)代碼push到倉(cāng)庫(kù),立即觸發(fā)項(xiàng)目自動(dòng)構(gòu)建。

  1. 在Jenkins中安裝GitlabHook的相關(guān)插件:Gitlab Hook 和 Gitlab
  2. Jenkins設(shè)置實(shí)驗(yàn)Gitlab hook進(jìn)行自動(dòng)構(gòu)建,記錄webhook URL之后配置到gitlab上面
image-20210818101301941.png
  1. Gitlab配置webhook

1)開啟webhook功能

使用root賬戶登錄到后臺(tái),點(diǎn)擊Admin Area -> Settings -> Network。勾選"Allow requests to the local network from web hooks and services" 讓網(wǎng)絡(luò)鉤子允許請(qǐng)求本地網(wǎng)絡(luò)

image-20210818101219587.png

2)在項(xiàng)目添加webhook

點(diǎn)擊項(xiàng)目->Settings->Webhook Settings

image-20210818103620870.png
  1. Jenkins關(guān)閉認(rèn)證功能

在Jenkins中,Manage Jenkins->Con?gure System

image-20210818103727017.png
  1. 測(cè)試webhook
image-20210818104257935.png

2.3 Jenkins參數(shù)化構(gòu)建

有時(shí)在項(xiàng)目構(gòu)建的過(guò)程中,我們需要根據(jù)用戶的輸入 動(dòng)態(tài)傳入一些參數(shù),從而影響整個(gè)構(gòu)建結(jié)果。比如在原來(lái)Jenkinsfile中只指定了master分支,那么用參數(shù)構(gòu)建可以替換分支為dev分支

  1. 新建dev分支,并提交到gitlab

  2. 在Jenkins的設(shè)置里添加字符串類型參數(shù)

image-20210818110851283.png
  1. 修改pipeline的流水線代碼,引用參數(shù)
image-20210818112347596.png
  1. 使用參數(shù)構(gòu)建項(xiàng)目

2.4 配置郵箱服務(wù)器發(fā)送構(gòu)建結(jié)果

  1. 安裝Email Extension插件
  2. 163郵箱開啟smtp服務(wù)
image-20210818113432469.png
  1. 配置Jenkins admin賬號(hào)郵箱
image-20210818133732798.png
  1. 配置 Email擴(kuò)展通知
image-20210818134051027.png
image-20210818134113062.png
  1. 配置 Email通知
image-20210818134147171.png
  1. 測(cè)試是否可以發(fā)送郵件成功
image-20210818134211686.png
  1. 在項(xiàng)目根目錄下編寫 email.html
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次構(gòu)建日志</title>
    </head>
    <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
          offset="0">
        <table width="95%" cellpadding="0" cellspacing="0"
               style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sansserif">
            <tr>
                <td>(本郵件是程序自動(dòng)下發(fā)的,請(qǐng)勿回復(fù)!)</td>
            </tr>
            <tr>
                <td><h2>
                    <font color="#0000FF">構(gòu)建結(jié)果 - ${BUILD_STATUS}</font>
                    </h2></td>
            </tr>
            <tr>
                <td><br />
                    <b><font color="#0B610B">構(gòu)建信息</font></b>
                    <hr size="2" width="100%" align="center" /></td>
            </tr>
            <tr>
                <td>
                    <ul>
                        <li>項(xiàng)目名稱&nbsp;:&nbsp;${PROJECT_NAME}</li>
                        <li>構(gòu)建編號(hào)&nbsp;:&nbsp;第${BUILD_NUMBER}次構(gòu)建</li>
                        <li>觸發(fā)原因:&nbsp;${CAUSE}</li>
                        <li>構(gòu)建日志:&nbsp;<a
                                          href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                        <li>構(gòu)建&nbsp;&nbsp;Url&nbsp;:&nbsp;<a
                                                             href="${BUILD_URL}">${BUILD_URL}</a></li>
                        <li>工作目錄&nbsp;:&nbsp;<a
                                                href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                        <li>項(xiàng)目&nbsp;&nbsp;Url&nbsp;:&nbsp;<a
                                                             href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                    </ul>
                </td>
            </tr>
            <tr>
                <td><b><font color="#0B610B">Changes Since Last
                    Successful Build:</font></b>
                    <hr size="2" width="100%" align="center" /></td>
            </tr>
            <tr>
                <td>
                    <ul>
                        <li>歷史變更記錄 : <a
                                        href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                    </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for
                    Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br
/>%m</pre>",pathFormat="&nbsp;&nbsp;&nbsp;&nbsp;%p"}
                </td>
            </tr>
            <tr>
                <td><b>Failed Test Results</b>
                    <hr size="2" width="100%" align="center" /></td>
            </tr>
            <tr>
                <td><pre
                         style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica,
                    sans-serif">$FAILED_TESTS</pre>
                    <br /></td>
            </tr>
            <tr>
                <td><b><font color="#0B610B">構(gòu)建日志 (最后 100行):</font></b>
                    <hr size="2" width="100%" align="center" /></td>
            </tr>
            <tr>
                <td><textarea cols="80" rows="30" readonly="readonly"
                              style="font-family: Courier New">${BUILD_LOG,
                    maxLines=100}</textarea>
                </td>
            </tr>
        </table>
    </body>
</html>
  1. 修改Jenkinsfile,增加構(gòu)建完成后發(fā)送郵件的功能,并將修改推送到gitlab
pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'a8cfc168-b4d4-42f5-a5fc-88ab0101de8f', url: 'git@10.211.55.8:lab202/hello-world-demo.git']]])
            }
        }
        stage('bulid code') {
            steps {
                sh 'mvn clean package'
            }
        }
    }
    post {
          always {
              emailext(
                  subject: '構(gòu)建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} -${BUILD_STATUS}!',
                  body: '${FILE,path="email.html"}',
                  to: 'yaoqijun@outlook.com'
              )
          }
    }

}
  1. 構(gòu)建測(cè)試,發(fā)現(xiàn)成功發(fā)送郵件
image-20210818135730344.png

3. Jenkins整合SonarQube代碼審查

3.1 SonarKube安裝(macos)

  1. 環(huán)境安裝要求:jdk,mysql
  2. 創(chuàng)建SonarQube的數(shù)據(jù)庫(kù)
create database sonar;
  1. 下載sonar壓縮包
#  https://www.sonarqube.org/downloads/
  1. 解壓sonar,修改配置
# 解壓與移動(dòng)sonar文件
unzip sonarqube-6.4.zip
mv sonarqube-6.4.zip /opt

# 修改配置
vim /opt/sonarqube-6.4/conf/sonar.properties

# sonar.jdbc.username=root  
# sonar.jdbc.password=199748
# sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useCon?gs= maxPerformance&useSSL=false
# 端口默認(rèn)為 9000
# 登錄用戶密碼 默認(rèn)為 admin/admin
  1. sonarQube啟動(dòng)停止操作
cd /opt/sonarqube-6.4

./bin/macosx-universal-64/sonar.sh start # 啟動(dòng)
./bin/macosx-universal-64/sonar.sh stop # 停止
./bin/macosx-universal-64/sonar.sh status # 查看狀態(tài)
  1. 創(chuàng)建token
image-20210819102432390.png

3.2 Jenkins整合SonarQube代碼審查

  1. 安裝SonarQube Scanner插件
  2. Jenkins進(jìn)行SonarQube配置
  1. Manage Jenkins->Con?gure System->SonarQube servers
image-20210819104538059.png
image-20210819104549539.png
  1. Manage Jenkins->Global Tool Con?guration
image-20210819104810649.png
  1. SonaQube關(guān)閉審查結(jié)果上傳到SCM功能
image-20210819105101417.png

3.3 在項(xiàng)目中添加sonarQube

  1. IDEA項(xiàng)目根目錄下,創(chuàng)建sonar-project.properties文件
# must be unique in a given SonarQube instance
sonar.projectKey=hello_world_demo
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=hello_world_demo
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.source=1.8
sonar.java.target=1.8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
  1. 在DEA修改Jenkins?le,加入SonarQube代碼審查階段
pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'a8cfc168-b4d4-42f5-a5fc-88ab0101de8f', url: 'git@10.211.55.8:lab202/hello-world-demo.git']]])
            }
        }
        stage('bulid code') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('SonarQube代碼審查'){
            steps{
                script{
                    scannerHome = tool 'sonarqube-scanner'//tool代表要引入Jenkins的一些工具,'sonarqube-scanner'是之前我們自己起的名字#自由風(fēng)格默認(rèn)會(huì)找全局工具
                }
                withSonarQubeEnv('sonarqube6.4'){
                    //這個(gè)配置在系統(tǒng)配置里
                    sh "${scannerHome}/bin/sonar-scanner"
                }
            }
        }
    }
    post {
          always {
              emailext(
                  subject: '構(gòu)建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} -${BUILD_STATUS}!',
                  body: '${FILE,path="email.html"}',
                  to: 'yaoqijun@outlook.com'
              )
          }
    }

}
  1. 到SonarQube的UI界面查看審查結(jié)果
image-20210819111116871.png

4. Harbor安裝(arm64)

  1. 安裝docker并啟動(dòng)
  2. 安裝docker-compose
apt install python3-pip
pip -V
pip3 install docker-compose
docker-compose -version
  1. harbor安裝
git clone https://github.com/fphub/harbor-arm64.git

# 修改配置文件
# 修改hostname和port 
vim make/harbor.yml
hostname: 192.168.66.102
port: 85

make package_offline -e VERSIONTAG=v1.9.3 PKGVERSIONTAG=v1.9.3 UIVERSIONTAG=v1.9.3 DEVFLAG=false CLAIRFLAG=true

cd make
./prepare
./install.sh
  1. harbor操作
docker-compose up -d #啟動(dòng)

docker-compose stop #停止

docker-compose restart #重新啟動(dòng)
  1. 訪問(wèn)
訪問(wèn)Harbor http://10.211.55.10:85
默認(rèn)賬戶密碼:admin/Harbor12345
  1. 把鏡像上傳Harbor和拉取Harbor鏡像
# 在harbor上創(chuàng)建名為springcloud-demo的項(xiàng)目,以及yorick用戶,并給予yorick用戶操作該項(xiàng)目的權(quán)利

# 給鏡像打上標(biāo)簽
docker tag hello-world:latest 10.211.55.10:85/springcloud-demo/hello-world:latest

# 把Harbor地址加入到Docker信任列表
vim /etc/docker/daemon.json
{
        "registry-mirrors": ["https://registry.docker-cn.com"],
        "insecure-registries": ["10.211.55.10:85"]
}

# 重啟docker服務(wù)
systemctl restart docker
# 重啟harbor
docker-compose restart

# 登錄Harbor
docker login -u 用戶 -p 密碼 192.168.66.102:85

# 推送鏡像
docker push 10.211.55.10:85/springcloud-demo/hello-world:latest

# 拉取鏡像(拉取鏡像的服務(wù)器也要修改配置,然后重啟docker)
docker pull 10.211.55.10:85/springcloud-demo/hello-world:latest
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容