背景
- 公司有多个开发团队(java\c#\android)需要做持续集成
- 代码质量评估基本没有
- 使用以上在线服务可以做样例,在实际私有部署时有开源方案(jenkins & sonarqube)
准备工作
- 需要有一个github账户
- 有一个可用的java项目代码
- maven(pom.xml)编译ok
sonarcloud
- 使用github账户登录https://about.sonarcloud.io/
- 浏览自己的organization名并记录,一般为:'github用户名-github',对应后面变量$SONAR_ORG
- 'My account' - 'security' 创建一个token并记录,对应后面变量$SONAR_TOKEN
travis
- 使用github账户登录travis注册
- 在github java项目中编写.travis.yml,举例如下
dist: trusty
sudo: required
language: java
addons:
sonarcloud:
organization: "$SONAR_ORG"
token:
secure: "$SONAR_TOKEN"
jdk:
- oraclejdk8
script:
# JaCoCo is used to have code coverage, the agent has to be activated
- mvn install -Dmaven.test.skip=true
- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar
cache:
directories:
- '$HOME/.m2/repository'
- '$HOME/.sonar/cache'
git commit and push
- 登录travis,‘add new repository’ - 添加在github上的此java项目
- ‘more options’ - 'settings' - 'Environment Variables'
增加两个变量SONAR_ORG 和 SONAR_TOKEN,分别对应sonarcloud中记录的值 - rebuild 并检查结果
![](https://img.haomeiwen.com/i2232496/0d5e6d76c7131e99.png)
sonarcloud
登录sonarcloud可看到代码评估情况
![](https://img.haomeiwen.com/i2232496/5e23f68dee1abd45.png)
给github项目加徽章
- 在java项目根目录中readme.md文件中最上面加入类似如下代码
[![Build Status](https://travis-ci.org/4admin2root/myhomework.svg?branch=master)](https://travis-ci.org/4admin2root/myhomework)
[![Quality Gate](https://sonarcloud.io/api/badges/gate?key=spring.boot:cloud-simple-helloworld)](https://sonarcloud.io/dashboard/index/spring.boot:cloud-simple-helloworld)
私有部署的几个要点
- 使用jenkins代替travis, 使用jenkinsfile方式代替.travis.yml
- jenkins 安装sonarqube plugin
- 下载sonarqube 并安装,也可使用我做的docker image
➜ sonarqube git:(master) ✗ cat envfile
SONARQUBE_JDBC_USERNAME=sonar
SONARQUBE_JDBC_PASSWORD=sonar
SONARQUBE_JDBC_URL=jdbc:mysql://10.8.32.30:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
➜ sonarqube git:(master) ✗ # 修改mysql 参数
mysql> set global max_allowed_packet = 16777216;
Query OK, 0 rows affected (0.00 sec)
➜ sonarqube git:(master) ✗ docker run -d -p 9000:9000 --name sonarqube --env-file envfile 4admin2root/sonarqube:5.6.6
参考
https://docs.travis-ci.com/user/sonarcloud/
https://about.sonarcloud.io/get-started/
网友评论