部署Jenkins+GitLab+Maven+Nexus+SonarQube持續集成環境

1、相關概念介紹:html

持續(Continuous):不斷地獲取反饋、響應反饋java

集成(Integration):編譯、打包、測試node

部署(Deployment):應用組件或基本設施的代碼或配置變動在產品環境生效mysql

發佈(Release):具備業務影響的功能變化對最終用戶可見linux

交付(Delivery):能夠理解爲從DeploymentRelease之間的階段,強調的是一種能力,開發有能力頻繁部署,業務有能力隨時發佈nginx

image.png

Jenkins:開源的、可擴展的、基於Web界面的持續集成平臺git

Git:開源的分佈式版本控制系統github

GitLab:用於倉庫管理系統的開源項目,使用Git做爲代碼管理工具,並在此基礎上搭建起來的Web服務web

Maven:基於項目對象模型(POM)的項目管理及自動構建工具,用於編譯源碼並打包sql

NexusMaven私服

image.png

SonarQube:用於代碼質量管理和檢測的開源平臺

Node.js:基於Chrome V8引擎的JavaScript運行環境

ZrLog:開源Java博客系統(編譯打包發佈後爲zrlog-2.1.3.war


2、準備工做(3個節點都須要執行以下操做):

一、演示環境:

IP

操做系統

主機名

部署軟件包

192.168.1.144

CentOS 7.6 x86_64

node1

JDKjdk-8u221-linux-x64.tar.gz

Node.jsnode-v12.10.0-linux-x64.tar.xz

Gitgit-2.23.0.tar.xz

Mavenapache-maven-3.6.2-bin.tar.gz

Nexusnexus-3.18.1-01-unix.tar.gz,端口8081,用戶名:admin,密碼:root@123)

Jenkins2.176.3yum方式安裝,端口8080,用戶名:root,密碼:root@123)

192.168.1.145

CentOS 7.6 x86_64

node2

GitLab12.2.5yum方式安裝,端口8000,用戶名:root,密碼:root@123)

192.168.1.146

CentOS 7.6 x86_64

node3

JDKjdk-8u221-linux-x64.tar.gz

Tomcatapache-tomcat-8.5.45.tar.gz,端口8080,用戶名:root,密碼:root@123)

MySQL5.7.27yum方式安裝,端口3306,用戶名:root,密碼:123456)

SonarQubesonarqube-7.7.zip,端口9000,用戶名:admin,密碼:root@123)

演示目標:修改本地ZrLog項目的源碼,經過Git提交至GitLab,若是推送master分支的代碼會觸發Jenkins自動使用Maven構建項目,而推送dev分支的代碼則不會觸發Jenkins使用Maven構建項目,項目構建完成後經過SonarQube對代碼進行質量管理和檢測,最終發佈至遠程節點的Tomcat中自動解壓執行

二、關閉SELinuxfirewalld

三、配置epel

四、配置節點時間同步

五、配置主機名

六、配置/etc/hosts文件:

# vim /etc/hosts

192.168.1.144 node1

192.168.1.145 node2

192.168.1.146 node3

七、下載所需軟件包:

(1)JDKhttps://www.oracle.com/technetwork/java/javase/downloads/index.html

(2)Node.jshttp://nodejs.cn/download/

(3)Githttps://mirrors.edge.kernel.org/pub/software/scm/git/

(4)Mavenhttp://maven.apache.org/download.cgi

(5)Nexushttps://www.sonatype.com/download-oss-sonatype

(6)Tomcathttp://tomcat.apache.org/

(7)SonarQubehttps://www.sonarqube.org/downloads/

8ZrLoghttps://github.com/*4fzb/zrlog

備註:因爲51CTO違禁詞規則,請將上述*換成9,再進行訪問


3、3個節點分別部署對應軟件包:

一、node1node3節點分別部署JDK

(1)解壓JDK

# tar -xf jdk-8u221-linux-x64.tar.gz -C /usr/local

# cd /usr/local

# ln -sv jdk1.8.0_221 jdk

(2)配置JDK環境變量:

# vim /etc/profile.d/jdk.sh

export JAVA_HOME=/usr/local/jdk

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export PATH=$JAVA_HOME/bin:$PATH

# . /etc/profile.d/jdk.sh

# echo $JAVA_HOME

image.png

image.png

(3)查看JDK版本信息:# java -version

image.png

image.png

二、node1節點部署Node.js

(1)解壓Node.js

# tar -xf node-v12.10.0-linux-x64.tar.xz -C /usr/local

# cd /usr/local

# ln -sv node-v12.10.0-linux-x64 node

(2)配置Node.js環境變量:

# vim /etc/profile.d/node.sh

export NODE_HOME=/usr/local/node

