极客时间运维进阶训练营第四周作业
必做题:
部署 jenkins master 及多 slave 环境
基于 jenkins 视图对 jenkins job 进行分类
总结 jenkins pipline 基本语法
部署代码质量检测服务 sonarqube
基于命令、shell 脚本和 pipline 实现代码质量检测
扩展题(选做):
jenkins 安装 Sonarqube Scanner 插件、配置 sonarqube server 地址、基于 jenkins 配置代码扫描参数实现代码质量扫描
Execute SonarQube Scanner
Jenkins Master-Slave 架构
解决:多任务同时并发处理时会增加单台 jenkins 负载,通过多台 jenkins 分发任务解决。功能:通过 Master 控制多台 Slave 节点构建。

一、准备工作
1、配置多台 jenkins 节点 、Slave 节点必须要有 JAVA 环境 2、将 Slave 节点公钥交给 jenkins 服务器内,之间通过 SSH 建立连接
二、配置架构
1、系统管理 --> 管理节点 --> 新建节点 (New Node) --> 填写配置、OK。
Node name:填写名称

2、添加下列参数并保存。
Name:名称
of executors:同时并发数量
Remote root directory:远程工作目录
Labels:标签:根据场景通过标签分组
Usage:使用方法:根据场景选择 指定 job 或者尽可能使用这个节点
Launch method:选择通过 SSH 连接 Slave
Host Key Veriflcation Strategy:可以选择 Manually trusted key Verification Strategy

添加 ssh 登录凭证、保存
注:如果添加了秘钥对则可以不用配置

3、系统管理 --> 管理节点 --> Slave1-192.168.0.215 ,点击进去。

4、点击 Launch agent 启动代理。

日志信息

5、Slave 操作:查看服务器内启动的 Slave
命令:ps -ef | grep slave

6、配合添加任务转交给 Slave 处理任务 configure --> General --> 限制项目的运行节点(Restrict where this project can be run) --> 添加 Slave 名称、保存。注:也可以添加标签,通过组的形式来分配。

7、验证:启动项目构建,查看日志 Slave 端项目目录:/var/lib/jenkins/workspace/A-Web1

基于 jenkins 视图对 jenkins job 进行分类
1, 点击创建视图

2, 配置视图,可以选择任务列表进行指定,也可以配置正则去模糊匹配 job

3, 在默认的所有标签的视图中创建名字为 test 开头的 job

4, 可以看到新建的 test 开头的 job 会自动被匹配到 test 视图中

总结 jenkins pipline 基本语法
pipeline 是什么,有什么作用,用 groovy 语言编写
创建 pipeline 操作步骤:
1、安装 jenkins 和安装插件 pipeline。
2、新建一个 pipeline 项目,在 pipeline 中加入脚本,构建执行。
1、pipeline 是什么
部署流水线:Deployment pipeline,从软件版本控制库到用户手中这一过程的自动化表现形式。所有的部署流水线都写在 jenkins-file 文件中,需要安装插件 pipeline 插件后才能使用 jenkins-file。
2、pipeline 的结构组成
1.pipeline 最简单的结构
pipeline 是什么,用于描述整条流水线是如何进行的。流水线的内容包括执行编译、打包、测试、输出测试报告等步骤。以下 5 个部分是 pipeline 的必需存在的,少一个,jenkins 都会报错。
1、pipeline: 代表整条流水线,包含整条流水线的逻辑。
2、stage 部分:阶段,代表流水线的阶段,每个阶段都必须有名称,本例中,build 就是此阶段的名称。
3、stages 部分:流水线中的多个 stage 的容器。stages 部分至少包含一个 stage。
4、steps 部分:代表阶段中的一个或多个具体步骤(step)的容器。steps 部分至少包含一个步骤,本例中,echo 就是一个步骤,在一个 stage 中有且只有一个 steps。
5、agent 部分:指定流水线的执行位置(Jenkins agent)。
2.pipeline 的步骤
pipeline 基本结构决定的是 pipeline 整体流程。
pipeline plugin 的 github 仓库:https://github.com/jenkinsci/pipeline-plugin/blob/master/README.md
3、pipeline 指令
jenkins pipeline 支持的指令有:
environment :用于设置环境变量,可定义在 stage 或 pipeline 部分。
tools: 可定义在 pipeline 或 stage 部分,会自动下载并安装我们指定的工具,并将其加入 PATH 变量中。
input:定义在 stage 部分,会暂停 pipeline,提示你输入内容。
options: 用于配置 jenkins pipeline 本身的选项,比如 options{retry(3)} 指当 pipeline 失败时在重试 2 次。options 指令可定义在 stage 或 pipeline 等部分。
声明式(Delar-ative)语法 &脚本式(Scripted)语法如图:

