一. 环境准备与工具链配置
1.1 Cppcheck安装与基础配置
在Jenkins服务器上安装Cppcheck是集成流程的第一步。根据操作系统环境选择适当的安装方式:
# Ubuntu/Debian sudo apt-get update && sudo apt-get install -y cppcheck # CentOS/RHEL sudo yum install -y epel-release sudo yum install -y cppcheck # macOS (通过Homebrew) brew install cppcheck验证安装是否成功:
cppcheck --version Cppcheck 1.821.2 Jenkins插件安装
在Jenkins管理界面安装以下关键插件:
- Cppcheck Plugin:用于解析和可视化报告
- Warnings Next Generation Plugin:提供高级问题跟踪功能
- Dashboard View:创建质量指标看板
1.3 Docker 创建 network
warrior@ciserver:~$ docker network create sonar-net e8b13bd477f8d17f06b63dc23f5aa7c243e17e02b472f5148d5a6e45d318e3a71.4 Docker安装 PostgreSQL容器
docker pull postgres:15 15: Pulling from library/postgres 6310eb16bf42: Pull complete 639367129460: Pull complete 248bdcd6e955: Pull complete 8d2f3610bf84: Pull complete 22bf49630d7e: Pull complete d523576e6b46: Pull complete d522ad15b8c1: Pull complete 876153b11224: Pull complete 04913c3958ed: Pull complete a849d36c92a3: Pull complete d6430d172c75: Pull complete b86905d46631: Pull complete 2659259e5615: Pull complete 8f6bdf8e7ebe: Pull complete Digest: sha256:9b1d34adbce1dd07ee6e94b4a2cf698884b89bd44a6c9c12f5da8f3acbfe4957 Status: Downloaded newer image for postgres:15 docker.io/library/postgres:15查看下载的镜像:
docker images REPOSITORY TAG IMAGE ID CREATED SIZE postgres latest a6638641707c 7 days ago 456MB hub.zentao.net/app/zentao latest e3701acc0b2a 12 days ago 741MB启动容器:
docker run --name postgresql \ --restart=always \ --network sonar-net \ -e POSTGRES_USER=admin \ -e POSTGRES_PASSWORD=123456 \ -e POSTGRES_DB=sonarqube_db \ -p 5432:5432 \ -v /opt/postgres:/var/lib/postgresql/data \ -e ALLOW_IP_RANGE=0.0.0.0/0 \ -d postgres:15参数含义如下:
- –name:后接容器名
- –restart=always:dcoker启动后,自动重启容器
- POSTGRES_USER:pg数据库用户名
- POSTGRES_PASSWORD:pg数据库密码
- POSTGRES_DB:创建一个名为sonarqube_db的pg数据库
- -p 5432:5432:端口映射
- -v:主机目录->容器目录的映射
- ALLOW_IP_RANGE=0.0.0.0/0:允许任何主机访问
- -d:守护进程方式创建容器
- 最后一个参数:镜像名。
查看镜像ID:
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f3aac1c57ad postgres "docker-entrypoint.s…" 26 seconds ago Restarting (1) 3 seconds ago postgresql进入镜像内部查看信息:
docker exec -it 1f3aac1c57ad /bin/bash Error response from daemon: Container 1f3aac1c57ad6d090ced1e743d7d0dd772db3e1ba465fcf00527ae6b1c7b1f69 is restarting, wait until the container is running warrior@ciserver:/opt$ warrior@ciserver:/opt$ warrior@ciserver:/opt$ warrior@ciserver:/opt$ docker logs 1f3aac1c57ad Error: in 18+, these Docker images are configured to store database data in a format which is compatible with "pg_ctlcluster" (specifically, using major-version-specific directory names). This better reflects how PostgreSQL itself works, and how upgrades are to be performed. See also https://github.com/docker-library/postgres/pull/1259 Counter to that, there appears to be PostgreSQL data in: /var/lib/postgresql/data (unused mount/volume) This is usually the result of upgrading the Docker image without upgrading the underlying database using "pg_upgrade" (which requires both versions). The suggested container configuration for 18+ is to place a single mount at /var/lib/postgresql which will then place PostgreSQL data in a subdirectory, allowing usage of "pg_upgrade --link" without mount point boundary issues. See https://github.com/docker-library/postgres/issues/37 for a (long) discussion around this process, and suggestions for how to do so.这个报错的意思是:你用了 PostgreSQL 18+ 的镜像,但数据卷还挂在旧路径/var/lib/postgresql/data上,新旧格式不兼容,容器拒绝启动。
核心改动:18+ 镜像不再把数据放在/var/lib/postgresql/data,而是放在带版本号的/var/lib/postgresql/18/docker
解决办法,用以下指令重新挂载:
docker run --name postgresql \ --restart=always \ --network sonar-net \ -e POSTGRES_USER=admin \ -e POSTGRES_PASSWORD=123456 \ -e POSTGRES_DB=sonarqube_db \ -p 5432:5432 \ -v /opt/postgres:/var/lib/postgresql \ -e ALLOW_IP_RANGE=0.0.0.0/0 \ -d postgresdocker exec -it a31ff7e363c1 /bin/bash root@a31ff7e363c1:/# root@a31ff7e363c1:/# root@a31ff7e363c1:/# root@a31ff7e363c1:/# root@a31ff7e363c1:/# psql -U admin -d sonarqube_db psql (18.6 (Debian 18.6-1.pgdg13+2)) Type "help" for help. sonarqube_db=# sonarqube_db=# sonarqube_db=# \l List of databases Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges --------------+-------+----------+-----------------+------------+------------+--------+-----------+------------------- postgres | admin | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | sonarqube_db | admin | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | template0 | admin | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/admin + | | | | | | | | admin=CTc/admin template1 | admin | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/admin + | | | | | | | | admin=CTc/admin (4 rows) sonarqube_db=# sonarqube_db=# sonarqube_db=# sonarqube_db=# exit root@a31ff7e363c1:/# root@a31ff7e363c1:/# exit exit1.5 Docker安装SonarQube容器
获取企业版sonarqube镜像:
docker pull sonarqube:9.9-enterprise和pg容器创建不同,这里需要手动创建主机到容器映射目录,并将其权限修改为777。
mkdir -p /opt/sonarqube/data /opt/sonarqube/extensions /opt/sonarqube/logs /opt/sonarqube/conf sudo chmod 777 -R /opt/sonarqube然后创建SonarQube容器实例:
docker run --name sonarqube \ --restart=always \ --network sonar-net \ -e SONAR_JDBC_URL=jdbc:postgresql://postgresql:5432/sonarqube_db \ -e SONAR_JDBC_USERNAME=admin \ -e SONAR_JDBC_PASSWORD=123456 \ -p 192.168.156.37:9000:9000 \ -v /opt/sonarqube/data:/opt/sonarqube/data \ -v /opt/sonarqube/extensions:/opt/sonarqube/extensions \ -v /opt/sonarqube/logs:/opt/sonarqube/logs \ -v /opt/sonarqube/conf:/opt/sonarqube/conf \ -d sonarqube:9.9-enterprise参数含义如下(同前面重复的省略):
- SONAR_JDBC_URL:pg数据库的url,ip根据主机实际情况填写
- SONAR_JDBC_USERNAME:pg数据库用户名
- SONAR_JDBC_PASSWORD:pg数据库密码
- extensions目录:sonarsube插件存放目录
- logs:日志目录
- conf: 配置目录
不出意外,容器状态会一直是restart状态,此时需要使用docker logs -f sonarqube查看对应的日志信息检查错误点。
一般是因为容器卷映射目录权限问题,或者是需要修改主机的系统参数(报错:max virtual memory areas vm.max_map_count [65530] is too low)。
权限问题好解决,直接chmod修改/opt/sonarqube目录权限为777即可。
而系统参数问题解决方法参考如下:
在主机上(宿主机,非docker容器!docker容器会继承宿主机的系统参数。)修改系统参数:
vim /etc/sysctl.conf # 最后一行添加: vm.max_map_count=262144 vm.max_map_count=524288 sysctl -p # 加载生效在ubuntu浏览器当中输入http://192.168.156.37:9000/ ,默认管理员用户及密码:admin/admin。
SonarQube汉化插件安装
进入到插件商城,搜索插件chinese,按下面操作进入到插件主页进行下载:
下载完成后,将插件拷贝到主机的/opt/sonarqube/extensions/plugins目录下(plugins目录需要手动创建),再次重启sonarqube容器,重新登录http://ip:9000/,可以看到提示变成了中文,说明我们的插件安装成功了!
生成license
将如下这段信息全部复制,然后进行base64加密就是license的内容:
Company=Unknown Digest=NotRequired Edition=Enterprise EditionLabel=Enterprise Expiration=2099-01-01 MaxLoc=9223372036854775806 Plugins=abap,cpp,plsql,security,sonarapex,swift,tsql,vbnet,cobol,pli,rpg,vb Features=* ServerId=* Support=false Type=ny0c同理,如果是开发版,只需将Enterprise替换为Developer。
Base64转换如下:
Q29tcGFueT1Vbmtub3duCkRpZ2VzdD1Ob3RSZXF1aXJlZApFZGl0aW9uPUVudGVycHJpc2UKRWRpdGlvbkxhYmVsPUVudGVycHJpc2UKRXhwaXJhdGlvbj0yMDk5LTAxLTAxCk1heExvYz05MjIzMzcyMDM2ODU0Nzc1ODA2ClBsdWdpbnM9YWJhcCxjcHAscGxzcWwsc2VjdXJpdHksc29uYXJhcGV4LHN3aWZ0LHRzcWwsdmJuZXQsY29ib2wscGxpLHJwZyx2YgpGZWF0dXJlcz0qClNlcnZlcklkPSoKU3VwcG9ydD1mYWxzZQpUeXBlPW55MGM=配置agent
首先下载agent jar包:SonarQubeAgent-1.2-SNAPSHOT.jar。
使用docker命令,将下载好的agent jar包拷贝到容器当中,这里为方便,我直接拷贝到根目录下面:
docker cp ./SonarQubeAgent-1.2-SNAPSHOT.jar sonarqube:/ Successfully copied 133kB to sonarqube:/新建sonar.properties文件
warrior@ciserver:/opt/sonarqube/conf$ touch sonar.propertieswarrior@ciserver:/opt/sonarqube/conf$ chmod 777 sonar.properties在sonarqube的配置文件conf/sonar.properties(sonar.properties文件需要手动创建),里面新增如下内容:
sonar.web.javaOpts=-javaagent:/SonarQubeAgent-1.2-SNAPSHOT.jar -Xmx1G -Xms128m -XX:+HeapDumpOnOutOfMemoryError sonar.ce.javaOpts=-javaagent:/SonarQubeAgent-1.2-SNAPSHOT.jar -Xmx2G -Xms128m -XX:+HeapDumpOnOutOfMemoryError重启sonarqube容器,然后License Manager当中填入上文获取的证书即可:
docker restart sonarqube sonarqube