赞
踩
Jenkins是Java编写的。所以需要先安装JDK。这里采用yum安装。如果对版本有需求,可以直接在Oracle官网下载JDK.
以下通过离线方式安装java1.8
[root@master ~]# mkdir /usr/java
[root@master ~]# tar -xvf tools/jdk-8u161-linux-x64.tar.gz -C /usr/java/
[root@master ~]# vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_161
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[root@master ~]# source /etc/profile
[root@master ~]# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
安装jenkins
Jnekins 到官网下载最新LTS版本war包:
https://jenkins.io/zh/download/
上传jenkins.war包到Linux
运行war包:java -jar jenkins.war (也可在tomcat等中间件部署)
[root@minion1 usr]# mkdir jenkins
[root@minion1 usr]# cd jenkins/
[root@minion1 jenkins]# java -jar jenkins.war
kvm所在物理机器,开启端口转发
[root@localhost ~]# nohup ncat --sh-exec "ncat 192.168.122.5 8080" -l 8080 --keep-open &
kvm所在机器,运行war包
#此时,保持运行,另外打开一个shell窗口
[root@minion1 jenkins]# nohup java -jar jenkins.war --httpPort=8080 &
..........
ingletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e48ca39: defining beans [filter,legacy]; root of factory hierarchy
2020-12-21 01:14:14.709+0000 [id=33] INFO jenkins.InitReactorRunner$1#onAttained: Completed initialization
2020-12-21 01:14:14.717+0000 [id=21] INFO hudson.WebAppMain$3#run: Jenkins is fully up and running
[root@minion1 jenkins]# netstat -nltp
Active Internet connections (only servers) LISTEN 1457/master
.......
tcp6 0 0 :::8080 :::*
.......
访问localhost:8080,进入Jenkins引导页
查看密钥
[root@minion1 jenkins]# vim /root/.jenkins/secrets/initialAdminPassword
1e4be0b3690840d695c029e8d88999f0
jenkins是平台,更多使用的是它的插件,插件完备且功能丰富。
离线安装以下插件:
例举离线安装Folders
如下错误提示,因为无法联网报错,请忽略
之后等待提示下载完成,开始安装
查看安装完全的组件
1.在连接网络的环境中,下载部署jenkins同样的环境,通过联网下载相关组件,组件内容放置在/root/.jenkins/plugins中,打成tar包,放置在内网环境中的plugins目录下,重启启动jenkins
2.在联网机器下载组件功能以及相关依赖,之后,上传到/root/.jenkins/plugins中,然后根据启动的提示,做调整解决相关出现依赖和版本问题。
推荐第1种方式方法实现
注:中文设置
安装Locale、Localization: Chinese(Simplified)、localization-support组件
之后重启jenkins
而且我相信你一定听过持续生成、持续交付、持续部署这些名词。
补充安装插件
安装gitlab相关插件与gitlab应用程序结合
登录gitLab
地址:http://10.105.0.134:8091/
账号/密码:root/12345678
jenkins下生成
[root@minion1 .ssh]# ssh-keygen -t rsa
[root@minion1 .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCb8W0Iw24vr3iF3FtZiJW4Kcx8MFEkFkvM+/8M6Jn7SgiDzDRyxAuXpFiz7NbSR4gOhWw8/zgpjAvD5rPVOsLy+EHR1EoXw5Lciu7mlosjpWPP09rDzpwiqktz1WvpKQ6bRBkFOkIU7QzWBmA0Yhqevao+gEka9qXJlK0ftMH+q1YuDlOzFOvtIsWIr8A4PKjvmZAIk31wFDlZu3JzPm7Ew+hCY+D+99F9MurIcoXzG7bXenEwNperN/g0skaJ6K5YWaMULBpfrio/kpTdcMl75dscjXFL+pVRLXNGZV/gLi9tKMgzB9PXPynluV7g0YvuJVBUBpEvrNsBP+y/ClLP root@minion1
配置完密钥后,git测试拉取
[root@minion1 ~]# git clone git@192.168.122.4:web/web-demo.git
Cloning into 'web-demo'...
..........
Receiving objects: 100% (6/6), done.
[root@minion1 ~]# cd web-demo/
[root@minion1 web-demo]# ls
index.html
以下继续配置jenkins
查看私钥,并填写私钥
[root@minion1 web-demo]# cd ~/.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
[root@minion1 web-demo]# vim ~/.ssh/id_rsa
......
继续配置jekins
查看git HTTP地址与jenkins版本
[root@master ~]# rpm -qa|grep gitlab
gitlab-ce-13.6.3-ce.0.el7.x86_64
查看创建好的任务信息
[root@minion1 web-demo]# cd /root/.jenkins/workspace/auto-deploy [root@minion1 auto-deploy]# ls index.html [root@minion1 auto-deploy]# git show commit 5142d8bc7673e8e73ccdcf5f09961d54f7abaa06 Author: root <root@example.com> Date: Fri Dec 18 01:14:03 2020 -0500 v2.0 diff --git a/index.html b/index.html index abb1232..91e15fd 100644 --- a/index.html +++ b/index.html @@ -1 +1,3 @@ dasdasdasdasdsahhhhhhhhAAAAAAAAAAAAddddd +你好 +太阳能 #已经创建了git [root@minion1 auto-deploy]# ls -a . .. .git index.html
持续代码质量管理Sonar部署
Sonar是一个用于代码质量管理的开放平台。通过插件机制, Sonar可以集成不同的测试工具,代码分析工具,以及持续生成工具。与持续生成工具(例如Hudson/Jenkins等)不同, Sonar并不是简单地把不同的代码检查工具结果(例如FindBugs[ PMD等)直接显示在Web页面上,而是通过不同的插件对这些结果进行再加工处理,通过里化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。
在对其他工具的支持方面, Sonar不仅提供了对IDE的支持,可以在Eclipse和IntelliJl IDEA这些工具里联机查看结果;同时Sonar还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用Sonar.此外, Sonar的插件还可以对Java以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。
Sonar邮署
Sonar的相关下载和文档可以在下面的链接中找到: htp://www.sonarqube.org/downloads/,需要注意最新版的Sonar需要至少JDK 1.8及以上版本。
这里我在内网机器中部署了一个docker的mysql,基本命令如下:
[root@minion1 ~]# docker run -d --name mysql -v /data/mysql/conf/my.cnf:/etc/mysql/my.cnf -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
创建地址转发
[root@localhost zhangyq]# nohup ncat --sh-exec "ncat 192.168.122.5 3307" -l 3316 --keep-open &
准备Sonar数据库
mysq1> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
nysq1> GRANT ALL ON sonar.* To 'sonar'@'localhost' IDENTIFIED BY 'sonar@pw';
mysql> GRANT ALL ON sonar.* To 'sonar'@'%' IDENTIFIED BY 'sonar@pw';
nysql> FLUSH PRIVILEGES;
部署Sonar
[root@minion1 tools]# unzip sonarqube-8.6.0.39681.zip -d /usr/local/
[root@minion1 local]# ln -s /usr/local/sonarqube-8.6.0.39681/ /usr/local/sonarqube
注意:安装部署的过程中发现,高版本从7.9的Sonar不再支持mysql。推荐使用7.5结合mysql
配置Sonar
[root@minion1 local]# cd /usr/local/sonarqube/conf/
[root@minion1 conf]# ls
sonar.properties wrapper.conf
编写配置文件,修改数据库配置
[root@minion1 conf]# vim sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@pw
sonar.jdbc.url=jdbc:mysql://localhost:3307/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
[root@minion1 conf]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
Starting SonarQube...
Started SonarQube.
未正常启动,检查日志
[root@minion1 logs]# tail /usr/local/sonarqube/logs/sonar.log --> Wrapper Started as Daemon Launching a JVM... Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. 2021.01.15 02:07:21 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /usr/local/sonarqube-7.5/temp 2021.01.15 02:07:21 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001 2021.01.15 02:07:21 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/usr/local/sonarqube-7.5/elasticsearch]: /usr/local/sonarqube-7.5/elasticsearch/bin/elasticsearch -Epath.conf=/usr/local/sonarqube-7.5/temp/conf/es 2021.01.15 02:07:21 INFO app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running 2021.01.15 02:07:21 INFO app[][o.e.p.PluginsService] no modules loaded 2021.01.15 02:07:21 INFO app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin] 2021.01.15 02:07:22 WARN app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [es]: 1 2021.01.15 02:07:22 INFO app[][o.s.a.SchedulerImpl] Process [es] is stopped 2021.01.15 02:07:22 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped <-- Wrapper Stopped
普通用户sonar启动
不能使用root启动,使用创建的用户赋权启动sonar
[root@minion1 logs]# adduser sonar
[root@minion1 logs]# echo 123456|passwd --stdin sonar
Changing password for user sonar.
passwd: all authentication tokens updated successfully.
[root@minion1 logs]# chown -R sonar:sonar /usr/local/sonarqube/
[root@minion1 logs]# su - sonar
[root@sonar logs]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
查看端口和生成文件
[sonar@minion1 plugins]$ ls /usr/local/sonarqube/extensions/plugins
-rw-r--r-- 1 sonar sonar 2573661 Dec 18 2018 sonar-csharp-plugin-7.9.1.7622.jar
-rw-r--r-- 1 sonar sonar 6031675 Dec 18 2018 sonar-css-plugin-1.0.3.724.jar
-rw-r--r-- 1 sonar sonar 1551459 Dec 18 2018 sonar-flex-plugin-2.4.0.1222.jar
-rw-r--r-- 1 sonar sonar 3904078 Dec 18 2018 sonar-go-plugin-1.1.0.1612.jar
-rw-r--r-- 1 sonar sonar 14629 Dec 18 2018 sonar-jacoco-plugin-1.0.1.143.jar
[sonar@minion1 plugins]$ netstat -ntlp
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
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:32000 0.0.0.0:* LISTEN 14957/java
tcp6 0 0 127.0.0.1:36581 :::* LISTEN 15322/java
tcp6 0 0 :::9000 :::* LISTEN 15079/java
tcp6 0 0 127.0.0.1:9001 :::* LISTEN 14982/java
sonar的地址转发
[root@localhost zhangyq]# nohup ncat --sh-exec "ncat 192.168.122.5 9000" -l 9000 --keep-open &
登录测试成功
[sonar@minion1 linux-x86-64]$ cat /usr/local/sonarqube/logs/sonar.log
WrapperSimpleApp: Encountered an error running main: java.nio.file.AccessDeniedException: /usr/local/sonarqube-7.5/temp/conf/es/elasticsearch.yml
java.nio.file.AccessDeniedException: /usr/local/sonarqube-7.5/temp/conf/es/elasticsearch.yml
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
.............
切换root用户,删除/usr/local/sonarqube/temp/文件
使用sonar用户,重新启动
[sonar@minion1 ~]$ /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
查看日志,启动成功
[sonar@minion1 ~]$ tail -f /usr/local/sonarqube/logs/sonar.log
2021.01.15 02:23:10 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/usr/local/sonarqube-7.5]: /usr/java/jdk1.8.0_161/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonarqube-7.5/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/usr/local/sonarqube-7.5/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.server.app.WebServer /usr/local/sonarqube-7.5/temp/sq-process2844949370038272350properties
根据版本选择sonar的对应汉化jar包
https://github.com/xuhuisheng/sonar-l10n-zh
https://github.com/xuhuisheng/sonar-l10n-zh/releases/tag/sonar-l10n-zh-plugin-1.25
或者自己寻找
https://github.com/xuhuisheng/sonar-l10n-zh/releases/
jar包放入到此文件夹中
[root@minion1 ~]# cd /usr/local/sonarqube-7.5/extensions/plugins
重启sonarqube
[root@minion1 plugins]# su - sonar
[root@minion1 ~]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart
查看登录页面显示,为中文版
SonarQube安装好后是没法扫描项目的,所以还需安装SonarScanner
下载地址:https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/
具体下载地址为:https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/
把下载好的sonar-scanner-xxxxxx-linux.zip放到/usr/local目录
[root@localhost ~]# cd /usr/local
[root@localhost local]# unzip sonar-scanner-cli-4.3.0.2102-linux.zip
[root@localhost local]# cd sonar-scanner-4.3.0.2102-linux
[root@localhost sonar-scanner-4.3.0.2102-linux]# vi /conf/sonar-scanner.properties
建立关联
[root@minion1 tools]# unzip sonar-scanner-2.6.1.zip [root@minion1 tools]# mv sonar-scanner-2.6.1/ /usr/local/ [root@minion1 tools]# ln -s /usr/local/sonar-scanner-2.6.1/ /usr/local/sonar-scann [root@minion1 tools]# cd /usr/local/sonar-scanner [root@minion1 sonar-scanner]# ls bin conf jre lib [root@minion1 sonar-scanner]# vim conf/sonar-scanner.properties #Configure here general information about the environment, such as SonarQube DB details for example #No information about specific project should appear here #----- Default SonarQube server sonar.host.url=http://localhost:9000 #----- Default source code encoding sonar.sourceEncoding=UTF-8 #----- Global database settings (not used for SonarQube 5.2+) sonar.jdbc.username=sonar sonar.jdbc.password=sonar@pw #----- PostgreSQL #sonar.jdbc.url=jdbc:postgresql://localhost/sonar #----- MySQL sonar.jdbc.url=jdbc:mysql://localhost:3307/sonar?useUnicode=true&characterEncoding=utf8 #----- Oracle #sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE #----- Microsoft SQLServer #sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
下载使用一个测试项目,做代码检测测试
下载项目为地址为https://github.com/SonarSource/sonar-scanning-examples
[root@localhost zhangyq]# scp sonar-scanning-examples-master.zip root@192.168.122.5:/root/tools/
[root@minion1 tools]# unzip sonar-scanning-examples-master.zip
切到sonar-project.properties所在文件目录中,启动扫描
[root@minion1 sonarqube-scanner]# ll
........
-rw-r--r-- 1 root root 647 Dec 22 03:06 sonar-project.properties
........
[root@minion1 sonarqube-scanner]# /usr/local/sonar-scann/bin/sonar-scanner
下载sonarQube Scanner的插件以及依赖
构建项目,使用到sonar
Analysis properties配置如下:
sonar.projectKey=demo
sonar.projectName=demo
sonar.projectVersion=1.0
sonar.sources=./
sonar.language=php
sonar.sourceEncoding=UTF-8
第一此尝试构建
重点部分内容
[auto-deploy] $ /usr/local/sonar-scann/bin/sonar-scanner -Dsonar.host.url=http://10.105.0.134:9000 -Dsonar.language=php -Dsonar.projectName=demo -Dsonar.projectVersion=1.0 -Dsonar.sourceEncoding=UTF-8 -Dsonar.projectKey=demo -Dsonar.sources=./
#此目录为发布的目录
-Dsonar.projectBaseDir=/root/.jenkins/workspace/auto-deploy
...................
INFO: ANALYSIS SUCCESSFUL, you can browse
#此地址为结合sonar检查状态的网页
http://10.105.0.134:9000/dashboard?id=demo
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at
#此地址为json的返回值
http://10.105.0.134:9000/api/ce/task?id=AXceq2S9CKeLm-6TRiTF
编写php文件内容,写入php网页内容
[root@minion1 auto-deploy]# cd /root/.jenkins/workspace/auto-deploy
[root@minion1 auto-deploy]# vim index.php
<!DOCTYPE html>
<html>
<body>
<h1>我的第一张 PHP 页面</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
重新创建发布,网页查看检查输出结果
可以根据自己需要,做发布后操作
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9hzIUxis-1623985800389)(./微信截图_20210120153732.png)]
重新构建项目,并测试shell的执行身份
122.5公钥加到122.4免登陆文件 /root/.ssh/authorized_keys,并远程执行发布程序脚本
修改发布目录下的index.html文件
完成两个任务的关联使用
[root@master webroot]# vim /deploy/code/web-demo/index.html
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
安装后,会有“构建后操作”功能
先确认项目已构建次数
auto-deploy
demo-deploy
视图工具的发布测试
gitlab页面network设置
在所在gitlab的机器上创建秘钥认证
122.4机器执行
[root@master ~]# openssl rand -hex 10
9a32c2997bfc48259d5d
组合URL如下
http://192.168.122.5:8080/buildByToken/build?job=auto-deploy&token=9a32c2997bfc48259d5d
编辑发布的网页内容
[root@master web-demo]# cd /deploy/code/web-demo
[root@master web-demo]# vim index.html
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
<h1>宾县</h1>
<h1>宾县2</h1>
</html>
[root@master web-demo]# /root/tools/deploy.sh deploy
gitlab自动部署操作开始
[root@master web-demo]# git add index.html
[root@master web-demo]# git commit -m "zhang"
[root@master web-demo]# git push
在git pull提交的时候,会触发两个关联任务
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。