export PATH=$NODE_HOME/bin:$PATH

export NODE_PATH=$NODE_HOME/lib/node_modules

# . /etc/profile.d/node.sh

# echo $NODE_HOME

image.png

(3)查看Node.js版本信息:# node -v

image.png

備註:若是不安裝Node.js,以後在構建ZrLog項目,SonarQube Scanner執行掃描時會提示「ERROR: Failed to get Node.js version. No CSS files will be analyzed.

三、node1節點部署Git

(1)安裝依賴軟件包:

# yum -y install gcc perl-ExtUtils-MakeMaker curl-devel expat-devel gettext-devel openssl-devel zlib-devel

(2)編譯安裝Git

# tar -xf git-2.23.0.tar.xz -C /usr/src

# cd /usr/src/git-2.23.0

# ./configure --prefix=/usr/local/git-2.23.0

# make && make install

# cd /usr/local

# ln -sv git-2.23.0 git

(3)配置Git環境變量:

# vim /etc/profile.d/git.sh

export PATH=/usr/local/git/bin:$PATH

# . /etc/profile.d/git.sh

(4)查看Git版本信息:# git --version

image.png

備註:yum方式安裝的Git版本爲1.8.3,版本過低

(5)建立本地用於存放ZrLog源碼的目錄:# mkdir -pv /projects

(6)初始化Git倉庫,並克隆ZrLog源碼:

# cd /projects

# git init

image.png

# git clone https://github.com/*4fzb/zrlog.git

備註:因爲51CTO違禁詞規則,請將上述*換成9,再進行代碼克隆

image.png

# ls -a

image.png

# ls -a .git

image.png

# ls -a zrlog

image.png

四、node2節點部署GitLab

(1)建立清華大學開源軟件鏡像站的yum倉庫,下載速度快:

# vim /etc/yum.repos.d/gitlab-ce.repo

[gitlab-ce]

name=GitLab CE Repository

baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/

gpgcheck=0

enabled=1

# yum clean all

# yum makecache

# yum repolist

(2)安裝GitLab# yum -y install gitlab-ce

image.png

(3)修改gitlab.rb配置文件:

# vim /etc/gitlab/gitlab.rb

註釋代碼:external_url 'http://gitlab.example.com'

新增代碼:external_url 'http://192.168.1.145:8000'

新增代碼:nginx['listen_port'] = 8000

# gitlab-ctl reconfigure

image.png

image.png

# gitlab-ctl status

image.png

# systemctl status gitlab-runsvdir.service

# ps aux | grep gitlab

# ss -tunlp | grep 8000

備註:可以使用# gitlab-ctl help查看gitlab-ctl命令更爲詳細的用法

(4)配置GitLab開機自啓:# systemctl enable gitlab-runsvdir.service

(5)瀏覽器訪問http://192.168.1.145:8000,輸入新密碼root@123,點擊「Change your password」:

image.png

登陸:用戶名root,密碼root@123

image.png

image.png

(6)配置GitLab郵件服務:

# vim /etc/gitlab/gitlab.rb

修改以下代碼:

image.png

gitlab_rails['smtp_enable'] = true

gitlab_rails['smtp_address'] = "smtp.qq.com"

gitlab_rails['smtp_port'] = 465

gitlab_rails['smtp_user_name'] = "834143808@qq.com"

gitlab_rails['smtp_password'] = "QQ郵箱受權碼"

gitlab_rails['smtp_domain'] = "qq.com"

gitlab_rails['smtp_authentication'] = "login"

gitlab_rails['smtp_enable_starttls_auto'] = true

gitlab_rails['smtp_tls'] = true

image.png

新增代碼:user['git_user_email'] = "834143808@qq.com"

image.png

新增代碼:gitlab_rails['gitlab_email_from'] = '834143808@qq.com'

image.png

# gitlab-ctl reconfigure

(7)測試GitLab郵件服務是否正常:

# gitlab-rails console

irb(main):001:0> Notify.test_email('834143808@qq.com','GitLab Test','Hello GitLab').deliver_now

備註:Notify.test_email('接收方郵件地址','郵件標題','郵件內容').deliver_now,回車,測試發送

image.png

收取郵件:

image.png

(8)開啓GitLab註冊郵箱驗證功能:

Admin Area --> Settings --> General --> Sign-up restrictions --> Expand --> 勾選「Send confirmation email on sign-up--> Save changes

image.png

(9)配置GitLab用戶名和郵箱:

右上角圖標 --> Settings --> Profile --> Full nameqiuyue--> Email834143808@qq.com--> Update profile settings

image.png

收取郵件,點擊「Confirm your email address」:

image.png

從新登陸:

image.png

