篇三:创建流水线-1.SonarQube 集成到流水线
创建流水线,编辑Jenkinsfile,包含Checkout SCM、单元测试、SonarQube分析、构建并推送镜像、部署服务至Kubernetes集群等几个步骤。
1.1将 SonarQube 集成到流水线
官网: https://kubesphere.io/zh/docs/devops-user-guide/how-to-integrate/sonarqube/
SonarQube 是一种主流的代码质量持续检测工具。您可以将其用于代码库的静态和动态分析。SonarQube 集成到 KubeSphere 流水线后,如果在运行的流水线中检测到问题,您可以直接在仪表板上查看常见代码问题,例如 Bug 和漏洞。
前提:启用 KubeSphere DevOps 系统
1.1.1安装 SonarQube 服务器
要将 SonarQube 集成到您的流水线,必须先安装 SonarQube 服务器。
-
先安装 Helm,后续使用该工具安装 SonarQube。在镜像中以有helm的安装包,解压即用:
#默认是安装上的,具体看环境 tar -zxvf /opt/helm-v3.7.1-linux-amd64.tar.gz -C /opt cp /opt/linux-amd64/helm /usr/bin/
查看 Helm 版本。
$ helm version version.BuildInfo{Version:"v3.4.1", GitCommit:"c4e74854886b2efe3321e185578e6db9be0a6e29", GitTreeState:"clean", GoVersion:"go1.14.11"}
执行以下命令安装 SonarQube 服务器。
helm upgrade --install sonarqube sonarqube --repo https://charts.kubesphere.io/main -n kubesphere-devops-system --create-namespace --set service.type=NodePort
会获取以下提示内容:
1.1.2获取 SonarQube 控制台地址
1.执行以下命令以获取 SonarQube NodePort。
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services sonarqube-sonarqube)
export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
#得到如下地址
http://10.18.4.10:31179
1.1.3配置 SonarQube 服务器
步骤 1:访问 SonarQube 控制台
-
执行以下命令查看 SonarQube 的状态。请注意,只有在 SonarQube 启动并运行后才能访问 SonarQube 控制台。
$ kubectl get pod -n kubesphere-devops-system NAME READY STATUS RESTARTS AGE sonarqube-postgresql-0 1/1 Running 0 5m31s sonarqube-sonarqube-bb595d88b-97594 1/1 Running 2 5m31s
-
在浏览器中访问 SonarQube 控制台
http://10.18.4.10:31179
。 -
点击右上角的 Log in,然后使用默认帐户
admin/admin
登录,然后改个密码。
步骤 2:创建 SonarQube 管理员令牌 (Token)
1.点击右上角字母 A,然后从菜单中选择 My Account 以转到 Profile 页面。
2.点击 Security 并输入令牌名称,例如 kubesphere
。
3.点击 Generate 并复制此令牌。
警告:如提示所示,无法再次查看此令牌,因此请确保复制成功。
Generate Tokens:1f87a540d94733dda04516b80a36dedc1e724615
步骤3:创建Webhook服务器
1.获取jenkins中的SonarQube Webhook的地址
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services devops-jenkins)
export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT/sonarqube-webhook/
#获取到如下地址
http://10.18.4.10:30180/sonarqube-webhook/
2.依次点击 Administration、Configuration 和 Webhooks 创建一个 Webhook。
步骤 4:将 SonarQube 配置添加到 ks-installer
-
执行以下命令编辑
ks-installer
。kubectl edit cc -n kubesphere-system ks-installer
-
搜寻至
devops
。添加字段sonarqube
并在其下方指定externalSonarUrl
和externalSonarToken
。devops: enabled: true jenkinsJavaOpts_MaxRAM: 2g jenkinsJavaOpts_Xms: 512m jenkinsJavaOpts_Xmx: 512m jenkinsMemoryLim: 2Gi jenkinsMemoryReq: 1500Mi jenkinsVolumeSize: 8Gi sonarqube: #Add this field manually. externalSonarUrl: http://10.18.4.10:31179 #SonarQube IP 地址。 externalSonarToken: 1f87a540d94733dda04516b80a36dedc1e724615 #上面创建的SonarQube 管理员令牌。
- 完成操作后保存此文件。
步骤 5:将 SonarQube 服务器添加至 Jenkins
-
执行以下命令获取 Jenkins 的地址。
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services devops-jenkins) export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}") echo http://$NODE_IP:$NODE_PORT #如下为Jenkins的IP:端口 http://10.18.4.10:30180
-
使用地址
http://10.18.4.10:30180
访问 Jenkins。安装 KubeSphere 时,默认情况下也会安装 Jenkins 仪表板。此外,Jenkins 还配置有 KubeSphere LDAP,可以直接使用 KubeSphere 帐户(例如admin/设置的密码
)登录 Jenkins。 -
点击左侧导航栏中的系统管理。
-
向下翻页找到并点击系统配置。
-
搜寻到 SonarQube servers,然后点击 Add SonarQube。
-
输入 Name 和 Server URL (
http://10.18.4.10:31179/
)。点击添加,选择 Jenkins,然后在弹出的对话框中用 SonarQube 管理员令牌创建凭证(如下方第二张截图所示)。创建凭证后,从 Server authentication token 旁边的下拉列表中选择该凭证。点击应用完成操作。
备注
如果点击添加按钮无效(Jenkins 已知问题),可以在系统管理下的 Manage Credentials 并点击 Stores scoped to Jenkins 下的 Jenkins,再点击全局凭据 (unrestricted),然后点击左侧导航栏的添加凭据,参考上方第二张截图用 SonarQube 管理员令牌添加凭证。添加凭证后,从 Server authentication token 旁边的下拉列表中选择该凭证。
步骤 6:将 sonarqubeURL 添加到 KubeSphere 控制台
您需要指定 sonarqubeURL
,以便可以直接从 KubeSphere 控制台访问 SonarQube。
-
执行以下命令:
kubectl edit cm -n kubesphere-system ks-console-config
-
搜寻到
data.client.enableKubeConfig
,在下方添加devops
字段并指定sonarqubeURL
。client: enableKubeConfig: true devops: # 手动添加该字段。 sonarqubeURL: http://10.18.4.10:31179 # SonarQube IP 地址。
-
保存该文件。
步骤 7:重启服务
执行以下命令。
kubectl -n kubesphere-devops-system rollout restart deploy devops-apiserver
kubectl -n kubesphere-system rollout restart deploy ks-console
1.1.4为新项目创建 SonarQube Token
需要一个 SonarQube 令牌,以便您的流水线可以在运行时与 SonarQube 通信。
-
在 SonarQube 控制台上,点击 Add a project。
-
输入项目密钥,例如
springcloud
,然后点击 Set Up。再输入项目名称,例如springcloud
,然后点击 Generate,创建令牌(SONAR_TOKEN)后,点击 Continue。
-
选择Maven。复制下图所示绿色框中的序列号,如果要在流水线中使用,则需要在凭证中添加此序列号。
1.1.5为SonarQube添加凭证