如何在Jenkins中为js/ts/react项目设置SonarQube扫描仪 [英] How to setup SonarQube scanner in jenkins for a js/ts/react project

查看:114
本文介绍了如何在Jenkins中为js/ts/react项目设置SonarQube扫描仪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在jenkins中构建项目时运行Sonar扫描仪.

I would like to have Sonar scanner running on my project when it builds in jenkins.

像这样的事情,

大多数教程似乎仅从Java角度解决了这个过程,所以我想知道如果可以的话如何做到这一点.

Most of the tutorials seem to only address this process from a Java perspective, So I am wondering how this can be done if at all.

我正在用我的项目中的Jenkinsfile做一些工作:

I am doing some of the work out of a Jenkinsfile in my project:

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/

I used the following link to get the project in SonarQube: https://nickkorbel.com/2020/02/05/configuring-sonar-with-a-create-react-app-in-typescript/

在Jenkins Build期间尝试运行扫描时,我遇到了几个不同的错误:

I get a couple different errors when the scan tries to run during the Jenkins Build:

错误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.

推荐答案

错误2与缺少与声纳服务器的集成有关.

Error 2 is about missing integration with sonarqube server.

完整安装了sonarqube:

Full install of sonarqube:

  1. 安装SonarQube服务器
  2. 为Jenkins安装SonarQube Scanner插件.
  3. 配置SonarQube服务器:

  • 以管理员身份登录Jenkins,然后转到Manage Jenkins>.配置系统.
  • 向下滚动到SonarQube配置部分,单击添加SonarQube",然后添加提示您输入的值.
  • 服务器身份验证令牌应作为机密文本"凭据创建.
  • withSonarQubeEnv('SonarQubeScanner')-"SonarQubeScanner"表示步骤3中Sonarqube服务器的名称.

    withSonarQubeEnv('SonarQubeScanner') - "SonarQubeScanner" means the name of the Sonarqube server from step 3.

    在管道中,您应该传递声纳扫描仪工具的参数,例如:

    In the pipeline you should pass parameters for sonar-scanner tool, for example:

    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.在此处

    Suppose Error 1 will be fixed after you fix Error 2. Take a look at official documentation here

    这篇关于如何在Jenkins中为js/ts/react项目设置SonarQube扫描仪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