SonarQube结合GitLab CI/CD自动检测代码【图文详述】

2 篇文章 0 订阅
1 篇文章 0 订阅

一、实验环境

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以达到持续集成的方法 - 简书

  • 17
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 以下是一个简单的微信自动回复的超链接代码,可以在回复消息时自动跳转到某个网站: ```html <a href="https://www.example.com/" target="_blank"><img src="https://www.example.com/image.png" alt="Image"></a> ``` 其中,`https://www.example.com/` 是你要跳转的网站链接,`https://www.example.com/image.png` 是你要显示的图片链接,`Image` 是图片的描述文字。你可以根据需要进行修改。 ### 回答2: 当用户给你发送消息时,我们可以编写一个简单的HTML代码来实现微信自动回复的功能。 首先,你需要创建一个HTML文档,并在`<head>`标签中引入jQuery库的链接,如下所示: ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>微信自动回复</title> <script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script> </head> <body> <script> $(document).ready(function() { $('body').on('DOMNodeInserted', '.message', function() { // 检测到新消息 var messageText = $(this).find('.text').text(); // 获取收到的消息内容 var replyText = '这是自动回复的内容。'; // 设置自动回复的内容 // 添加回复消息 $(this).parent().append('<div class="message you"><a href="https://www.example.com">' + replyText + '</a></div>'); }); }); </script> </body> </html> ``` 在上述代码中,我们使用了jQuery库来简化操作。当页面加载完成后,通过`$('body').on('DOMNodeInserted', '.message', ...)`来监听新消息的插入。一旦检测到有新消息,我们从消息中获取文本内容,并设置自动回复的内容。最后,我们使用`.append()`方法将回复消息添加到对话框中,并通过`<a>`元素将其设置为一个超链接。 请将`https://www.example.com`替换为你要设置的超链接的地址,然后保存并部署这段代码。当有新消息到达时,它将自动回复一个包含超链接的文本消息。 ### 回答3: 下面是一个简单的微信自动回复的HTML图文超链接代码示例: ```html <!DOCTYPE html> <html> <head> <title>微信自动回复</title> </head> <body> <h1>微信自动回复</h1> <p>点击以下链接获取更多信息:</p> <ul> <li><a href="https://example.com/page1">页面1</a></li> <li><a href="https://example.com/page2">页面2</a></li> <li><a href="https://example.com/page3">页面3</a></li> </ul> </body> </html> ``` 这个代码会显示一个标题为"微信自动回复"的h1标签,以及一个包含三个超链接的无序列表。每个超链接都指向不同的页面,你可以将页面链接修改为你的实际需要的链接。 要使用这个代码,你只需要将它保存为一个HTML文件,并将文件上传到公共的服务器或使用相关的工具将其部署到微信自动回复服务中。当用户发送消息并触发自动回复时,他们将收到这个HTML页面的内容,并可以点击其中的链接获取更多信息。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值