代码质量持续约束 | 8lovelife's life
0%

代码质量持续约束

基于Docker的代码质量持续约束流程搭建

为了提高团队整体的代码质量,有必要搭建一套持续的代码约束流程。本文记录基于Docker如何搭建自动化代码检测流程

创建网络

方便组件间通过容器名通信

docker network create -d bridge --subnet=10.0.0.0/24 cicd_bridge

GitLab

开源的代码托管平台,集成了CI/CD

启动

docker run -it --network=cicd_bridge -p 443:443 -p 80:80 -p 22:22 --name gitlab_cicd --restart always -v ~/DockerVolumeCicd/gitlab/config:/etc/gitlab -v ~/DockerVolumeCicd/gitlab/logs:/var/log/gitlab -v ~/DockerVolumeCicd/gitlab/data:/var/opt/gitlab -d gitlab/gitlab-ce:latest

访问 http://127.0.0.1

首次登陆设置密码为:adminadmin ,使用 root用户名登陆

创建一个spring 测试项目

设置 GitLab 扩展url 并 重启

vi ~DockerVolumeCicd/gitlab/config/gitlab.rb

docker restart gitlab_cicd

GitLab Runner

GitLab Runner用于运行.gitlab-ci.yml中定义的Jobs,Jobs的执行结果会被发送到GitLab,GitLab Runner与GitLab CI(持续集成服务) 集成使用。GitLab Runner由Go语言实现

启动

docker run -it --network=cicd_bridge --name gitlab-runner_cicd --restart always -v /var/run/docker.sock:/var/run/docker.sock -v ~/DockerVolumeCicd/gitlab-runner/conf:/etc/gitlab-runner/ -d gitlab/gitlab-runner:latest

获取 Runner Token


注册 Runner

docker exec -it gitlab-runner_cicd gitlab-ci-multi-runner register

或者命令指定注册: docker exec -it gitlab-runner_cicd gitlab-ci-multi-runner register --non-interactive --executor "docker" --docker-image maven:3.5-jdk-8 --url "http://gitlab_cicd" --registration-token "NDcNdmx5jaQxka8Ua3sn" --description "docker-runner" --tag-list "whw" --run-untagged --locked="false" --docker-network-mode =cicd_bridge

配置注册信息

vi ~/DockerVolumeCicd/gitlab-runner/conf/config.toml

SonarQube

SonarQube是一个代码质量管理系统,支持多种语言分析,提供重复代码、编码标准、单元测试、代码覆盖率、代码复杂度、潜在Bug的报告分析

启动

docker run -it --network=cicd_bridge -p 9000:9000 --name sonarqube_cicd --restart always -v ~/DockerVolumeCicd/sonar/conf:/opt/sonarqube/conf -v ~/DockerVolumeCicd/sonar/data:/opt/sonarqube/data -v ~/DockerVolumeCicd/sonar/logs:/opt/sonarqube/logs -v ~/DockerVolumeCicd/sonar/extensions:/opt/sonarqube/extensions -d sonarqube

访问 http://127.0.0.1:9000

user/pwd 默认为 admin/admin

首次登陆配置


安装Java语言插件 AND 刷新页面Restart SonarQube


.gitlab-ci.yml

在项目的根目录添加.gitlab-ci.yml文件,用于定义Jobs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
image: maven:3.5-jdk-8
stages:
- build
- test
- sonarqube

variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
paths:
- .m2/repository
- target/

build:
stage: build
only:
- release-test
- develop
script:
- mvn clean install -Dmaven.test.skip
tags:
- whw

test:
stage: test
only:
- release-test
- develop
script:
- mvn test
tags:
- whw
allow_failure: true

sonarqube:
stage: sonarqube
only:
- release-test
- develop
script:
- mvn sonar:sonar -Dsonar.host.url=http://sonarqube_cicd:9000 -Dsonar.language=java
tags:
- whw

GitLab & GitLab Runner & GitLab CI

当有项目代码提交到GitLab的时候,GitLab会通知到GitLab CI,GitLab CI根据配置的Jobs将任务分发给指定的GitLab Runner,GitLab Runner 执行配置好的的Piplelines

Boys, you must strive to find your own voice. Because the longer you wait to begin, the less likely you are to find it at all. - John

Dead Poets Society