部署代码质量检测服务 sonarqube
onarQube 是一款用于代码质量管理的开源工具,它主要用于管理源代码的质量。 通过插件形式,可以支持众多计算机语言,比如 java, C#, go,C/C++, PL/SQL, Cobol, JavaScrip, Groovy 等。sonar 可以通过 PMD,CheckStyle,Findbugs 等等代码规则检测工具来检测你的代码.
下载地址:https://www.sonarqube.org/downloads/
选择社区版

上传至服务器,解压后根据平台选择启动脚本
[root@iZbp10h6o0wjk0k0c9ciy0Z opt]# cd sonarqube-8.4.2.36762/bin/
[root@iZbp10h6o0wjk0k0c9ciy0Z bin]# ll
total 16
drwxr-xr-x 2 root root 4096 Aug 27 14:30 jsw-license
drwxr-xr-x 3 root root 4096 Sep 4 10:20 linux-x86-64
drwxr-xr-x 3 root root 4096 Aug 27 14:30 macosx-universal-64
drwxr-xr-x 3 root root 4096 Aug 27 14:30 windows-x86-64
[root@iZbp10h6o0wjk0k0c9ciy0Z bin]# cd linux-x86-64/
[root@iZbp10h6o0wjk0k0c9ciy0Z linux-x86-64]# ./sonar.sh start
启动脚本运行后发现端口没起来,查看日志发现 JDK 版本过低,后来又看了最新版不支持 mysql 改用 postgres
索性就直接用 docker 部署了

docker 部署简单快速,直接拉取相关镜像
docker pull sonarqube:8.4.0-community
docker pull postgres:12.3-alpine
先启动数据库 postgres
在启动 sonar,注意避免端口冲突
发现端口还没启动,应该还有错,查看日志,发现用户内存权限太小

更改分配,正常启动
[root@iZbp10h6o0wjk0k0c9ciy0Z logs]# sysctl -a|grep vm.max_map_count
[root@iZbp10h6o0wjk0k0c9ciy0Z logs]# sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144
[root@iZbp10h6o0wjk0k0c9ciy0Z logs]# sysctl -a|grep vm.max_map_count
vm.max_map_count = 262144
默认用户密码:admin/admin
基于命令、shell 脚本和 pipline 实现代码质量检测
扩展题(选做):
jenkins 安装 Sonarqube Scanner 插件、配置 sonarqube server 地址、基于 jenkins 配置代码扫描参数实现代码质量扫描
1.安装插件SonarQube Scanner

2.sonar 生成令牌,jenkins 的凭据需要用到

3.jenkins 系统配置中添加 SonarQube servers 的配置信息

4.全局配置工具中安装 SonarQube Scanner

5.新建 sonarqube 项目 test

6.jenkins 新建自由构建项目

7.更改 jenkins 配置文件中.m2 目录下 settings.xml 和代码文件中 pom.xml
settings.xml 加入如下内容

7.更改 jenkins 配置文件中.m2 目录下 settings.xml 和代码文件中 pom.xml
settings.xml 加入如下内容

pom.xml 加入如下内容:

8.去 jenkins 部署项目,部署完毕查看 sonarqube 检测情况


Execute SonarQube Scanner
使用流程
1.开发人员的代码在自己的 IDE 和使用 SonarLint 运行局部分析。
2.开发人员推他们的代码到自己喜爱的供应链管理:SCM,SVN,TFVC,...
3.持续集成服务器触发自动构建和 SonarQube 扫描仪的运行 SonarQube 分析所需的执行。
4.分析报告被发送到 SonarQube 服务器进行处理。
5.SonarQube 服务器处理和存储分析报告导致 SonarQube 数据库,并显示结果在 UI 中。
6.开发者审核,评论,挑战他们的管理,并通过 SonarQube UI 减少他们的技术债务问题。
7.经理收到的分析报告。 OPS 使用 API 从 SonarQube 自动化配置和提取数据。 OPS 使用 JMX 来监控 SonarQube 服务器。
扫描方式一:SonarScanner 配置
修改配置信息/opt/SonarScanner/conf/sonar-scanner.properties:

扫描方式二:创建项目
在 sonar 控制台,新建项目,创建令牌,选择扫描方式。

扫描方式三:扫描项目
编写脚本执行
[root@localhost simple-java-maven-app]# ./sonar.sh
sonar-scanner -Dsonar.host.url=http://xxx.xxx.xxx.xxx:9000 \
-Dsonar.projectKey=demo-maven-service \
-Dsonar.projectName=demo-maven-service \
-Dsonar.projectVersion=1.0 \
-Dsonar.login=admin \
-Dsonar.password=xxx \
-Dsonar.ws.timeout=30 \
-Dsonar.projectDescription="my first project!" \
-Dsonar.links.homepage=http://www.baidu.com \
-Dsonar.sources=src \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.java.binaries=target/classes \
-Dsonar.java.test.binaries=target/test-classes \
-Dsonar.java.surefire.report=target/surefire-reports

版权声明: 本文为 InfoQ 作者【LiaoWD】的原创文章。
原文链接:【http://xie.infoq.cn/article/198e804246a2f4a3b74c58212】。文章转载请联系作者。
评论