It is really great to do CI/CD based on Drone. Compared with the big brother Jekins in the industry, I prefer Drone. Comparatively speaking, I think it has the following advantages

  1. Plug-ins require no additional management
  2. Yaml file based, easy to write, configuration can be version management
  3. You can build for different conditions
  4. More user-friendly UI interface

For our back-end Java project, what do we CI do?

The general code submission process is as follows

  1. Clone project to local, create a branch to complete the new functionality, git checkout -b feature/sync-status. Modify some code in this branch
  2. Git Commit -m “sync article status”
  3. Git push Origin feature/sync-status git push origin feature/sync-status
  4. If the functionality is already developed, send a Pull Request to the Develop(or Master) branch and have the project lead Code Review it
  5. After the Review was approved, the project leader merged the branches into the main branch

As you can see from the figure above, when we submit the code, the entire CI process is executed, with the following two points to note

  1. When a build or Unit test fails, a message is sent to Slack, so developers can notice the problem. You can also send an email or wechat message
  2. When SonarQube check is executed, if there is a problem, the results are written back to Github and the developer looks at the problem

What are the results of SonarQube’s github check

Sonar Qube will send the results to Github via Oauth, so you’ll need to create a Personal Access token on Github (note this)

When you activate your repository, Drone automatically adds Webhooks to version control systems, such as GitHub, without having to manually configure them

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

# Here we use commands to deeply customize our scans instead of using the 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

Copy the code

The configuration of drone above is the basic process of the whole CI. The following points need to be paid attention to

  1. The above steps will only be triggered if the branch name starts with feature/,issue/,develop. For Unit test, only the Develop branch will work (you can customize as needed).
  2. Sonar configuration of sonar projectKey, sonar. The projectName must be with you in sonar server (sonar. Host. Url specified address) of the same name when creating the project
  3. The value of sonar_token is created on sonar server, and then set this value in the secrets of drone (click a warehouse in drone, enter Settings to set)
  4. Github token and Sonar_token are in the same way, both need to be pre-set in drone (the advantage is that you won’t expose your password in the file, which is more secure).
  5. Because the Java project being used is a multi-module project, you can specify multiple module names in sonary.modules
  6. Sonar Scan feedback content to PR do not specify preview mode
  7. Jacoco (analyzing unit test coverage) was used in build, so the plugin needs to be introduced in pom.xml in your Java project
<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>
Copy the code

Other problems you may encounter:

  1. How to send emails or messages to wechat group after CI execution is completed

A: Drone offers plugins for email and wechat

  1. Can Sonarqube integrate alibaba’s P3C or custom CheckStyle

A: There is no plug-in for P3C, but it can be integrated through PMD

Integrated p3c: www.jianshu.com/p/a3a58ac36… Custom checkstyle: www.jianshu.com/p/a3a58ac36…

  1. I want to do my own statistics according to the build information (success, time, etc.)?

A: Drone provides webhooke plugin, you only need to write their own statistical procedures can, according to the template Settings need to send information

  1. What if I don’t have the plugins I want?

A: You can write your own plugin. There are bash/ Go examples on the website, or you can use a language you are familiar with


Recommended reading

And the interviewer blows the MongoDB replica set like this

Knowing these MongoDB design tips increases your efficiency by 50%

I spent a week reading Kafka Producer’s source code

Interviewer: How do I implement LRU with LinkedHashMap

How do I understand Java8 Stream

I’m no longer afraid of being asked JDK8 HashMap

Timing indexes in MongoDB