右上角圖標 --> Settings --> Profile --> Public email834143808@qq.com--> Update profile settings

image.png

右上角圖標 --> Settings --> Emails --> 刪除默認郵箱admin@example.com

image.png

image.png

(10)啓用「容許來自鉤子和服務對本地網絡的請求」:

Admin Area --> Settings --> Network --> Outbound requests --> Expand --> 勾選「Allow requests to the local network from web hooks and services--> Save changes

image.png

(11)建立GitLab項目:

image.png

填寫項目名稱爲zrlogVisibility Level爲「Private」,點擊「Create project」:

image.png

提示「建立SSH公鑰」:

image.png

(12)建立SSH公鑰:

node1節點生成密鑰對:

# yum -y install openssh-clients

# ssh-keygen -t rsa -P ""

# ls -a ~/.ssh

image.png

複製id_rsa.pub文件中的內容

右上角圖標 --> Settings --> SSH Keys --> 粘貼id_rsa.pub文件中的內容 --> Add key

image.png

image.png

收取郵件:

image.png

以前「建立SSH公鑰」的提示已經消失:

image.png

image.png

(13)node1節點將本地zrlog項目源碼推送至node2節點中的GitLab

# git config --global user.name "qiuyue"

# git config --global user.email "834143808@qq.com"

# git config --global credential.helper store

# git config --global color.ui true

# git config --list

image.png

備註:命令# git config --global credential.helper store的做用是避免每次pull/push代碼時輸入用戶名和密碼

# cd /projects/zrlog

# git remote rename origin old-origin

# git remote add origin git@192.168.1.145:root/zrlog.git

# git push -u origin --all

image.png

# git push -u origin --tags

image.png

備註:上述執行的命令可參考以下頁面

image.png

刷新此頁面,zrlog項目的倉庫再也不爲空倉庫:

image.png

(14)建立dev分支,將本地zrlog項目源碼推送至node2節點中的GitLabdev分支:

# cd /projects/zrlog

# git branch

image.png

備註:默認爲master分支

# git branch dev

# git checkout dev

image.png

備註:建立並切換到dev分支

# git branch

image.png

備註:如今默認爲dev分支

# git push -u origin dev

image.png

刷新頁面,已變爲2個分支:

image.png

image.png

image.png

(15)建立「Access Tokens」:

右上角圖標 --> Settings --> Access Tokens --> Namezrlog--> Scopesapi--> Create personal access token

image.png

image.png

image.png

Access TokensnziSYx2sSMHmaDFs5x7Q

image.png

image.png

五、node1節點部署Maven

(1)解壓Maven

# tar -xf apache-maven-3.6.2-bin.tar.gz -C /usr/local

# cd /usr/local

# ln -sv apache-maven-3.6.2 maven

(2)配置Maven環境變量:

# vim /etc/profile.d/maven.sh

export MAVEN_HOME=/usr/local/maven

export PATH=$MAVEN_HOME/bin:$PATH

# . /etc/profile.d/maven.sh

# echo $MAVEN_HOME

image.png

(3)查看Maven版本信息:# mvn -v

image.png

備註:Maven的部署依賴於JDK

六、node1節點部署Nexus

(1)建立Nexus安裝目錄:# mkdir -pv /usr/local/nexus

(2)解壓Nexus

# tar -xf nexus-3.18.1-01-unix.tar.gz -C /usr/local/nexus

# cd /usr/local/nexus

image.png

備註:解壓後會生成兩個目錄,分別爲nexus-3.18.1-01sonatype-work

# ln -sv nexus-3.18.1-01 nexus

(3)修改nexus-default.properties配置文件:

# vim /usr/local/nexus/nexus/etc/nexus-default.properties,修改以下代碼:

application-host=0.0.0.0 --> application-host=192.168.1.144

備註:Nexus默認監聽的端口號爲8081

(4)建立Nexus啓動用戶nexus,並賦予對應權限:

# useradd nexus

# chown -R nexus.nexus /usr/local/nexus

(5)修改Nexus的啓動用戶爲nexus

# vim /usr/local/nexus/nexus/bin/nexus.rc,修改以下代碼:

#run_as_user="" --> run_as_user="nexus"

(6)啓動Nexus

# ln -sv /usr/local/nexus/nexus/bin/nexus /usr/local/bin

# nexus start

image.png

備註:nexus腳本支持的參數爲{ start | stop | run | run-redirect | status | restart | force-reload }

# ps aux | grep nexus

# ss -tunlp | grep 8081

# tail -100 /usr/local/nexus/sonatype-work/nexus3/log/nexus.log

備註:若是沒有修改nexus.rc文件,啓動Nexus前須要先su - nexus

(7)瀏覽器訪問http://192.168.1.144:8081,點擊右上角「Sign in」:

