Jenkins持续集成Gitlab

环境准备

部署好的Jenkins不懂得小伙伴可查看jenkins部署流程:http://www.zhanghaobk.com/archives/jenkins-an-zhuang-liu-cheng
部署好的Gitlab,不懂得小伙伴可参考Gitlab部署以及Gitlab进:
http://www.zhanghaobk.com/archives/gitlab-bu-shu-liu-cheng #部署流程
http://www.zhanghaobk.com/archives/gitllab-jin-jie-shi-yong #Gitlab进阶使用

登录Jenkins,下载插件

1、主节点点击“”“Manage Jenkins”

image-1677658942946

2、点击管理插件“Manage Plugins”

image-1677659071888

3、点击安装插件“Available plugins”,然后在搜索框输入“git”,将git的插件进行安装。(注意这里为了方便查看,本文在“Installed plugins”展示需要暗转给的git所有插件)

image-1677659610924
image-1677660041004

4、下载插件并启动服务,安装完后重新刷新页面

image-1677659840276
image-1677660138597

登录Gitlab设置ssh以及token

1、点击右上方用户图标,再选择“Edit profile”

image-1677660881819

2、点击左侧菜单栏中的“Access Tokens”

image-1677660998050

3、填写创建token信息,然后点击创建token

image-1677661453513

4、复制token,并将token进行保存

image-1677661683700

Jenkins添加Gitlab的token

Jenkins添加凭据

1、点击右上角admin账户

image-1677727004686

2、点击“Credentails”

image-1677727096194

3、点击全局的“System”

image-1677727198626

4、点击“Global Credentails(unrestricten)”

image-1677727430804

5、点击“Add Credentails”

image-1677727735436

6、填写连接Gitlab信息,填写完成后点击“Create”创建,凭据创建完成

image-1677736263586

Jenkins添加连接gitlab配置

1、在主界面点击“Manage Jenkins”

image-1677736658198

2、点击“Configure System”

image-1677736778936

3、找到配置Gitlab区域

image-1677736843171

4、配置连接Gitlab的信息

image-1677737053821

5、点击测试连接按钮“Test Connection”,测试成功后点击保存

image-1677743242611

Jenkins新建视图

1、主界面点击“新建item”

image-1677737356689

2、填写任务名称,再点击Pipeiline模式,再点击确定

image-1677737499357

3、Gitlab连接地址选择刚才配置Gitlab的名称

image-1677743562636

4、编写pipeline流水线脚本,编写完成后点击保存
node{
    stage ('pull') {
        checkout([$class: 'GitSCM',
        branches: [[name: 'master']],
        userRemoteConfigs: [[credentialsId: 'jenkins-ggitlab-token',
        url: 'http://192.168.1.61:5000/test-one-test1/test1-project.git']]])
    }
}

image-1677743824249

5、点击构建项目

image-1677743947594

6、查看项目构建过程,并查看控制台输出

image-1677744007844
image-1677744066125
image-1677744149247

Jenkins持续集成go build

jenkins主机部署go环境

下载go环境包

wget https://storage.googleapis.com/golang/go1.17.2.linux-amd64.tar.gz

部署go环境

tar zxvf go1.17.2.linux-amd64.tar.gz
mv go /usr/local/

添加go环境变量

vi /etc/profile
export GO111MODULE=on
export GOPROXY=https://goproxy.io
export GOROOT=/usr/local/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin:$GOBIN

刷新profile配置文件,并验证环境

source /etc/profile
go version

image-1677936233511

准备一段go代码,下面作为示例演示

cat test.go

package main

import (
        "fmt"
        "github.com/zhang19523zhao/zhfmt"
)

func main() {
        s1 := []string{"zh", "21", "golang"}
        s2 := []string{"zh", "21", "python"}
        r1, _ := zhfmt.Zhfmt(s1, "|", 1, 5)
        r2, _ := zhfmt.Zhfmt(s2, "|", 1, 5)
        fmt.Print(r1)
        fmt.Print(r2)
}

推送至Gitlab仓库

克隆仓库

#如果没有克隆仓库地址,注意先克隆仓库
git clone http://192.168.1.61:5000/test-one-test1/test1-project.git  #此地址在不本机暴部署的gitlab上查看,上面已经讲解过,这里不在演示地址获取

进入克隆仓库目录

cd /home/test1/test1-project/

将上面的test.go放入仓库目录下

image-1677822777775

推送至Gitlab代码仓库

git checkout master   #切换master分支
git add . #提交至缓存区
git commit -m '测试构建test.go' #添加注释
git push origin master  #推送至master分支

Jenkins编写pipeline流水线,构建test.go

添加pipeline脚本如下,添加完成后点击保存

node{
        stage ('pull') {
            checkout([$class: 'GitSCM',
            branches: [[name: 'master']],
            userRemoteConfigs: [[credentialsId: 'jenkins-ggitlab-token',
            url: 'http://192.168.1.61:5000/test-one-test1/test1-project.git']]])
        }
        echo "+++++++++++++++++++pull success++++++++++++++++++"
        stage('build') {
            sh 'rm -rf *.mod *.sum'
            //sh '/usr/local/go/bin/go  env -w GO111MODULE=on'  #首次构建需要添加加速,可以在linux服务器上执行,也可以这里首次执行关闭注释,之后的构建再将此行与下面一行进行关闭
            //sh '/usr/local/go/bin/go env -w GOPROXY=https://goproxy.cn,direct'
            sh 'pwd'
            sh '/usr/local/go/bin/go mod init test'
            sh '/usr/local/go/bin/go mod tidy'
            sh '/usr/local/go/bin/go build *.go'
        }
        echo "++++++++++++++++++++build success++++++++++++++"
}

