• 持续集成/持续部署(2)Jenkins & SonarQube


    jenkins

    安装jenkins

    第一步: 安装java环境
    使用rpm包安装

    [root@jenkins ~]# ll
    total 166040
    -rw-r--r-- 1 root root 170023183 Aug 14  2018 jdk-8u181-linux-x64.rpm
    [root@jenkins ~]# rpm -ivh jdk-8u181-linux-x64.rpm 
    warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:jdk1.8-2000:1.8.0_181-fcs        ################################# [100%]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    或者使用yum -y install java

    第二步: 下载上传jenkins安装包
    https://mirrors.jenkins-ci.org/redhat/

    第三步: 安装jenkins

    [root@jenkins ~]# rpm -ivh jenkins-2.176.1-1.1.noarch.rpm 
    warning: jenkins-2.176.1-1.1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID d50582e6: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:jenkins-2.176.1-1.1              ################################# [100%]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    修改启动jenkins的用户为root

    [root@jenkins ~]# grep JENKINS_USER /etc/sysconfig/jenkins
    JENKINS_USER="root"
    
    • 1
    • 2

    第四步: 启动jenkins 加入开机自动运行

    [root@jenkins ~]# systemctl start jenkins
    [root@jenkins ~]# systemctl enable jenkins
    jenkins默认端口为8080
    [root@jenkins ~]# netstat -tnulp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1275/master         
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1192/sshd           
    tcp6       0      0 ::1:25                  :::*                    LISTEN      1275/master         
    tcp6       0      0 :::8080                 :::*                    LISTEN      2443/java   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    通过页面配置登录jenkins
    在这里插入图片描述

    [root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
    24ca0fe2a923400b8c4f635da34bc865
    
    • 1
    • 2

    在这里插入图片描述
    在这里插入图片描述

    修改jenkins的登录密码
    在这里插入图片描述

    在这里插入图片描述

    用户名 admin 密码 123

    插件管理 修改为国内仓库

    在这里插入图片描述

    jenkins安装插件

    命令行上传插件压缩包

    [root@jenkins plugins]# ll
    total 283056
    -rw-r--r-- 1 root root 289849332 Oct 26  2019 jenkins_plugins.tar.gz
    [root@jenkins plugins]# tar xf jenkins_plugins.tar.gz 
    [root@jenkins plugins]# mv plugins/* .
    
    • 1
    • 2
    • 3
    • 4
    • 5

    重启重新加载插件

    [root@jenkins plugins]# systemctl restart jenkins
    
    • 1

    jenkins创建一个测试项目

    在这里插入图片描述
    创建一个自由风格的项目
    在这里插入图片描述

    在这里插入图片描述
    执行shell命令测试当前的路径
    在这里插入图片描述
    构建执行项目:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    注意: jenkins的家目录下默认是没有workspace工作目录

    创建项目后自动生成项目的家目录

    [root@jenkins freestyle-game]# pwd
    /var/lib/jenkins/workspace/freestyle-game
    
    • 1
    • 2

    下载gitlab的代码到jenkins的本地

    配置jenkins
    在这里插入图片描述

    配置好后无法拉取代码

    第一步: 在201jenkins服务器做hosts解析

    [root@jenkins ~]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    10.0.0.200 gitlab.ahui.com
    
    • 1
    • 2
    • 3
    • 4

    第二步: jenkins生成秘钥对

    [root@jenkins ~]# ssh-keygen 
    
    • 1

    第三步: 将jenkins的公钥放到gitalb的root管理员

    [root@jenkins ~]# cat .ssh/id_rsa.pub 
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9B2FRh0BQEz1g1sGFW86+Z2xyNnuffhTXj4gilHIEmRsmi5jMbGQjr5PbAMPA9fD48fEBrgAemD09FRiRvsEQeFK/1M1wtGrOmthBW+fwwQsu58AOIvmVvlTBvZRq0T1BHC9q2tVSRFGBwulE/YQxAs9v3LYBR/20k/ECzOOjoVn1oIZMewVmGraIt/KZVxRHQdmU22N3Q4Xa+a4htAqCu2q8MUq0i+rbeCfaLSldxsLDh59YzLahdKekl1wfo3PlB/B8iaMguWthpYAgAAlGyOXLznmvluPZS2g4gj2sNUTAfzKmE7oo+IUKuRqv+i/UXo9UnstUB5oG5PY8VurX root@jenkins
    
    • 1
    • 2

    在这里插入图片描述
    在jenkins的命令行clone项目测试

    [root@jenkins ~]# git clone git@gitlab.ahui.com:test/game.git
    Cloning into 'game'...
    remote: Counting objects: 101, done.
    remote: Compressing objects: 100% (99/99), done.
    remote: Total 101 (delta 5), reused 0 (delta 0)
    Receiving objects: 100% (101/101), 7.35 MiB | 2.46 MiB/s, done.
    Resolving deltas: 100% (5/5), done.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意: 只有在命令行能下载成功 然后才可以配置到页面上

    在这里插入图片描述

    最后检查jenkins的项目目录下是否获取了gitlab game的项目代码

    [root@jenkins ~]# cd /var/lib/jenkins/workspace/freestyle-game/
    [root@jenkins freestyle-game]# ll
    total 48
    -rw-r--r-- 1 root root 28032 Aug 10 10:41 bgm.mp3
    drwxr-xr-x 2 root root    23 Aug 10 10:41 css
    drwxr-xr-x 2 root root    23 Aug 10 10:41 images
    -rw-r--r-- 1 root root  8957 Aug 10 10:41 index.html
    drwxr-xr-x 2 root root   213 Aug 10 10:41 js
    drwxr-xr-x 2 root root  4096 Aug 10 10:41 roms
    -rw-r--r-- 1 root root   811 Aug 10 10:41 shuoming.html
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    将代码通过shell推送到web服务器

    第一步: jenkins和web服务器做免秘钥认证

    [root@jenkins ~]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.7
    
    • 1

    第二步: 配置jenkins执行shell命令推送代码
    在这里插入图片描述

    点击立即构建测试web服务器代码是否发生变化

    需求: 如果发布到web服务器出现问题 想回滚到上一个版本
    利用软连接方式做回滚操作

    web服务器配置文件

    [root@test conf.d]# cat test.conf 
    server {
    	listen 80;
    	server_name _;
    	
    	location / {
    	root /code/html;
    	index index.html index.htm;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    jenkins脚本

    [root@jenkins scripts]# cat test.sh
    #!/bin/bash
    #变量配置
    ip=10.0.0.7
    #打包代码
    tar zcf /tmp/game_${BUILD_ID}.tar.gz ./*
    #拷贝代码到web服务器
    scp /tmp/game_${BUILD_ID}.tar.gz $ip:/code/
    #在web服务器上创建唯一的目录
    ssh $ip "mkdir /code/game_${BUILD_ID}"
    #解压代码到目录中并且删除压缩包
    ssh $ip "tar xf /code/game_${BUILD_ID}.tar.gz -C  /code/game_${BUILD_ID};rm -rf /code/game_${BUILD_ID}.tar.gz"
    #删除默认的html目录 做软连接到当前版本目录
    ssh $ip "cd /code;rm -rf html; ln -s game_${BUILD_ID} html"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    配置jenkins触发器

    在这里插入图片描述

    如果开发有提交代码到master或者分支合并到master则自动触发jenkins项目:

    自动拉取代码并且执行脚本
    在这里插入图片描述在这里插入图片描述

    配置gitlab 触发的时候拿着jenkins的钥匙
    在这里插入图片描述
    在这里插入图片描述

    触发配置完毕:

    测试: 开发写代码提交到master主干或者分支合并到master主干----> 直接通过浏览器查看结果即可

    SonarQube

    1.安装jdk环境
    上传rpm包

    [root@sonar ~]# rpm -ivh jdk-8u181-linux-x64.rpm 
    warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:jdk1.8-2000:1.8.0_181-fcs        ################################# [100%]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.安装数据库

     wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
     rpm -ivh mysql80-community-release-el7-3.noarch.rpm
    
    • 1
    • 2

    修改仓库的版本:

    [root@sonar ~]# vim /etc/yum.repos.d/mysql-community.repo 
    name=MySQL 5.6 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
    enabled=1			 #开启5.6
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    ......
    ......
    name=MySQL 8.0 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
    enabled=0			#关闭8.0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    安装5.6数据库并启动

    [root@sonar ~]# yum install mysql-community-server -y
    [root@sonar ~]# systemctl start mysqld
    
    • 1
    • 2

    设置数据库密码

    [root@sonar ~]# mysqladmin -uroot passwd 123
    
    • 1

    创建sonar库

    [root@sonar ~]# mysql -uroot -p123 -e "CREATE DATABASE sonar DEFAULT CHARACTER SET utf8;"
    [root@sonar ~]# mysql -u root -p123 -e "show databases"
    
    • 1
    • 2

    3.安装sonarqube
    上传soanrqube 安装包
    解压到/usr/local目录并修改名称

    [root@sonar ~]# unzip sonarqube-7.0.zip -d /usr/local/
    [root@sonar ~]# mv /usr/local/sonarqube-7.0/ /usr/local/sonarqube
    
    • 1
    • 2

    配置sonar连接数据库

    [root@sonar ~]# vim /usr/local/sonarqube/conf/sonar.properties
    
    • 1

    在这里插入图片描述

    创建soanr普通用户

    [root@sonar ~]# useradd sonar
    
    • 1

    授权代码文件属主属组

    [root@sonar ~]# chown -R sonar.sonar /usr/local/sonarqube/
    
    • 1

    启动soanrqube服务 注意必须使用普通用户启动

    [root@sonar ~]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"
    Starting SonarQube...
    Started SonarQube.
    
    • 1
    • 2
    • 3

    检查端口查看是否启动(sonarqbue默认端口为9000)

    [root@sonar ~]# netstat -tnulp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1201/sshd           
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1349/master         
    tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      4383/java           
    tcp6       0      0 :::9000                 :::*                    LISTEN      4521/java           
    tcp6       0      0 127.0.0.1:9001          :::*                    LISTEN      4405/java           
    tcp6       0      0 :::3306                 :::*                    LISTEN      3004/mysqld         
    tcp6       0      0 :::22                   :::*                    LISTEN      1201/sshd           
    tcp6       0      0 ::1:25                  :::*                    LISTEN      1349/master  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    sonarqbue日志文件

    [root@sonar ~]# tailf /usr/local/sonarqube/logs/sonar.log
    
    • 1

    最后通过浏览器访问10.0.0.202:9000 登录sonarqube

    默认用户名称: admin
    默认密码: admin

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    安装sonarqub插件

    上传soanr插件压缩包

    [root@sonar plugins]# cd /usr/local/sonarqube/extensions/plugins
    [root@sonar plugins]# rm -rf *		#删除所有自带插件
    [root@sonar plugins]# rz			#上传插件包
    [root@sonar plugins]#  tar xf sonar_plugins.tar.gz 	#解压
    
    • 1
    • 2
    • 3
    • 4

    解压后重启sonar

    [root@sonar plugins]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"
    
    • 1

    在jenkins安装sonar客户端

    上传客户端rpm包

    [root@jenkins ~]# unzip sonar-scanner-cli-4.2.0.1873-linux.zip
    [root@jenkins ~]# mv sonar-scanner-4.2.0.1873-linux/ /usr/local/sonar
    [root@jenkins ~]# tail -1 /etc/profile
    export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sonar/bin/"
    [root@jenkins ~]# source /etc/profile
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在命令行进入代码目录执行扫描

    [root@jenkins ~]# cd /var/lib/jenkins/workspace/freestyle-game
    sonar-scanner \
      -Dsonar.projectKey=html \
      -Dsonar.sources=. \
      -Dsonar.host.url=http://10.0.0.202:9000 \
      -Dsonar.login=f254aa77ea29dead6ac3f9990052cab3f3809918
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    jenkins集成sonar

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    告诉jenkins sonar客户端的位置
    在这里插入图片描述

    配置命令

    sonar.projectName=${JOB_NAME}    # 在soanr服务端显示的名称
    sonar.projectKey=html			 # 自定义的key值
    sonar.sources=.					 # 代码所在的位置
    
    • 1
    • 2
    • 3

    在这里插入图片描述

  • 相关阅读:
    RFID基础知识,都学会了吗
    Web篇_01 了解web开发
    SpringBoot:Invalid bound statement (not found)的原因和解决方案
    【postgresql基础入门】 新建数据库服务集群目录,定制属于自己的独享数据区
    如何在 Jenkins CI/CD 流水线中保护密钥?
    Rocketmq学习3——消息发送原理源码浅析
    Go内置函数make和new的区别?
    docker for windonws--Windows 10 家庭中文版安装apache zeppelin 0.10.1版本及安全配置
    180页100+题15W+字解析的《Java高级面试指南》,果断收下
    【微信小程序】使用uni-app——开发首页搜索框导航栏(可同时兼容APP、H5、小程序)
  • 原文地址:https://blog.csdn.net/AHui_CSDN/article/details/126271563