image.png

登陸:用戶名admin,默認密碼保存在/usr/local/nexus/sonatype-work/nexus3/admin.password文件中,此處爲b6873c5b-d8ee-4454-a17a-d99bc7f19f40

image.png

image.png

新密碼爲root@123

image.png

image.png

image.png

image.png

(8)修改系統文件描述符大小,並重啓生效:

# ulimit -a

image.png

# vim /etc/security/limits.conf,末尾新增以下代碼:

* soft nofile 65536

* hard nofile 65536

# reboot

# ulimit -a

image.png

(9)啓動Nexus,並刷新頁面:

# nexus start

# ps aux | grep nexus

# ss -tunlp | grep 8081

image.png

(10)配置Nexus開機自啓:

# vim /etc/rc.d/rc.local

/usr/local/nexus/nexus/bin/nexus start

# chmod +x /etc/rc.d/rc.local

備註:若是沒有修改nexus.rc文件,開機自啓命令爲su - nexus -c '/usr/local/nexus/nexus/bin/nexus start'

(11)修改Nexusmaven-central的倉庫地址爲阿里雲倉庫:

image.png

點擊「maven-central」,將Remote storage中的地址https://repo1.maven.org/maven2/修改成http://maven.aliyun.com/nexus/content/groups/public/,點擊「Save」:

image.png

備註:倉庫類型

a、hosted:宿主倉庫,用於發佈內部項目的倉庫,其中maven-releases用來存放releases版本jar包的倉庫,maven-snapshots用來存放snapshots版本jar包的倉庫

b、proxy:代理倉庫,用於代理遠程倉庫

c、group:倉庫組,一般包含了多個宿主倉庫和代理倉庫,其中maven-publicmaven-centralmaven-releasesmaven-snapshots三個倉庫的合集

(12)修Nexusmaven-releasesDeployment policy爲「Allow redeploy」:

image.png

(13)node1節點修改Maven配置文件:

# cd $MAVEN_HOME/conf

# cp settings.xml settings.xml.bak

# vim settings.xml

a、配置私服用戶名密碼,在<servers></servers>配置段中新增以下代碼:

<servers>

<server>

      <id>nexus-releases</id>

      <username>admin</username>

      <password>root@123</password>

</server>

<server>

      <id>nexus-snapshots</id>

      <username>admin</username>

      <password>root@123</password>

</server>

</servers>

b、配置倉庫地址,在<profiles></profiles>配置段中新增以下代碼:

<profiles>

<profile>

       <id>nexus</id>

       <!--私服地址-->

       <repositories>

<repository>

<id>nexus</id>

<url>http://192.168.1.144:8081/repository/maven-public/</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>

       </repositories>

       <!--插件庫地址-->

       <pluginRepositories>

<pluginRepository>

<id>nexus</id>

<url>http://192.168.1.144:8081/repository/maven-public/</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>true</enabled>

</snapshots>

</pluginRepository>

       </pluginRepositories>

</profile>

</profiles>

<activeProfiles>

<activeProfile>nexus</activeProfile>

</activeProfiles>

備註:http://192.168.1.144:8081/repository/maven-public/的來源以下圖所示

image.png

(14)node1節點修改ZrLog源碼中common/pom.xml配置文件:

# vim /projects/zrlog/common/pom.xml,註釋以下代碼:

<!--

<repositories>

<repository>

       <id>ngnx</id>

       <url>https://oss.sonatype.org/content/groups/public</url>

       <releases>

<enabled>true</enabled>

       </releases>

       <snapshots>

<enabled>false</enabled>

       </snapshots>

</repository>

<repositories>

-->

(15)node1節點修改ZrLog源碼中pom.xml配置文件:

# vim /projects/zrlog/pom.xml,在</build></project>配置段中新增以下代碼:

<distributionManagement>

<repository>

       <id>nexus-releases</id>

       <name>Nexus Releases</name>

       <url>http://192.168.1.144:8081/repository/maven-releases/</url>

</repository>

<snapshotRepository>

       <id>nexus-snapshots</id>

       <name>Nexus Snapshots</name>

       <url>http://192.168.1.144:8081/repository/maven-snapshots/</url>

</snapshotRepository>

</distributionManagement>

備註:

a、此處的id必需要與$MAVEN_HOME/conf/settings.xmlserver下的id內容一致

b、http://192.168.1.144:8081/repository/maven-releases/的來源以下圖所示

image.png

c、http://192.168.1.144:8081/repository/maven-snapshots/的來源以下圖所示

image.png

(16)node1節點將修改推送至node2節點中的GitLab

# cd /projects/zrlog

# git add .

# git status

image.png

# git commit -m "modify pom.xml"

image.png

