Hey, I used to do Drone CI

Based Drone do CI / CD, personal feeling really good, relative to the industry's big brother jekins, I prefer the drone, comparatively speaking, I think it mainly has the following advantages

  1. No additional plug-in management
  2. Yaml file-based, easy to prepare, can be configured version management
  3. May be constructed according to different conditions
  4. More user-friendly UI interface

For our back-end java project in terms of what we CI do it?

General procedure is as follows commit code

  1. Clone project to local, create a branch to complete the development of new features, git checkout -b feature / sync-status. Modify some code in this branch
  2. git add., compliant Commit writing and submit the code, git commit -m "sync article status"
  3. Push code corresponding to the code library branch, git push origin feature / sync-status
  4. If the function has been developed, can initiate a Pull Request to Develop (or Master) branch, and let the person in charge of the project Code Review
  5. After Review by the project leader will be merged into the main branch branch

From the above figure can be seen when we submit code, CI will perform the entire process, to note the following 2:00

  1. Way to perform build or unit test of time, if it fails, it will send a message to Slack, this time the developers will be able to notice the problem, of course, also be used to send mail or micro-letter
  2. When executed SonarQube check if there is a problem will result written back to github, developers will look at this issue

Look at SonarQube written back to check the results of the github what

When the sonar qube tests are complete, test results will be sent by oauth way to github, so you need to create a Personal access token in the github (this should be written down)

When you activate your code repository, Drone will automatically add Webhooks to version control systems, such as GitHub, without the need for manual configuration

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

复制代码

Above the drone of the configuration is the basic flow of the entire CI, note the following points

  1. Only when the name of the branch to perform step feature /, issue /, will develop trigger the beginning of the above, for unit test, only the develop branch takes effect (can be tailored to the needs)
  2. sonar configuration sonar.projectKey, sonar.projectName must and name when you create a project in sonar server (sonar.host.url specified address) in the same
  3. Sonar_token value is created on the sonar server, and then set the value of the secrets drone (drone in the click on a warehouse, enter Settings can be set)
  4. github token and sonar_token the same way, we need to pre-set (advantage is that you do not expose your password in the file, so that more secure) in a drone in
  5. Because the Java project with a multi-module project, so you can specify more than one module name in the sonar.modules
  6. Content sonar scan feedback to pr do not specify the preview mode
  7. build when using jacoco (analysis unit test coverage), so it is necessary to introduce this plugin in pom.xml 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>
复制代码

Other issues that may be encountered:

  1. Ci After the execution is completed, or how to send an email message to the micro-channel group

A: drone provides information about e-mail and plug-in micro-letter

  1. Can sonarqube Alibaba p3c integrated or custom checkstyle

A: No p3c plug-in, but can be integrated via PMD

Integrated P3C: www.jianshu.com/p/a3a58ac36... custom checkstyle: www.jianshu.com/p/a3a58ac36...

  1. I want to build according to their own information (success, time, etc.) Statistical how to do?

A: drone provides a plugin webhooke, you only need to write your own statistical procedures you can, according to the template settings may need to send information

  1. I do not want the plug-in, how do?

A: You can write your own plug-ins, the official website there is an example bash / go, and you are familiar with the language are possible


Past Recommended Reading

Such blowing interviewer and replication sets MongoDB

MongoDB know these design tips to improve efficiency by 50%

I spent a week reading Kafka Producer source

Interviewer: How to implement LRU with LinkedHashMap

How do I understand Java8 Stream

No longer afraid of the interviewer asked me JDK8 HashMap

Timing index in MongoDB

Guess you like

Origin juejin.im/post/5df0801c5188251257286699
Hey