image-1677823108477

点击构建“Build Now”

image-1677823210327

查看构建结果

点击构建列表,找到对应的构建时间,查看控制台输出

image-1677823278615
image-1677823793916

Jenkins持续集成sonar

Jenkins主机部署sonar scanner

现在客户端sonar scanner客户端扫描工具

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.7.0.2747-linux.zip

解压客户端软件包

unzip sonar-scanner-cli-4.7.0.2747-linux.zip
mv sonar-scanner-4.7.0.2747-linux  /usr/loca

执行程序做连接

ln -s /usr/local/sonar-scanner/bin/sonar-scanner

sonarqube如何创建用户、群组、分配权限、创建项目请参考如下地址

http://www.zhanghaobk.com/archives/sonar-jin-jie-shi-yong

Jenkins安装sonarqube插件

1、点击“Manage Jenkins--->点击“Manage Plugins”

image-1678092508866
image-1678092533979

2、点击“Available plugins”,搜索sonar,勾选如下图的插件,点击安装

image-1678092164594
image-1678092243744

sonarqube生成token

1、点击配置、点击权限,选择用户

image-1678092869368

2、点击令牌

image-1678092943261

3、填写令牌信息,点击生成

image-1678093006736

4、将令牌的信息第一时间进行保存,后面jenkins需要这个令牌,保存完后点击完成

image-1678093126793

Jenkins添加sonarqube令牌

1、点击首页的“Manage Jenkins”

image-1678152160824

2、点击“Manage credentials”

image-1678152235651

3、点击“System”

image-1678152285506

4、点击“Gloable credentials(unrestricted)”

image-1678152342684

5、点击“Add Credentails”

image-1678152476645

6、填写凭据信息,填写完成后点击create

image-1678153385985

Jenkins添加sonaruqbe服务信息

1、点击首页的“Manage Jenkins”

image-1678152160824

2、点击“Manage credentials”

image-1678154290095

3、找到“SonarQube servers”区域

image-1678154339122

4、勾选对应配置,并点击“Add SonarQube”

image-1678154414504

5、填写信息后点击保存

image-1678154636597

添加sonar scanner客户端工具配置信息

1、点击首页的“Manage Jenkins”

image-1678152160824

2、点击“Global Tools configuration”

image-1678158644041

3、找到SonarQube Scanner区域,点击“新增SonarQube Scanner”

image-1678158799970

4、填写客户端信息

image-1678159128164

5、Jenkins增加pipeline脚本扫描,点击刚才持续继承gitlab闯将的项目,我的为“test-link-gitlab”

image-1678159376540

7、点击配置

image-1678159462258

3、pipeline增加扫描脚本,如下:
node{
        stage ('pull') {
            checkout([$class: 'GitSCM',
            branches: [[name: 'master']],
            userRemoteConfigs: [[credentialsId: 'jenkins-ggitlab-token',
            url: 'http://192.168.1.61:5000/test-one-test1/test1-project.git']]])
        }
        echo "+++++++++++++++++++pull success++++++++++++++++++"
        stage('sonar') {
            withSonarQubeEnv() {
                sh "/usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=test1-project"  //注意这里的“-Dsonar.projectKey=”是sonarqube创建的项目对应的项目标识,不是项目名称,
               // sh "/usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=test1-project -Dsonar.host.url=http://192.168.1.60:9000 -Dsonar.login=squ_edbe4b1882ad8014d44cbf2f477b5453c1b25308"    //“-Dsonar.projectKey=”参数指定sonar创建项目的项目标识,“-Dsonar.host.url=” //指定sonarqube的地址,“-Dsonar.login=”指定sonar生成token令牌,如果jenkins未设置sonarqube的配置信息,这里就需要把信息写全,如果配置了则只需要指定项目标识即可
                
            }
        }
        echo "++++++++++++++++++++soner success++++++++++++++"
        stage('build') {
            sh 'rm -rf *.mod *.sum'
            //sh '/usr/local/go/bin/go  env -w GO111MODULE=on'
            //sh '/usr/local/go/bin/go env -w GOPROXY=https://goproxy.cn,direct'
            sh 'pwd'
            sh '/usr/local/go/bin/go mod init test'
            sh '/usr/local/go/bin/go mod tidy'
            sh '/usr/local/go/bin/go build *.go'
        }
        echo "++++++++++++++++++++build success++++++++++++++"
}
2、改完上面的pipeline后点击保存

构建验证扫描过程

1、点击立即构建“Build Now”

image-1678168183400

2、点击构建后,查看构建过程

image-1678168299094

3、点击控制台输出“Console output”

image-1678168382657

4、j就可以看到整个构建的过程从拉取代码,到代码扫描,再到代码编译

image-1678168468846
image-1678168498238
image-1678168522477
image-1678168575430
image-1678168616519

5、构建完成后同时在Jenkins上会出现SonarQube的对应项目连接按钮,如下图指向位置,点击此按钮可跳转sonarqube界面

image-1678168734001

6、登录sonarqube,就可以看到代码扫描结果

image-1678168909045
image-1678168885132