Sonar搭建过程记录

431 阅读3分钟

官网:SONARQUBE

默认系统环境: AlibabaCloud+JAVA1.8(JAVA 8)

[root@iZuf6eyc5l2cngqmbv09npZ ~]# lsb_release  -a
LSB Version:	:core-4.1-amd64:core-4.1-noarch
Distributor ID:	AlibabaCloud
Description:	Alibaba Cloud Linux release 3 (Soaring Falcon) 
Release:	3
Codename:	SoaringFalcon

先在官网下载对应版本的sonar安装包,上传到服务器,解压到/opt/sonarqube。关闭防火墙。

关闭防火墙:
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/g' /etc/selinux/config
setenforce 0

JAVA升级到17:

官网可见,新版的sonarqube都是要求JAVA17的,所以先把JAVA8升级到17。 参考文章

  1. 用yum安装JDK
yum list installed | grep java // 检查系统中的JAVA版本
yum install -y java-17-openjdk //安装17版本的JDK

whereis java //查看JAVA安装位置
  1. 配置环境变量
vim /etc/profile 编辑所有用户系统文件

#jdk环境变量
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-17.0.11.0.9-2.0.1.1.al8.x86_64
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib

source /etc/profile 应用所有用户系统文件配置
  1. 查看JAVA版本

echo $JAVA_HOME 看看路径是否正确

[root@iZuf6eyc5l2cngqmbv09npZ ~]# java -version
openjdk version "17.0.11" 2024-04-16 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.11.0.9-3) (build 17.0.11+9-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.11.0.9-3) (build 17.0.11+9-LTS, mixed mode, sharing)

注意点一:不能用管理员账号登录

groupadd sonarqube //新建组
useradd sonarqube -g sonarqube -p sonarqube //新建用户 用户名和密码均为sonarqube

chown -R sonarqube sonarqube-9.7.0.61563/ //用于更改安装包解压后的路径文件或目录的所有者
 
chgrp -R sonarqube sonarqube-9.7.0.61563/ //用于更改安装包解压后的路径文件或目录的所属组

注意点二:7.9版本后不支持MySQL,需要安装postgresql

安装postgresql

yum install -y https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

yum install -y postgresql13 postgresql13-server

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

yum install -y postgresql13-server
/usr/pgsql-13/bin/postgresql-13-setup initdb

systemctl enable postgresql-13
systemctl start postgresql-13

创建数据库

#先切换到postgres用户  
su - postgres

执行创建指令

psql
create user sonarqube with password ‘sonarqube’;
create database sonarqube owner sonarqube;
grant all on database sonarqube to sonarqube;
\q
su -

进入sonar的conf目录,编辑conf文件

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube

启动:

image.png

9000端口一定要打开。

查看启动日志: tail -f …/…/logs/sonar.log

image.png

针对以上问题做如下操作:

修改/etc/sysctl.conf文件,文件末尾追加vm.max_map_count=262144。

浏览器访问:http://你的服务器ip:9000

登录用户admin 密码admin

汉化包可以直接到插件市场安装。

其他需要用到的LINUX命令:

lsb_release -a 查看系统版本
netstat -tuln | grep 9000 端口占用情况

vim /etc/profile 编辑所有用户系统文件
source /etc/profile 应用所有用户系统文件配置

env 查看环境变量列表

alternatives --config java 查看已安装的JAVA版本 不过这个不太好用

unzip 解压命令 需要安装

mv a b 移动a到b

ps -ef | grep sonar 查看sonar相关进程

本地跑通

创建项目,选择本地:

image.png

image.png

创建一个令牌,安装教程安装本地sonarCLI,推送即可。

连接GItLab

需要在Gitlab上有一个runner。 见GitLab PipeLine配置

新建一个项目,选择使用GitLabCI:

image.png

image.png 按照教程操作即可。

  1. 在仓库中创建 sonar-project.properties 文件,粘贴以下代码:

    复制

    sonar.projectKey=-
    sonar.qualitygate.wait=true
    

2.更新Git LabCI配置。 我按照官网的配置总是报错get-binaries不存在,之后再处理这个问题吧,猜测是安装runner的时候漏掉了什么。我自己找了一个配置:

sonarqube-check:
  image: 
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # 缓存SonarQube插件
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
    - sonar-scanner
      -Dsonar.projectKey=projectKey
      -Dsonar.sources=.
      -Dsonar.host.url=url
      -Dsonar.login=${SONAR_TOKEN}
      -Dsonar.scanner.timeoutSeconds=300
  allow_failure: true
  
  only:
    - merge_requests
    - me_cicd_build

之后在特定的分支上提交代码:

image.png

流水线就跑通啦,回到sonar可以看到扫码结果:

image.png

报错一:

image.png

原因:sonarqube没有取得sonar目录的全部权限。

sudo chown -R sonarqube:sonarqube /opt/sonarqube-10.6.0.92116/*

报错二:

image.png

查找es日志:

image.png

磁盘超限。

查看所在磁盘占用情况

df -h /opt/sonarqube-10.6.0.92116/data/es8


[root@XXX es8]# du -sh /opt/sonarqube-10.6.0.92116
1.3G    /opt/sonarqube-10.6.0.92116