Translate sonarqube.md

Signed-off-by: kaige <154599029@qq.com>
This commit is contained in:
kaige 2020-11-26 16:17:10 +08:00
parent 1261c23033
commit 5beaf35d84
21 changed files with 83 additions and 84 deletions

View File

@ -6,29 +6,28 @@ linkTitle: "将 SonarQube 集成到流水线"
weight: 200
---
[SonarQube](https://www.sonarqube.org/) 是一种流行的代码质量持续监测工具。 您可以将其用于代码库的静态和动态分析。将其集成到 KubeSphere 的流水线中后, 当 SonarQube 在运行的流水线检测问题时,您可以直接在仪表盘上查看常见的代码问题,比如 bug 和漏洞。
[SonarQube](https://www.sonarqube.org/) 是一种流行的代码质量持续监测工具。 您可以将其用于代码库的静态和动态分析。将其集成到 KubeSphere 的流水线中后, 当 SonarQube 在运行的流水线检测到问题时,您可以直接在仪表盘上查看常见的代码问题,比如 bug 和漏洞。</br>
本教程演示了如何将 SonarQube 集成到流水线中。 在[使用 Jenkinsfile 创建流水线](../../../devops-user-guide/how-to-use/create-a-pipeline-using-jenkinsfile/)之前,请先参考以下步骤。
This tutorial demonstrates how you can integrate SonarQube into pipelines. Refer to the following steps first before you [create a pipeline using a Jenkinsfile](../../../devops-user-guide/how-to-use/create-a-pipeline-using-jenkinsfile/).
## 先决条件
## Prerequisites
您需要[启用 KubeSphere DevOps 系统](../../../../docs/pluggable-components/devops/)。
You need to [enable KubeSphere DevOps System](../../../../docs/pluggable-components/devops/).
## 安装 SonarQube 服务
## Install SonarQube Server
1. Execute the following command to install SonarQube Server if it is not ready:
1. 如果尚未安装,请执行以下命令来安装 SonarQube 服务:
```bash
helm upgrade --install sonarqube sonarqube --repo https://charts.kubesphere.io/main -n kubesphere-devops-system --create-namespace --set service.type=NodePort
```
2. You will get this prompt:
2. 您将得到以下提示:
![sonarqube-install](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-install.png)
![sonarqube-install](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-install.png)
## Get Address of SonarQube Console
## 获取 SonarQube 控制台地址
1. Execute the following command to get SonarQube NodePort.
1. 执行以下命令以获取 SonarQube 控制台地址。
```bash
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services sonarqube-sonarqube)
@ -36,17 +35,17 @@ export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonp
echo http://$NODE_IP:$NODE_PORT
```
2. You can get the output as below (`31434` is the port number in this example, which may be different from yours):
2. 您可以得到如下输出 (`31377` 是此示例中的端口号,可能与您的端口号不同):
```bash
http://192.168.0.4:31434
http://10.77.1.201:31377
```
## Configure SonarQube Server
## 配置 SonarQube 服务器
### Step 1: Access SonarQube Console
### 步骤 1: 访问 SonarQube 控制台
1. Execute the following command to view the status of SonarQube. Note that the SonarQube console is not accessible until SonarQube is up and running.
1. 执行以下命令以查看 SonarQube 的状态。 请注意,只有在 SonarQube 启动并运行后才能访问 SonarQube 控制台。
```bash
$ kubectl get pod -n kubesphere-devops-system
@ -58,43 +57,43 @@ sonarqube-sonarqube-bb595d88b-97594 1/1 Running 2 5m31s
uc-jenkins-update-center-8c898f44f-m8dz2 1/1 Running 0 85m
```
2. Access the SonarQube console `http://{$Node IP}:{$NodePort}` in your browser and you can see its homepage as below:
2. 在浏览器中访问 SonarQube 控制台 `http://10.77.1.201:31377`,您可以看到其主页,如下所示:
![access-sonarqube-console](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/access-sonarqube-console.jpg)
![access-sonarqube-console](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/access-sonarqube-console.png)
3. Click **Log in** in the top right corner and use the default account `admin/admin`.
3. 单击右上角的 **Log in**,然后使用默认帐户登陆 `admin/admin`
![log-in-page](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/log-in-page.jpg)
![log-in-page](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/log-in-page.png)
{{< notice note >}}
You may need to set up necessary port forwarding rules and open the port to access SonarQube in your security groups depending on where your instances are deployed.
您可能需要设置必要的端口转发规则并打开端口以访问安全组中的 SonarQube ,具体取决于实例的部署位置。
{{</ notice >}}
### Step 2: Create SonarQube Admin Token
### 步骤 2: 创建 SonarQube 管理员 Token
1. Click the letter **A** and select **My Account** from the menu to go to the **Profile** page.
1. 单击右上角字母 **A**,然后从菜单中选择 **My Account** 以转到 **Profile** 页面。
![sonarqube-config-1](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-config-1.jpg)
![sonarqube-config-1](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-config-1.png)
2. Click **Security** and input a token name, such as `kubesphere`.
2. 单击 **Security** 并输入Token 名称,如 kubesphere。
![sonarqube-config-2](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-config-2.jpg)
![sonarqube-config-2](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-config-2.png)
3. Click **Generate** and copy the token.
3. 单击 **Generate** 并复制 token。
![sonarqube-config-3](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-config-3.jpg)
![sonarqube-config-3](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-config-3.png)
{{< notice warning >}}
{{< notice warning >}}
Make sure you do copy the token because you won't be able to see it again as shown in the prompt.
如提示所示,请确保您确实复制了 token因为您将无法再次看到此 token。
{{</ notice >}}
### Step 3: Create a Webhook Server
### 步骤 3: 创建一个 SonarQube Webhook 服务
1. Execute the following command to get the address of SonarQube Webhook.
1. 执行以下命令以获取 SonarQube Webhook 的地址。
```bash
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services ks-jenkins)
@ -102,33 +101,33 @@ export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonp
echo http://$NODE_IP:$NODE_PORT/sonarqube-webhook/
```
2. Expected output:
2. 预期的输出结果:
```bash
http://192.168.0.4:30180/sonarqube-webhook/
http://10.77.1.201:30180/sonarqube-webhook/
```
3. Click **Administration**, **Configuration** and **Webhooks** in turn to create a webhook.
3. 依次单击 **Administration**, **Configuration****Webhooks** 创建一个 webhook。
![sonarqube-webhook-1](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-webhook-1.jpg)
![sonarqube-webhook-1](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-webhook-1.png)
4. Click **Create**.
4. 点击 **Create**
![sonarqube-webhook-3](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-webhook-3.jpg)
![sonarqube-webhook-3](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-webhook-2.png)
5. Input **Name** and **Jenkins Console URL** (i.e. the SonarQube Webhook address) in the dialogue that appears. Click **Create** to finish.
5. 在出现的对话框中输入**Name** 和 **Jenkins Console URL**即SonarQube Webhook地址。 单击 **Create** 完成。
![webhook-page-info](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/webhook-page-info.jpg)
![webhook-page-info](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/webhook-page-info.png)
### Step 4: Add SonarQube Configuration to ks-installer
### 步骤 4: 将 SonarQube 配置添加到 ks-installer
1. Execute the following command to edit `ks-installer`.
1. 执行以下命令编辑 `ks-installer`
```bash
kubectl edit cc -n kubesphere-system ks-installer
```
2. Navigate to `devops`. Add the field `sonarqube` and specify `externalSonarUrl` and `externalSonarToken` under it.
2. 导航至 `devops`。 添加字段 `sonarqube` 并在其下指定 `externalSonarUrl``externalSonarToken`
```yaml
devops:
@ -140,15 +139,15 @@ devops:
jenkinsMemoryReq: 1500Mi
jenkinsVolumeSize: 8Gi
sonarqube: # Add this field manually.
externalSonarUrl: http://192.168.0.4:31434 # The SonarQube IP address.
externalSonarToken: f75dc3be11fd3d58debfd4e445e3de844683ad93 # The SonarQube admin token created above.
externalSonarUrl: http://10.77.1.201:31377 # The SonarQube IP address.
externalSonarToken: 00ee4c512fc987d3ec3251fdd7493193cdd3b91d # The SonarQube admin token created above.
```
3. Save the file after you finish.
3. 完成后保存文件。
### Step 5: Add SonarQube Server to Jenkins
### 步骤 5: 将 SonarQube Server 添加到 Jenkins
1. Execute the following command to get the address of Jenkins.
1. 执行以下命令获取 Jenkins 的地址。
```bash
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services ks-jenkins)
@ -156,49 +155,49 @@ export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonp
echo http://$NODE_IP:$NODE_PORT
```
2. You can get the output as below, which tells you the port number of Jenkins.
2. 您可以得到以下输出,获取您 Jenkins 的端口号。
```bash
http://192.168.0.4:30180
http://10.77.1.201:30180
```
3. Access Jenkins with the address `http://Public IP:30180`. When KubeSphere is installed, the Jenkins dashboard is also installed by default. Besides, Jenkins is configured with KubeSphere LDAP, which means you can log in Jenkins with KubeSphere accounts (e.g. `admin/P@88w0rd`) directly. For more information about configuring Jenkins, see [Jenkins System Settings](../../../devops-user-guide/how-to-use/jenkins-setting/).
3. 使用地址 `http://10.77.1.201:30180` 访问 Jenkins。安装 KubeSphere 时,默认情况下也会安装 Jenkins 仪表板。 Jenkins 配置了KubeSphere LDAP这意味着您可以直接使用 KubeSphere 帐户(例如 admin/P@88w0rd登录 Jenkins。 有关配置 Jenkins 的更多信息,请参阅 [Jenkins 系统设置](../../../devops-user-guide/how-to-use/jenkins-setting/)。
![jenkins-login-page](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/jenkins-login-page.jpg)
![jenkins-login-page](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/jenkins-login-page.png)
{{< notice note >}}
You may need to set up necessary port forwarding rules and open the port `30180` to access Jenkins in your security groups depending on where your instances are deployed.
您可能需要设置必要的端口转发规则并打开端口 `30180` 才能访问安全组中的 Jenkins具体取决于您的实例部署的位置。
{{</ notice >}}
4. Click **Manage Jenkins** on the left.
4. 单击左侧的 **Manage Jenkins**
![manage-jenkins](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/manage-jenkins.jpg)
![manage-jenkins](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/manage-jenkins.png)
5. Scroll down to **Configure System** and click it.
5. 向下翻页找到并单击 **Configure System**
![configure-system](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/configure-system.jpg)
![configure-system](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/configure-system.png)
6. Navigate to **SonarQube servers** and click **Add SonarQube**.
6. 导航到 **SonarQube servers**,然后单击 **Add SonarQube**
![add-sonarqube](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/add-sonarqube.jpg)
![add-sonarqube](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/add-sonarqube.png)
7. Input **Name**, **Server URL** (`http://Node IP:port`) and **Server authentication token** (the SonarQube admin token). Click **Apply** to finish.
7. 输入 **Name****Server URL** (`http://10.77.1.201:31377`)和 **Server authentication token**SonarQube 管理管理员 token。 单击**Apply**完成。
![sonarqube-jenkins-settings](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-jenkins-settings.jpg)
![sonarqube-jenkins-settings](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-jenkins-settings.png)
### Step 6: Add sonarqubeUrl to KubeSphere Console
### 步骤 6: 将 sonarqubeUrl 添加到 KubeSphere 控制台
You need to specify `sonarqubeURL` so that you can access SonarQube directly from the KubeSphere console.
您需要指定 `sonarqubeURL`,以便可以直接从 KubeSphere 控制台访问 SonarQube。
1. Execute the following command:
1. 执行以下命令:
```bash
kubectl edit cm -n kubesphere-system ks-console-config
```
2. Navigate to `client` and add the field `devops` with `sonarqubeURL` specified.
2. 导航到 `client`,添加 `devops`字段,填写 `sonarqubeURL` 的值。
```yaml
client:
@ -208,14 +207,14 @@ client:
openpitrix: v0.3.5
enableKubeConfig: true
devops: # Add this field manually.
sonarqubeURL: http://192.168.0.4:31434 # The SonarQube IP address.
sonarqubeURL: http://10.77.1.201:31377 # The SonarQube IP address.
```
3. Save the file.
3. 保存文件。
### Step 7: Restart Services to Make All Effective
### 步骤 7: 重新启动服务使所有功能生效
Execute the following commands.
执行以下命令:
```bash
kubectl -n kubesphere-system rollout restart deploy ks-apiserver
@ -225,32 +224,32 @@ kubectl -n kubesphere-system rollout restart deploy ks-apiserver
kubectl -n kubesphere-system rollout restart deploy ks-console
```
## Create SonarQube Token for New Project
## 为新项目创建 SonarQube Token
You need a SonarQube token so that your pipeline can communicate with SonarQube as it runs.
您需要一个 SonarQube Token以便您的流水线可以在运行时与 SonarQube 通信。
1. On the SonarQube console, click **Create new project**.
1. 在 SonarQube 控制台上,单击 **Create new project**
![sonarqube-create-project](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-create-project.jpg)
![sonarqube-create-project](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-create-project.png)
2. Enter a project key, such as `java-demo`, and click **Set Up**.
2. 输入项目密钥,例如 `java-demo`,然后单击 **Set Up**
![jenkins-projet-key](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/jenkins-projet-key.jpg)
![jenkins-projet-key](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/jenkins-projet-key.png)
3. Enter a project name, such as `java-sample`, and click **Generate**.
3. 输入项目名称,例如 `java-sample`,然后单击 **Generate**
![generate-a-token](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/generate-a-token.jpg)
![generate-a-token](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/generate-a-token.png)
4. After the token is created, click **Continue**.
4. 创建令牌后,单击 **Continue**
![token-created](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/token-created.jpg)
![token-created](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/token-created.png)
5. Choose **Java** and **Maven** respectively. Copy the serial number within the green box in the image below, which needs to be added in the [Credentials](../../../devops-user-guide/how-to-use/credential-management/#create-credentials) section if it is to be used in pipelines.
1. 分别选择 **Java****Maven** 。 复制下图中的绿色框中的序列号,如果要在流水线中使用,则需要在[凭据](../../../devops-user-guide/how-to-use/credential-management/#create-credentials)中添加此序列号。
![sonarqube-example](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-example.jpg)
![sonarqube-example](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-example.png)
## View Results on KubeSphere Console
## 在 KubeSphere 控制台查看结果
After you [create a pipeline using the graphical editing panel](../../how-to-use/create-a-pipeline-using-graphical-editing-panel) or [create a pipeline using a Jenkinsfile](../../how-to-use/create-a-pipeline-using-jenkinsfile), you can view the result of code quality analysis. For example, you may see an image as below if SonarQube runs successfully.
使用[图形编辑面板创建流水线](../../how-to-use/create-a-pipeline-using-graphical-editing-panel)或[使用 Jenkinsfile 创建流水线](../../how-to-use/create-a-pipeline-using-jenkinsfile)之后,您可以查看代码质量分析的结果。 如果 SonarQube 成功运行,您可能会看到以下图片所示结果。
![sonarqube-view-result](/images/docs/devops-user-guide/integrate-sonarqube-into-pipeline/sonarqube-view-result.jpg)
![sonarqube-view-result](/images/docs/devops-user-guide-zh/integrate-sonarqube-into-pipeline-zh/sonarqube-view-result.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB