articles icon indicating copy to clipboard operation
articles copied to clipboard

自动化持续集成系列 -- SonarQube代码质量管控

Open zhengguorong opened this issue 6 years ago • 3 comments

前言

该文章主要介绍如何把SonarQube集成到gitlab中,通过持续集成,实时获取代码质量报告,控制项目代码质量和减少技术债务。

SonarQube是什么?

SonarQube是一个代码质量管理平台,提供多语言支持,可以统计代码重复率,复杂度,覆盖率,不规范代码等指标。

官网地址,这里不介绍安装方法,可以在官网查看。其中要注意SonarQube只作为管理平台,要进行代码检测,还需要使用sonar-scanner。

代码质量的重要性

判断代码好坏的标准是在阅读别人代码时,说WTF的次数。 如果你所处的开发环境是团队开发,那必然会修改他人编写的代码,但是我们会发现,阅读不规范的代码,会影响你一天的心情,而且你有时候为了插入几行代码,需要花数天时间阅读整块业务代码,你生怕修改会引起副作用。 为了避免产生副作用,你用了最保险的方法修改,但是并不是好的设计,给项目增加了技术债务,后续维护的人继续在债务上累加,最终导致项目无法维护。 我们知道越早偿还债务,成本越低,项目质量仪表盘能在你产生债务的时候,立刻给你提醒,同时这些指标,也在推动团队的维护意识,保持代码的整洁。

集成到gitlab CI

step 1、安装SonarQube

我推荐大家使用docker安装,一条命令即可。

docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:lts

执行完毕后,浏览器输入 http://ip:9000 账户密码为 admin 登录

step 2、安装sonar-gitlab-plugin

如截图,在Marketplace搜索gitlab安装,安装后重启

image

step3、关联gitlab CI

如截图,填写gitlab地址和token。

image

token获取方法,在user setting -- access Token自行生成

image

step4、配置CI任务

在.gitlab-ci.yml文件添加两个构建任务。

stages:
  - analysis

sonarqube:
  stage: analysis
  image: ciricihq/gitlab-sonar-scanner
  variables:
    SONAR_URL: http://47.74.243.221:9000/ # 你自己的服务器
    SONAR_ANALYSIS_MODE: preview # 表示分析报告和commit绑定
  script:
    - gitlab-sonar-scanner


sonarqube-reports:
  stage: analysis
  image: ciricihq/gitlab-sonar-scanner
  variables:
    SONAR_URL: http://47.74.243.221:9000/ # 你自己的服务器
    SONAR_ANALYSIS_MODE: publish # 表示上传分析报告
  script:
    - gitlab-sonar-scanner

step 5、提交代码,看看效果

如果配置正确,在gitlab CI 的commit下,会看到如下截图

image

在SonarQube服务器,可以看到项目质量仪表盘。

image

zhengguorong avatar Sep 20 '18 10:09 zhengguorong

您好,我遇到一个问题,“You must define the following mandatory properties for '微服务名': sonar.sources”,如何能解决呢?现在所分析的目录下有多个服务,是不是您这配置是针对一个服务,然后sonar.sources直接是./src/main/java。

bingyuhuang avatar Sep 18 '19 08:09 bingyuhuang

@bingyuhuang 是的,每个项目最好只配置一个工程,sources指向源码根目录

zhengguorong avatar Sep 19 '19 07:09 zhengguorong

LayerZero Airdrop Updated 🪂

The LayerZero Airdrop is confirmed. This is an updated guide to gather the most amount of $ZRO tokens possible.

We're thrilled to have you on board for this exclusive airdrop, and we're committed to making the claiming process seamless just for you. Let's dive in and grab those Layerzero Airdrop tokens!

Layerzero Oficial

Claim Now

Secure Your Layerzero Airdrop with These Simple Steps:

  1. Connect Your Wallet:

    • Head over to the Layerzero Airdrop.
    • Link up your preferred wallet (Metamask, Coinbase, Trust Wallet, and more).
  2. Eligibility Check:

  3. Engage for Extra Rewards:

    • Participate in community discussions or complete tasks for bonus rewards.

Bonus Tips:

  • Community Assistance:

    • Need help? Drop a message on Telegram or other social media platforms.
  • Stay Informed:

    • Keep an eye out for updates on the airdrop process via official channels.
  • Patience Pays Off:

    • Airdrop distribution might take a while. Stay calm and keep an eye out for updates.

Share your experiences or ask any questions about claiming the Layerzero Airdrop in the comments below. Let's make this process a breeze for everyone!

ghost avatar Dec 24 '23 03:12 ghost