# git push -u origin dev

image.png

# git status

image.png

刷新以下頁面,dev分支頁面內容均已更新:

http://192.168.1.145:8000/root/zrlog/blob/dev/pom.xml

http://192.168.1.145:8000/root/zrlog/blob/dev/common/pom.xml

master分支同理:

# git checkout master

# git branch

# vim /projects/zrlog/common/pom.xml,如上述註釋代碼

# vim /projects/zrlog/pom.xml,如上述在</build></project>配置段中新增代碼

# git add .

# git status

# git commit -m "modify pom.xml"

# git push -u origin master

# git status

刷新以下頁面,master分支頁面內容也已更新:

http://192.168.1.145:8000/root/zrlog/blob/master/pom.xml

http://192.168.1.145:8000/root/zrlog/blob/master/common/pom.xml

(16)node1節點上傳jar包至私服:

默認倉庫爲空:

image.png

image.png

# cd /projects/zrlog

# mvn clean deploy -Dmaven.test.skip=true

image.png

刷新頁面http://192.168.1.144:8081/#browse/browse:maven-public

image.png

刷新頁面http://192.168.1.144:8081/#browse/browse:maven-releases

image.png

(17)node1節點從私服下載jar包:

https://mvnrepository.com/上任意找一個依賴,如https://mvnrepository.com/open-source/json-libraries

點擊「Jackson Databind」:

image.png

選擇版本,如2.9.9.3

image.png

image.png

將上述紅框中的代碼複製至/projects/zrlog/pom.xml

image.png

從私服下載以前本地沒有此jar包:# find / -name jackson-databind-2.9.9.3.jar

image.png

# cd /projects/zrlog

# mvn clean deploy -Dmaven.test.skip=true

image.png

image.png

七、node3節點部署Tomcat

(1)解壓Tomcat

# tar -xf apache-tomcat-8.5.45.tar.gz -C /usr/local

# cd /usr/local

# ln -sv apache-tomcat-8.5.45 tomcat

(2)配置Tomcat環境變量:

# vim /etc/profile.d/tomcat.sh

export CATALINA_HOME=/usr/local/tomcat

export PATH=$CATALINA_HOME/bin:$PATH

# . /etc/profile.d/tomcat.sh

# echo $CATALINA_HOME

image.png

# catalina.sh version

image.png

(3)建立Tomcat啓動用戶tomcat,並賦予對應權限:

# useradd tomcat

# chown -R tomcat.tomcat /usr/local/apache-tomcat-8.5.45

# chown -R tomcat.tomcat /usr/local/tomcat

(4)調整JVM使用的內存大小:

# vim /usr/local/tomcat/bin/catalina.sh

在首行#!/bin/sh下新增代碼JAVA_OPTS='-server -Xms2048m -Xmx2048m'

備註:內存大小按實際狀況調整,ServerJVM最好將-Xms-Xmx設爲相同值

(5)配置Tomcat訪問權限:

# cd /usr/local/tomcat/conf

# cp tomcat-users.xml tomcat-users.xml.bak

# vim tomcat-users.xml,在<tomcat-users></tomcat-users>配置段中新增以下代碼:

<role rolename="admin"/>

<role rolename="admin-gui"/>

<role rolename="admin-script"/>

<role rolename="manager"/>

<role rolename="manager-gui"/>

<role rolename="manager-script"/>

<role rolename="manager-jmx"/>

<role rolename="manager-status"/>

<user username="root" password="root@123" roles="admin,admin-gui,admin-script,manager,manager-gui,manager-script,manager-jmx,manager-status"/>

# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml

註釋以下代碼:

<Valve className="org.apache.catalina.valves.RemoteAddrValve"

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

(6)啓動Tomcat

# su - tomcat

$ catalina.sh configtest

$ catalina.sh start

$ ps aux | grep java

$ ss -tunlp | grep 8080

$ tail -100 /usr/local/tomcat/logs/catalina.out

(7)瀏覽器訪問http://192.168.1.146:8080

image.png

點擊右側「Manager App」,用戶名root,密碼root@123

image.png

image.png

(8)配置Tomcat開機自啓:

# vim /etc/rc.d/rc.local

export JAVA_HOME=/usr/local/jdk

su - tomcat -c '/usr/local/tomcat/bin/startup.sh'

# chmod +x /etc/rc.d/rc.local

八、node3節點部署MySQL

(1)刪除CentOS 7.6內置的MySQL相關組件:

# rpm -qa | grep -i mariadb --> mariadb-libs-5.5.60-1.el7_5.x86_64

# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

若是以前安裝過MySQL,先卸載:# rpm -qa | grep -i mysql

若是存在/etc/my.cnf配置文件,先刪除:# rm -rf /etc/my.cnf

