#1.centos8系统环境准备:
ip 主机名 软件
10.0.0.170 sonarqube sonarqube 8.9.9LTS postgresql-10.17
10.0.0.160 jenkins-master jenkins-2.346.3 sonar-scanner-4.7
#sonarqube 7.9.x版本后不在支持mysql。
#sonarqube 6.7.xLTS版本要求数据库需要使用mysql5.6及以上版本,不支持5.5及更早版本。使用java8,不支持更低或者更高版本
#2.安装postgresql
[root@sonarqube ~]#yum install -y postgresql-server
#初始数据库
[root@sonarqube ~]#/usr/bin/postgresql-setup initdb
WARNING: using obsoleted argument syntax, try --help
WARNING: arguments transformed to: postgresql-setup --initdb --unit postgresql
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
#启动数据库
[root@sonarqube ~]#systemctl enable --now postgresql
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
#进入数据库配置sonar连接的数据库,用户和授权
[root@sonarqube ~]#su - postgres
[postgres@sonarqube ~]$psql
psql (10.17)
Type "help" for help.
postgres=# create database sonar;
CREATE DATABASE
postgres=# create user sonar with encrypted password '123456';
CREATE ROLE
postgres=# grant all privileges on database sonar to sonar;
GRANT
postgres=# alter database sonar owner to sonar;
ALTER DATABASE
#配置监听地址
[root@sonarqube ~]#vim /var/lib/pgsql/data/postgresql.conf
listen_addresses = '*'
#配置允许远程连接,添加一行
[root@sonarqube ~]#vim /var/lib/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 md5
#重启数据库
[root@sonarqube ~]#systemctl restart postgresql.service
#验证数据库端口
[root@sonarqube ~]#ss -tnl |grep 5432
LISTEN 0 128 0.0.0.0:5432 0.0.0.0:*
#3.安装sonarqube
#3.1到官网下载包并传到sonarqube服务器
https://www.sonarqube.org/downloads/
#3.2配置系统及内核参数
[root@sonarqube ~]#vim /etc/sysctl.conf
vm.max_map_count = 262144
fs.file-max = 65536
[root@sonarqube ~]#sysctl -p
[root@sonarqube ~]#vim /etc/security/limits.conf
sonarqube - nofile 65536
sonarqube - nproc 2048
#3.3安装
[root@sonarqube ~]#yum install -y java-11-openjdk
[root@sonarqube local]#cd /usr/local/src/
[root@sonarqube src]#unzip sonarqube-8.9.9.56886.zip
[root@sonarqube local]#ln -sv /usr/local/src/sonarqube-8.9.9.56886 /usr/local/sonarqube
'/usr/local/sonarqube' -> '/usr/local/src/sonarqube-8.9.9.56886'
[root@sonarqube src]#useradd -u 2020 sonarqube
[root@sonarqube src]#echo 123456 |passwd --stdin sonarqube
Changing password for user sonarqube.
passwd: all authentication tokens updated successfully.
[root@sonarqube src]#chown -R sonarqube.sonarqube /usr/local/sonarqube/ /usr/local/src/sonarqube-8.9.9.56886
[root@sonarqube src]#su - sonarqube
[sonarqube@sonarqube ~]$vim /usr/local/sonarqube/conf/sonar.properties
[sonarqube@sonarqube ~]$grep "^[a-Z]" /usr/local/sonarqube/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:postgresql://10.0.0.170/sonar
[sonarqube@sonarqube ~]$/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
Starting SonarQube...
Started SonarQube.
#报错线程太低,最低4096
[sonarqube@sonarqube ~]$cat /usr/local/sonarqube/logs/sonar.20220906.log
bootstrap check failure [1] of [1]: max number of threads [2048] for user [sonarqube] is too low, increase to at least [4096]
#改为4096
[root@sonarqube ~]#vim /etc/security/limits.conf
sonarqube - nofile 65536
sonarqube - nproc 4096
#3.4查看日志,启动成功
[sonarqube@sonarqube ~]$cat /usr/local/sonarqube/logs/sonar.20220906.log
2022.09.06 09:27:13 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up
2022.09.06 09:27:13 INFO app[][o.s.a.SchedulerImpl] SonarQube is up
#3.5访问网页地址10.0.0.170:9000。
默认账户密码都是admin,第一次登录修改密码为123456
#3.6安装中文插件。
sonarqube--administration--marketplace--了解风险--搜索chinese,在chinese pack插件右边点击安装。重启sonarqube。再次登录就是中文界面了。
#3.7关闭强制认证。
sonarqube--配置-配置-通用设置--权限--Force user authentication--关闭--保存
#4.jenkins部署sonar-scanner
#4.1下载sonar-scanner
https://docs.sonarqube.org/8.9/analysis/scan/sonarscanner/
#4.2在jenkins上安装sonar-scanner
[root@jenkins-master ~]#mv sonar-scanner-cli-4.7.0.2747-linux.zip /usr/local/src/
[root@jenkins-master ~]#cd /usr/local/src/
[root@jenkins-master src]#unzip sonar-scanner-cli-4.7.0.2747-linux.zip
[root@jenkins-master src]#ln -sv /usr/local/src/sonar-scanner-4.7.0.2747-linux/ /usr/local/sonar-scanner
'/usr/local/sonar-scanner' -> '/usr/local/src/sonar-scanner-4.7.0.2747-linux/'
[root@jenkins-master src]#cd /usr/local/sonar-scanner/
[root@jenkins-master sonar-scanner]#ls
bin conf jre lib
[root@jenkins-master sonar-scanner]#vim conf/sonar-scanner.properties
#----- Default SonarQube server
#sonar.host.url=http://localhost:9000
sonar.host.url=http://10.0.0.170:9000
#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
sonar.sourceEncoding=UTF-8
#4.3在上面的官网下载sonar-scanning-examples-master.zip示例文件
[root@jenkins-master ~]#unzip sonar-scanning-examples-master.zip
[root@jenkins-master sonarqube-scanner]#pwd
/root/sonar-scanning-examples-master/sonarqube-scanner
[root@jenkins-master sonarqube-scanner]#grep "^[a-z]" sonar-project.properties
sonar.projectKey=org.sonarqube:sonarqube-scanner #自定义项目key
sonar.projectName=Example of SonarQube Scanner Usage #项目名称,会显示在web
sonar.projectVersion=1.0 #项目版本
sonar.sources=src,copybooks #源代码目录
sonar.sourceEncoding=UTF-8 #编码格式
sonar.cobol.copy.directories=copybooks
sonar.cobol.file.suffixes=cbl,cpy
sonar.cobol.copy.suffixes=cpy
sonar.flex.cobertura.reportPath=coverage-report/coverage-cobertua-flex.xml
sonar.pli.marginLeft=2
sonar.pli.marginRight=0
#4.4在配置目录执行扫描
[root@jenkins-master sonarqube-scanner]#/usr/local/sonar-scanner/bin/sonar-scanner
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 26.889s
INFO: Final Memory: 81M/270M
INFO: ------------------------------------------------------------------------
#5.jenkins执行代码扫描
#5.1jenins安装sonarqube插件
jenkins--系统管理--插件管理--可选插件--sonarqube scanner
安装完成后重启jenkins
#5.2jenkins上配置sonarqube servers
jenkins--系统管理--系统配置--sonarqube servers--添加sonarqube
name:sonarqube-server
server url:http://10.0.0.170:9000
#5.3jenkins上配置sonar scanner扫描器
Jenkins--系统管理--全局工具配置--sonarqube scanner--新增sonarqube scanner
name:sonarqube-scanner
SONAR_RUNNER_HOME:/usr/local/src/sonar-scanner-4.7.0.2747-linux/
或者勾选“自动安装”
#5.4将代码上传gitlab
[root@jenkins-master ~]#git clone -b develop [email protected]:linux39/web1.git
[root@jenkins-master ~]#mv src/ web1/
[root@jenkins-master ~]#cd web1/
[root@jenkins-master web1]#vim index.html
#添加一行v7的数据
<h1>test v1</h1>
<h1>test v2</h1>
<h1>test v3</h1>
<h1>test v4</h1>
<h1>test v5</h1>
<h1>test v6</h1>
<h1>test v7</h1>
[root@jenkins-master web1]#git add .
[root@jenkins-master web1]#git commit -m "v7"
[root@jenkins-master web1]#git push
#5.5配置扫描
选择主机的项目--配置--构建--新增构建步骤--Execute SonarQube Scanner
其他配置默认
Analysis properties:
sonar.projectKey=job-test
sonar.projectName=job-test
sonar.projectVersion=1.0
sonar.sources=src
sonar.sourceEncoding=UTF-8
保存后测试构建完成
#构建中两个框,扫描代码和shell目录,拖东扫描代码的框到上面,先git克隆后,再扫描,再部署。
#5.6可以在sonarqube图形界面查看扫描结果
标签:scanner,--,Sonarqube,扫描,sonarqube,sonar,jenkins,root
From: https://www.cnblogs.com/tanll/p/17748074.html