1 Prerequisites

1. Need to install gitlab, jenkins, sonarqube;

2. Gitlab needs to be able to access the jenkins address, and the network is connected, because it needs to push events to the jenkins machine through gitlab;

3. The gitlab project requires the authority of the main programmer and above.

2 Overall thinking

1. When there is code push to the code warehouse, gitlab knows it. When gitlab detects that there is code push, it executes a hook (called hook on gitlab), which can be understood as triggering a push time and push to jenkins;

2. After Jenkins detects this event, it will build automatically (no need to manually);

3. Jenkins can configure post-build actions and automatically execute sonarqube detection after configuration. At this point, the whole process of automatic construction + automatic detection has been completed.

3 Step 1: Configure the key pair

Use the jenkins account mailbox to generate a key pair.

ssh-keygen -t rsa -C "邮箱地址" -b 4096

1. The private key is configured in the project configuration of jenkins. The following will say where the configuration is.

2. The public key is configured in the personal settings of gitlab, menu: "SSH key", as shown in the figure below.

gitlab配置SSH密钥

4 Configure jenkins and sonarqube

1. Create a project in jenkins;

2. Open "Source Code Management", configure the ssh address of the gitlab project, and configure the branch name, as shown in Figure 2 below.

jenkins源码配置

3. Add an account, select "SSH username with private key" as the type, and add the jenkins private key generated above below.

配置jenkins私钥

3. Set the trigger, select "Build when a change is pushed to GitLab. GitLab webhook", remember the project address (remember 1)

记住地址

4. Select the following advanced, click Generate, generate secret token, remember this token (remember 2).

记住token

5. Configure post-build actions-sonarqube scan

one example

sonar.projectKey=project-demo
sonar.projectName=project-demo
sonar.projectVersion=1.0 
sonar.language=java 
sonar.java.binaries=target/classes
sonar.sources=src/main/java

配置sonarqube

5 Configure gitlab webhook

As shown in the figure below, in the project, select Settings——"Integration——"Add hook.

Fill in url and secret token (remember the two in jenkins configuration), uncheck "SSL certificate verification"

取消选中SSL验证

To test, click test——"push event

测试推送 .

Note: If the test is executed and an error occurs: Hook executin fail: execution expired, then it may be that gitlab and jenkins are not connected to the network.

If the test passes, it will return: Hook executed successfully: HTTP 200.

The configuration is successful.

This will be automatically built by jenkins. After the build is completed, the sonarqube detection result will be generated. You can directly access the sonarqube interface through the jenkins interface.

点击即可跳转sonarqube界面

PS: You can find the detailed process of jenkins configuration sonarqube by yourself.

Reference article:

1、Create a Continuous Integration Pipeline with GitLab and Jenkins:https://docs.bitnami.com/tutorials/create-ci-pipeline/

2. Jenkins+gitlab realizes the automatic deployment of springboot: https://blog.csdn.net/songguopeng/article/details/97634829


程序员伍六七
201 声望597 粉丝