(2)http://repo.mysql.com/下載mysql57-community-release-el7.rpm

(3)安裝mysql57-community-release-el7.rpm# rpm -ivh mysql57-community-release-el7.rpm

備註:安裝後會在/etc/yum.repos.d目錄中生成mysql-community-source.repomysql-community.repo

(4)安裝MySQL

# yum -y install mysql-community-client mysql-community-libs mysql-community-common mysql-community-libs-compat mysql-community-devel mysql-community-server

(5)初始化MySQL# mysqld --initialize --user=mysql --datadir=/var/lib/mysql

備註:初始化以前確保/var/lib/mysql目錄爲空

(6)修改/etc/my.cnf配置文件:

# mv /etc/my.cnf /etc/my.cnf.bak

# vim /etc/my.cnf

[mysqld]

port=3306

socket=/var/lib/mysql/mysql.sock

datadir=/var/lib/mysql

pid-file=/var/run/mysqld/mysqld.pid

log-error=/var/log/mysqld.log

lower_case_table_names=1

character_set_server=utf8mb4

collation_server=utf8mb4_general_ci

innodb_file_per_table=1

skip_name_resolve=1

slow_query_log=1

slow_query_log_file=mysql-slow.log

symbolic-links=0

explicit_defaults_for_timestamp=1

server_id=1

sync_binlog=1

innodb_flush_log_at_trx_commit=1

log_bin=mysql-bin

log_bin_index=mysql-bin.index

binlog_format=mixed

(7)啓MySQL服務:

# systemctl start mysqld.service

# systemctl status mysqld.service

# ps aux | grep mysqld

# ss -tunlp | grep 3306

# tail -100 /var/log/mysqld.log

(8)配置MySQL服務開機自啓:# systemctl enable mysqld.service

(9)查看root@localhost用戶的初始密碼:# grep password /var/log/mysqld.log

(10)配置MySQL安全向導:# mysql_secure_installation

(11)建立ZrLogSonarQube所需的數據庫和用戶:

# mysql -uroot -p

mysql> create database zrlog;

mysql> grant all on zrlog.* to 'zrlog'@'192.168.1.%' identified by '123456';

mysql> create database sonar default character set utf8;

mysql> grant all on sonar.* to 'sonar'@'192.168.1.%' identified by '123456';

mysql> flush privileges;

mysql> select user,host from mysql.user;

image.png

(17)node1節點配置ZrLog源碼中的MySQL數據庫鏈接信息:

# cd /projects/zrlog

# git branch

image.png

# rm -rf web/src/main/webapp/WEB-INF/install.lock

# vim web/src/main/webapp/WEB-INF/db.properties

image.png

(18)node1節點將修改推送至node2節點中的GitLab

# cd /projects/zrlog

# git add .

# git status

image.png

備註:以前pom.xml中引用jackson-databind-2.9.9.3.jar時沒有推送代碼至GitLab

# git commit -m "modify db.properties"

image.png

# git push -u origin master

image.png

刷新master分支頁面,內容已更新:

image.png

dev分支同理:

# git checkout dev

# git branch

# rm -rf web/src/main/webapp/WEB-INF/install.lock

# vim web/src/main/webapp/WEB-INF/db.properties,如上述修改代碼

# git add .

# git status

# git commit -m "modify db.properties"

# git push -u origin dev

刷新dev分支頁面,內容也已更新:

image.png

九、node3節點部署SonarQube

(1)查看部署SonarQube的前置條件:

目前最新版本爲7.9.1,必須JDK 11+,且不支持MySQL

https://docs.sonarqube.org/7.9/requirements/requirements/

此處使用的版本爲7.7,只兼容JDK 8,並支持MySQL 5.6 or 5.7

https://docs.sonarqube.org/7.7/requirements/requirements/

(2)修改系統文件描述符大小,並重啓生效:

# ulimit -a

image.png

# vim /etc/security/limits.conf,末尾新增以下代碼:

* soft nofile 65536

* hard nofile 65536

# reboot

# ulimit -a

image.png

(3)修改系統參數值vm.max_map_count

# vim /etc/sysctl.conf

vm.max_map_count = 262144

# sysctl -p

image.png

(4)解壓SonarQube

# yum -y install unzip

# unzip -qd /usr/local/ sonarqube-7.7.zip

# cd /usr/local

# ln -sv sonarqube-7.7 sonar

(5)配置SonarQube訪問MySQLWeb相關信息:

# vim /usr/local/sonar/conf/sonar.properties,新增以下代碼:

sonar.jdbc.username=sonar

sonar.jdbc.password=123456

sonar.jdbc.url=jdbc:mysql://192.168.1.146:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

sonar.web.host=192.168.1.146

