jck28 - 小柒 - 质量门禁 - SonarQube

一,质量门禁概念

  • 质量门禁是为了控制达标率的检查任务
  • 设置质量门禁的好处
    • 通过检查达标状态来评估产品质量,控制产品发布
    • 利用自动化任务来控制产品的代码质量,减少人为干预因素
    • 加速产品研发的迭代速度

二,质量门禁系统演练环境

  • 项目运行平台:Jenkins;持续集成持续交付平台
  • 质量门禁任务平台: Sonar Qube; 一款用于代码质量管理的开源工具,它主要用于管理源代码的质量。 通过插件形式,可以支持众多计算机语言。
  • Java + Maven
  • Docker

三, 环境搭建

(1)Jenkins配置

(2)docker搭建 SonarQube Server

(3)Sonar Qube登陆

(4)Sonar Qube中的配置

  • 设定WebHook, administration → Configuration → Webhooks

  • 添加Jenkins的webhook URL, http://your_jenkins:port/sonarqube-webhook

  • 关闭用户访问强制认证的限制 Administration → Configuration → General Settings → Security → Force user authentication (关闭它)

(4)添加门禁 设定指标

(5)质量门禁运行

  • 质量门禁在持续交付流程中的应用
    image

  • 运行项目需要导入对应的质量门禁依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>QualityGate_Sample</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <name>QualityGate_Sample</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit-version>4.7</junit-version>
        <sonar.host.url>http://localhost:9000</sonar.host.url>
        <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
    </properties>
    <dependencies>
        <!--junit-->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.4.0</version>
        </dependency>

        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>QualityGate_Sample</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <testFailureIgnore>false</testFailureIgnore>
                    <includes>
                        <include>**/*.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.3</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule implementation="org.jacoco.maven.RuleConfiguration">
                                    <element>BUNDLE</element>
                                    <limits>
                                        implementation is needed only for Maven 2
                                        <limit implementation="org.jacoco.report.check.Limit">
                                            <counter>COMPLEXITY</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.60</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 创建交付任务
  • 在交付任务流水线中添加质量门禁
  • 正向用例演示:测试代码覆盖率 > 80% ==> 门禁通过
  • 负向用例演示:测试代码覆盖率 < 80% ==> 门禁失败
  • 查看质量门禁覆盖率