当我的项目在 jenkins 中构建时,我希望在我的项目上运行 Sonar 扫描器。
大多数教程似乎只从 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.
最佳答案
错误 2 是关于缺少与 sonarqube 服务器的集成。
sonarqube 的完整安装:
- 安装 SonarQube 服务器
- 为 Jenkins 安装 SonarQube Scanner 插件。
- 配置您的 SonarQube 服务器:
- 以管理员身份登录 Jenkins,然后转到管理 Jenkins > 配置系统。
- 向下滚动到 SonarQube 配置部分,点击添加 SonarQube,然后添加系统提示的值。
- 服务器身份验证 token 应创建为“ secret 文本”凭据。
withSonarQubeEnv('SonarQubeScanner')
- “SonarQubeScanner”表示第 3 步中的 Sonarqube 服务器的名称。
在管道中,您应该为 Sonar 扫描仪工具传递参数,例如:
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
'''
}
}
}
假设错误 1 将在您修复错误 2 后得到修复。 看看官方文档here
关于javascript - 如何在 jenkins 中为 js/ts/react 项目设置 SonarQube 扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62876686/