基于Drone来作CI/CD,我的感受真的很棒,相对于业界老大哥jekins,我更喜欢drone,相比较而言,我以为它主要有如下优点java
对于咱们后端的java项目而言,咱们CI要作什么呢?git
通常提交代码流程以下github
- Clone 项目到本地,建立一个分支来完成新功能的开发, git checkout -b feature/sync-status。在这个分支修改一些代码
- git add .,书写符合规范的 Commit 并提交代码, git commit -m "sync article status”
- 将代码推送到代码库的对应分支, git push origin feature/sync-status
- 若是功能已经开发完毕,能够向 Develop(或者Master) 分支发起一个 Pull Request,并让项目的负责人 Code Review
- Review 经过后,项目负责人将分支合并入主干分支
从上图中能够看到当咱们提交代码时,会执行整个CI流程,须要注意的有如下2点web
先看下SonarQube回写到github中的的检查结果是怎样的面试
当sonar qube检测完成以后,会将检查结果经过oauth的方式发送给github,因此你须要在github中建立Personal access token(这个要记下来)后端
当你激活你的代码仓库时,Drone会自动将Webhooks添加到版本控制系统中,例如GitHub,而无需手动配置api
kind: pipeline
name: default
steps:
# build for push and pull_request
- name: build-pr
image: maven:latest
pull: if-not-exists
commands:
- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.skip=true -s settings.xml
when:
branch:
- feature/*
- issue/*
- develop
event:
- push
- pull_request
- name: unittest
image: maven:latest
pull: if-not-exists
commands:
- mvn test -s settings.xml
when:
branch:
- develop
event:
include:
- pull_request
- push
# 这里咱们使用命令来深度定制咱们的扫描,而不是使用drone sonar plugin
- name: sonar-scan
image: newtmitch/sonar-scanner:4.0.0-alpine
environment:
SONAR_TOKEN:
from_secret: sonar_token
GITHUB_ACCESS_TOKEN_FOR_SONARQUBE:
from_secret: github_access_token_for_sonarqube
commands:
- > sonar-scanner -Dsonar.host.url=https://sonarqube.company-beta.com/ -Dsonar.login=$$SONAR_TOKEN -Dsonar.projectKey=smcp-service-BE -Dsonar.projectName=smcp-service-BE -Dsonar.projectVersion=${DRONE_BUILD_NUMBER} -Dsonar.sources=src/main/java -Dsonar.tests=src/test/java -Dsonar.language=java -Dsonar.java.coveragePlugin=jacoco -Dsonar.modules=smcp-api,smcp-web -Dsonar.java.binaries=target -Dsonar.projectBaseDir=. -Dsonar.analysis.mode=preview -Dsonar.github.repository=Today_Group/SMCP-Service -Dsonar.github.oauth=$$GITHUB_ACCESS_TOKEN_FOR_SONARQUBE -Dsonar.github.pullRequest=${DRONE_PULL_REQUEST} -Dsonar.github.disableInlineComments=false when:
event:
- pull_request
branch:
- develop
# post sonarscan result back to git PR (not in preview mode)
- name: sonar-scan-feedback
image: newtmitch/sonar-scanner:4.0.0-alpine
environment:
SONAR_TOKEN:
from_secret: sonar_token
GITHUB_ACCESS_TOKEN_FOR_SONARQUBE:
from_secret: github_access_token_for_sonarqube
commands:
- > sonar-scanner -Dsonar.host.url=https://sonarqube.company-beta.com/ -Dsonar.login=$$SONAR_TOKEN -Dsonar.projectKey=smcp-service-BE -Dsonar.projectName=smcp-service-BE -Dsonar.projectVersion=${DRONE_BUILD_NUMBER} -Dsonar.sources=src/main/java -Dsonar.tests=src/test/java -Dsonar.language=java -Dsonar.java.coveragePlugin=jacoco -Dsonar.modules=smcp-api,smcp-web -Dsonar.java.binaries=target -Dsonar.projectBaseDir=. -Dsonar.analysis.gitRepo=Today_Group/SMCP-Service -Dsonar.analysis.pullRequest=${DRONE_PULL_REQUEST} when:
event:
- pull_request
branch:
- develop
复制代码
上面drone的配置就是整个CI的基本流程了,须要注意的有如下几点安全
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
复制代码
其余可能遇到的问题:bash
答: 没有p3c的插件,可是能够经过PMD来进行集成
集成p3c: www.jianshu.com/p/a3a58ac36… 自定义checkstyle: www.jianshu.com/p/a3a58ac36…
答: drone提供了webhooke的plugin,你只须要编写本身统计的程序就能够了,能够根据模板设置须要发送的信息
答: 能够本身写一个插件,官网有bash/go的示例,用你熟悉的语言也是能够的
往期推荐阅读