sonar.web.context=/sonar

sonar.web.port=9000

備註:數據庫名稱爲sonar

(6)建立SonarQube啓動用戶sonar,並賦予對應權限:

# useradd sonar

# chown -R sonar.sonar /usr/local/sonarqube-7.7

# chown -R sonar.sonar /usr/local/sonar

(7)啓動SonarQube

# ln -sv /usr/local/sonar/bin/linux-x86-64/sonar.sh /usr/local/bin

# su - sonar

$ sonar.sh start

image.png

$ sonar.sh status

image.png

$ ps aux | grep sonar

$ ss -tunlp | grep 9000

$ tail -100 /usr/local/sonar/logs/sonar.log

備註:sonar.sh腳本支持的參數爲{ console | start | stop | restart | status | dump }

(8)瀏覽器訪問http://192.168.1.146:9000/sonar,點擊右上角「Log in」:

image.png

登陸:用戶名和密碼均爲admin

image.png

image.png

(9)修改admin用戶的默認密碼:

點擊右上角「Administrator--> My Account

image.png

點擊「Security」:

image.png

填寫舊密碼爲admin,新密碼爲root@123,點擊「Change password」:

image.png

(10)開啓強制用戶身份認證:

點擊「Administration--> Configuration --> Security --> 啓用「Force user authentication--> Save

image.png

(11)建立SonarQube項目:

image.png

項目名稱:zrlog

image.png

點擊「Generate」生成Token

image.png

Tokena8a629f19768a1cf81c4ed772f294c5a17eec854

image.png

項目的主要語言:Java,項目的構建技術:Maven

image.png

(12)node1節點在項目根目錄(pom.xml所在位置)執行上述紅框中的代碼進行代碼分析:

# cd /projects/zrlog

mvn sonar:sonar \

-Dsonar.projectKey=zrlog \

-Dsonar.host.url=http://192.168.1.146:9000/sonar \

-Dsonar.login=a8a629f19768a1cf81c4ed772f294c5a17eec854

image.png

(13)刷新頁面,查看代碼分析結果:

image.png

image.png

十、node1節點部署Jenkins

(1)下載Jenkins倉庫:

# yum -y install wget

# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

# cat /etc/yum.repos.d/jenkins.repo

image.png

# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

(2)下載Jenkins# yum -y install jenkins

備註:也能夠從https://jenkins.io/zh/download/下載jenkins.war,而後將war包部署於Tomcatwebapps目錄下啓動

(3)修改啓動腳本,並使其生效:

# which java

image.png

# vim /etc/init.d/jenkins,在/usr/bin/java下新增代碼:/usr/local/jdk/bin/java

image.png

# systemctl daemon-reload

(4)啓動Jenkins

# systemctl start jenkins.service

# systemctl status jenkins.service

# ps aux | grep jenkins

# ss -tunlp | grep 8080

備註:安裝後生成的文件和目錄

a、配置文件:/etc/sysconfig/jenkins

b、監聽端口:8080

c、war包位置:/usr/lib/jenkins/jenkins.war

d、緩存目錄:/var/cache/jenkins

e、安裝目錄:/var/lib/jenkins

f、日誌文件:/var/log/jenkins/jenkins.log

g、啓動用戶:jenkins

(5)配置Jenkins開機自啓:# systemctl enable jenkins.service

(6)瀏覽器訪問http://192.168.1.144:8080

image.png

默認密碼保存在/var/lib/jenkins/secrets/initialAdminPassword文件中,此處爲d81df2baa4e84f08b802a1baf3660232

image.png

image.png

安裝推薦的插件:

image.png

image.png

登陸:用戶名root,密碼root@123

image.png

image.png

image.png

image.png

(7)Jenkins安裝指定插件:

Manage Jenkins --> Manage Plugins --> Available --> 安裝Deploy to container PluginGitLab PluginMaven Integration pluginNodeJS PluginPublish Over SSHSonarQube Scanner for Jenkins插件

image.png

image.png

image.png

image.png

備註:下載的插件存放於/var/lib/jenkins/plugins目錄下

# systemctl restart jenkins.service

刷新頁面:

image.png

image.png

備註:界面語言自動變爲簡體中文

(8)系統管理 --> 全局安全配置:

image.png

勾選「容許用戶註冊」和「匿名用戶具備可讀權限」:

image.png

取消勾選「防止跨站點請求僞造」,點擊「保存」:

image.png

(9)系統管理 --> 全局工具配置:

image.png

Maven配置:

image.png

JDK

image.png

Git

image.png

SonarQube Scanner

image.png

Maven

image.png

NodeJS

image.png

最後點擊「保存」

(10)系統管理 --> 系統設置:

image.png

Maven項目配置:

