如何在jenkins中为js/ts/react项目设置SonarQube扫描仪

a5g8bdjr  于 2022-11-02  发布在  Jenkins
关注(0)|答案(2)|浏览(343)

我想有声纳扫描仪运行在我的项目时,它建立在Jenkins。
就像这样

大多数教程似乎只是从Java的Angular 来解决这个过程,所以我想知道如果有的话,如何才能做到这一点。
我正在我的项目中做一些Jenkinsfile的工作:

stage('SonarQube') {
  environment {
    scannerHome = tool 'SonarQubeScanner'
  }
  steps {
    withSonarQubeEnv('SonarQubeScanner') {
      sh "${scannerHome}/bin/sonar-scanner"
    }
  }
}

我使用以下链接在SonarQube中获取该项目:https://nickkorbel.com/2020/02/05/configuring-sonar-with-a-create-react-app-in-typescript/
在Jenkins构建过程中尝试运行扫描时,我收到了几个不同的错误:
错误1

Could not find executable in "/opt/app-root/src/.sonar/native-sonar-scanner".

Proceed with download of the platform binaries for SonarScanner...
 Creating /opt/app-root/src/.sonar/native-sonar-scanner

Downloading from https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.4.0.2170-linux.zip

(executable will be saved in cache folder: /opt/app-root/src/.sonar/native-sonar-scanner)

ERROR: impossible to download and extract binary: connect ETIMEDOUT

错误2

ERROR: Failed to download https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.4.0.2170-linux.zip from agent; will retry from master

SonarQube installation defined in this job (sonarqube) does not match any configured installation. Number of installations that can be configured: 1.
ljsrvy3e

ljsrvy3e1#

错误2是关于缺少与sonarqube服务器的集成。
声纳管的完整安装:
1.安装SonarQube服务器
1.为Jenkins安装SonarQube扫描仪插件。
1.配置您的SonarQube服务器:

  • 以管理员身份登录Jenkins,然后转至管理Jenkins〉配置系统。
  • 向下滚动到SonarQube配置部分,单击Add SonarQube,然后添加提示您输入的值。
  • 服务器身份验证令牌应创建为“机密文本”凭据。

withSonarQubeEnv('SonarQubeScanner')-“SonarQubeScanner”表示步骤3中的Sonarqube服务器的名称。
在管道中,您应该为声纳扫描仪工具传递参数,例如:

stage('SonarQube analysis') {
        environment {
            scannerHome = tool 'SonarQube_4.3.0'
        }
        steps {
            withSonarQubeEnv('Your Sonar Server Name here') {
                sh '''
                ${scannerHome}/bin/sonar-scanner \
                -D sonar.projectKey=YOUR_PROJECT_KEY_HERE \
                -D sonar.projectName=YOUR_PROJECT_NAME_HERE \
                -D sonar.projectVersion=YOUR_PROJECT_VERSION_HERE \
                -D sonar.languages=js,ts \  // DEPRECATED, do not use this option
                -D sonar.sources=./src \
                -D sonar.test.inclusions=YOUR_INCLUSIONS_HERE \
                -D sonar.exclusions=YOUR_EXCLUSIONS_HERE
                '''
            }
        }
    }

假设在修复错误2之后,错误1也将得到修复。请查看官方文档here

f87krz0w

f87krz0w2#

在我的例子中,任何初始化scannerHome var的方法都不成功。所以我用我的方法:

stage("SonarQube analysis") {
    steps {
        script {
            withSonarQubeEnv('SonarQube Server') {
                sh '''
                  ${JENKINS_HOME}/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube_Scanner/bin/sonar-scanner \
                   -Dsonar.host.url=URL_TO_SONAR \
                   -Dsonar.login=SONAR_TOKEN \
                   -Dsonar.projectKey=PROJECT_KEY \
                   -Dsonar.projectName=PROJECT_NAME
                '''
            }
        }
    }
}

相关问题