一、实验环境

1)服务器系统:CentOS Linux release 7.9.2009

2)GItLab版本:GitLab Community Edition 14.9.0

3)SonarQube版本:Community Edition 版本 9.2.3  【备注:Docker 镜像mc1arke/sonarqube-with-community-branch-plugin:latest版本】

二、修改服务器系统参数

1)cat /etc/sysctl.conf

#末尾添加如下两行
vm.max_map_count = 262144
fs.file-max = 65536

2)cat /etc/security/limits.conf

#末尾添加如下内容:
*    soft    nofile    65536
*    hard    nofile    65536

3)重启服务器生效

[root@localhost~]# init  6

三、部署配置GItLab

1)  部署GItLab参考链接:Yum一键安装GItLab_岚天start的博客-CSDN博客_yum 安装gitlab

2)  GitLab 用户令牌生成

备注:必须登录具有管理员权限的账号

四、Docker部署SonarQube

1)安装Docker环境

[root@localhost~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@localhost~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
[root@localhost~]# yum clean all
[root@localhost~]# yum makecache fast
[root@localhost~]# yum -y install docker-ce
[root@localhost~]# systemctl   enable  docker  --now

或者

[root@localhost~]#  curl -sSL https://get.daocloud.io/docker | sh
[root@localhost~]#  systemctl   enable  docker  --now

2)安装Docker-compose

[root@localhost~]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
[root@localhost~]# chmod +x /usr/local/bin/docker-compose

3)配置sonarqube-docker-compose.yml

version: "3.8"

services:
  sonarqube:
    depends_on:
      - db
    image: mc1arke/sonarqube-with-community-branch-plugin:latest
    restart: always
    container_name: sonarqube
    ports:
      - 9000:9000
    networks:
      - sonarnet
    environment:
      - SONAR_JDBC_URL=jdbc:postgresql://db:5432/sonar
      - SONAR_JDBC_USERNAME=sonar
      - SONAR_JDBC_PASSWORD=sonar
    volumes:
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_logs:/opt/sonarqube/logs
  db:
    image: postgres:11
    restart: always
    container_name: postgres
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_conf:
  sonarqube_logs:
  postgresql:
  postgresql_data:

networks:
  sonarnet:

4)下载镜像、启动容器

[root@localhost~]# docker-compose   -f  docker-compose.yml   up

5)启动成功显示

浏览器访问http://192.168.33.66:9000 ,初始使用 admin/admin 登录,如下图所示

安装中文插件

重启后显示如图: 

五、配置SonarQube

1)配置SonarQube服务器地址

2)配置Gitlab授权

 

 配置成功后,可以看到GItLab所有的代码仓库

 注销账号后,登录首页可以看到【通过GItLab登录】按钮

 六、GitLab 关联 SonarQube 实现代码扫描

1)安装Runner

 注意:这里的GItLab-ci 标签是:gitlab-ci-sonarqube,后续.gitlab-ci.yml会用到

2)安装 sonar scanner

   如果 GItLab runner 执行器为 shell, 在 runner 所在服务器安装 

   在此不在赘述,安装过程可参考:GitLab 关联 SonarQube 实现CI/CD代码扫描 - 掘金

 3)配置SonarQube 和 Gitlab CI

1、以php_test代码仓库为例

 

 

2、在代码根目录下新建sonar-project.properties文件,复制上述内容

3、 GItLab上添加环境变量

 

 4、复制到GItLab代码仓库之php_test项目的根路径下的 .gitlab-ci.yml文件内

 cat   .gitlab-ci.yml

sonarqube-check:
  image: 
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
    - sonar-scanner   -Dsonar.sources=.  -Dsonar.host.url=http://192.168.33.66:9000 -Dsonar.login=b5f8eb1c37c90dphuwo3a36dd2fkr50156f3cb6e
  allow_failure: true
  only:
    - merge_requests
    - master # or the name of your main branch
    - develop
  tags:
    - gitlab-ci-sonarqube

 备注:

a、tags:gitlab-ci-sonarqube是runner的标签

b、-Dsonar.host.url和-Dsonar.login两个参数可以通过如下过程生成

-Dsonar.host.url=http://192.168.33.66:9000

-Dsonar.login=b5f8eb1c37c90dhuwo3a8d36dd2fkr50156f3cb6e

  

5、更新php_test代码仓库的matser分支的.gitlab-ci.yml文件,即触发自动扫描

6、打开SonarQube控制台,即可发现项目自动扫描结果 

七、参考链接

[1]   SonarQube学习(一)- 使用Docker安装SonarQube(亲测可用) | 程序员灯塔

[2]   Install the Server | SonarQube Docs

[3]   GitLab 关联 SonarQube 实现CI/CD代码扫描 - 掘金

[4]   Gitlab-ci:从零开始的前端自动化部署 - 知乎

[5]   Releases · mc1arke/sonarqube-community-branch-plugin · GitHub

[6]   Gitlab-CI:Gitlab-runner自动部署GItlab项目_岚天start的博客-CSDN博客

[7]   gitlab-ci.yml 配置Gitlab pipeline以达到持续集成的方法 - 简书

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