如何在 Google Cloud 构建步骤中包含 sonarqube 扫描步骤

问题描述 投票:0回答:3

我有一个在 Azure 之上运行的 sonarqube 服务器,以及一个使用 GCP 之上的 Google 云构建配置的 CICD 管道。您知道如何将 sonarqube 连接信息作为自定义构建步骤包含在我的 cloudbuild 文件中吗?我正在使用 gradle 来构建我的版本并测试我的图像。

sonarqube sonarqube-scan google-cloud-build
3个回答

1
投票

下面的示例代码对我有用

#static code analysis by sonarqube
- name: 'maven:3.6.1-jdk-8'
  entrypoint: 'bash'
  args:
    - -c
    - |
      unset MAVEN_CONFIG \
      && echo "104.199.71.165 sonarqube.ct.blue.cdtapps.com" > /etc/hosts \
      && mvn sonar:sonar -q -Dsonar.login=5531b1a2d571c0482a3d45f605830e08ccf5f245 \
      '-Dsonar.projectKey=odp.df.pubsub-sftp' \
      '-Dsonar.projectName=ODP-DF-PUBSUB-SFTP' \
      '-Dsonar.host.url=https://sonarqube.ct.blue.cdtapps.com' \
      '-Dsonar.qualitygate.wait=true' \
      'allow_failure: true'
  dir: 'dataflows/generic/pubsub-sftp/src'
  id: 'sonarqube-analysis'

0
投票

您已经提供了完美的答案,但我想我会在这里提及所有步骤。

这里提供了所有必要的资源

第一步是克隆

repo 并运行以下 gcloud 命令在工件注册表上创建声纳扫描仪图像:

cd sonarqube gcloud builds submit . --config=cloudbuild.yaml
现在将以下步骤添加到您的 cloudbuild.yaml 步骤中

steps: - name: 'gcr.io/$PROJECT_ID/sonar-scanner:latest' args: - '-Dsonar.host.url=https://sonarcloud.io' - '-Dsonar.login=$_SONAR_TOKEN' - '-Dsonar.projectKey=career-crew_career-board' - '-Dsonar.organization=career-crew' - '-Dsonar.sources=.'
现在,在将更改推送到 GitHub 之前,将 

_SONAR_TOKEN

 添加到云构建触发器作为环境变量,并使用从 
sonarqube.io 生成的令牌值(右上角的配置文件 -> 我的帐户 -> 安全性 -> 生成令牌

不要

将 SONAR_TOKEN 直接添加到代码库中,因为它是发布到 sonarqube 项目的秘密令牌。

从 sonarqube 中删除手动分析的项目,因此使用 cloudbuild 自动化可以做到这一点。否则它将构建失败。

© www.soinside.com 2019 - 2024. All rights reserved.