image.png

全局屬性:

image.png

image.png

SonarQube servers

image.png

Secret處填寫SonarQubeTokena8a629f19768a1cf81c4ed772f294c5a17eec854

image.png

image.png

image.png

Gitlab

image.png

API token處填寫GitLabTokennziSYx2sSMHmaDFs5x7Q

image.png

image.png

image.png

Jenkins Location

image.png

Git plugin

image.png

image.png

Extended E-mail Notification

Extended E-mail Notification插件不管項目構建成功與否都會發送郵件,而「郵件通知」插件只有在項目構建不穩定或項目構建失敗時發送郵件

image.png

image.png

image.png

image.png

Publish over SSH

node1節點執行以下命令:

# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.146

# ssh root@192.168.1.146 'hostname'

image.png

Key處填寫node1~/.ssh/id_rsa的私鑰內容

image.png

image.png

最後點擊「保存」


4、Jenkins建立新任務:

image.png

image.png

General

image.png

源碼管理:

image.png

image.png

UsernamegitKey處填寫node1~/.ssh/id_rsa的私鑰內容

image.png

image.png

image.png

構建觸發器:

image.png

GitLab webhook URLhttp://192.168.1.144:8080/project/zrlog

image.png

Secret tokenbb9547d853de09897f0d5ae8fff7c4c8

image.png

配置GitLab webhook

image.png

image.png

image.png

image.png

構建環境:

image.png

Pre Steps保持默認

Build

image.png

Post Steps

image.png

Analysis properties

sonar.projectKey=zrlog

sonar.projectName=zrlog

sonar.projectVersion=1.0

sonar.sources=./common/src,./data/src,./service/src,./web/src

sonar.java.binaries=./common/target/classes,./data/target/classes,./service/target/classes,./web/target/classes

sonar.language=java

sonar.sourceEncoding=UTF-8

sonar.login=admin

sonar.password=root@123

備註:sonar.sources表示存放.java文件的目錄,sonar.java.binaries表示存放.class文件的目錄,不一樣項目文件的存放位置可能不一樣

image.png

構建設置保持默認

構建後操做:

Editable Email Notification

image.png

image.png

Deploy war/ear to a container

image.png

image.png

image.png

conf/tomcat-users.xml文件中配置的用戶名和密碼

image.png

image.png

image.png

Send build artifacts over SSH

image.png

Exec command

su - tomcat -c "/usr/local/tomcat/bin/shutdown.sh"

sleep 5

cd /usr/local/tomcat/webapps

mv zrlog /tmp/zrlog_`date +%F_%T` &> /dev/null

mv zrlog-*.war /tmp

mv zrlog-* zrlog

su - tomcat -c "/usr/local/tomcat/bin/startup.sh"

image.png

最後點擊「保存」

image.png


5、Jenkins構建新任務:

手動構建:

點擊「當即構建」:

image.png

點擊「控制檯輸出」:

image.png

image.png

image.png

image.png

image.png

/var/lib/jenkins/workspace/zrlog/target/zrlog-2.1.3.war部署至遠程node3節點的Tomcat中自動解壓執行

收取郵件:

image.png

查看Nexus

image.png

image.png

查看SonarQube

image.png

image.png

安裝ZrLog

瀏覽器訪問http://192.168.1.146:8080/zrlog

image.png

管理員帳號:root,管理員密碼:root@123

image.png

image.png

「點擊查看」,顯示主頁:

image.png

點擊「管理」,登陸後顯示後臺管理:

image.png

image.png

ZrLog目錄結構比較:

Git源碼目錄:

image.png

Jenkins工做目錄:

image.png

編譯打包發佈後的目錄:

image.png

GitLab測試Webhook

image.png

測試成功,返回「HTTP 200」:

image.png

已觸發自動構建:

image.png

image.png

image.png

收取郵件:

image.png

查看SonarQube

image.png

node1節點修改dev分支源碼並推送至node2節點中的GitLab不會觸發自動構建:

# cd /projects/zrlog

# git checkout dev

# git branch

# echo "dev branch" > README.md

# git add .

# git commit -m "modify README.md"

# git push -u origin dev

image.png

image.png

node1節點修改master分支源碼並推送至node2節點中的GitLab會觸發自動構建:

# cd /projects/zrlog

# git checkout master

# git branch

# echo "master branch" > README.md

# git add .

# git commit -m "modify README.md"

# git push -u origin master

image.png

image.png

image.png

image.png

收取郵件:

image.png

查看SonarQube

image.png

全部頁面最終效果以下所示:

GitLab

image.png

image.png

Nexus

image.png

image.png

SonarQube

image.png

image.png

Jenkins

image.png

image.png

相關文章
相關標籤/搜索