一、安装SonarQube注意的关键点或坑
1、严格按照下面链接安装sonarscanner-for-jenkins
https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/
2、在Execute SonarQube Scanner下的Analysis properties 填写如下信息:
sonar.projectKey=name-must-be-letter
sonar.sources=.
sonar.exclusions=test/**,node_modules/**
sonar.nodejs.executable=/root/.nvm/versions/node/v12.22.6/bin/node
sonar.host.url=http://127.0.0.1:9000
sonar.login=your-key
这里提一下自己遇到的坑,就是构建时出现:
ERROR: CSS rules were not executed. Only Node.js v10 or later is supported, got 8.
org.sonarsource.nodejs.NodeCommandException: Only Node.js v10 or later is supported, got 8.
at org.sonarsource.nodejs.NodeCommandBuilderImpl.checkNodeCompatibility(NodeCommandBuilderImpl.java:162)
也就是它提示我的node 版本是8,它仅支持10及以上,
因此我在Execute SonarQube Scanner
前面加一段 Execute shell
并在里面写nvm use 12.22.6,发现还是报同样的错误,后来搜索相关资料发现这篇文章:https://community.sonarsource.com/t/javascript-and-or-typescript-rules-were-not-executed-only-node-js-v8-or-later-is-supported-got-v6-15-0/17231 和https://community.sonarsource.com/t/skip-this-error-error-only-node-js-v8-or-later-is-supported-got-v0-10-32/27760 这篇文章
发现原来sonar支持单独设置node脚本,也就是上面的sonar.nodejs.executable=/root/.nvm/versions/node/v12.22.6/bin/node
设置成功后完美解决。
3、多个项目都配置好并成功后,后来发现其中一个项目死活不成功,一直报
WARN: Not indexing due to symlink loop: /data/xxx/node_modules/_babel-register@6.22.0@babel-register/node_modules/babel-core/node_modules/babel-register/node_modules/babel-core
INFO: 270 files indexed... (last one was test/unit/specs/pagination.spec.js)
INFO: 270 files indexed... (last one was test/unit/specs/pagination.spec.js)
INFO: 270 files indexed... (last one was test/unit/specs/pagination.spec.js)
INFO: 270 files indexed... (last one was test/unit/specs/pagination.spec.js)
刚开始以为是pagination.spec.js
文件循环引用问题,便使用 sonar.exclusions=test/**,node_modules/**
排除,发现还不行,于是google、stackoverflow、sonarqube的issues 均没有找到答案,最后无奈只好在sonarqube源码找Not indexing due to symlink loop
语句
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
if (exc instanceof FileSystemLoopException) {
LOG.warn("Not indexing due to symlink loop: {}", file.toFile());
return FileVisitResult.CONTINUE;
}
throw exc;
}
发现确实是访问文件时失败了,原因是因为文件系统出现了循环异常,然后我往回看报错日志,发现node_modules下的包名均加了_下划线,这有点不太正常,我再想是不是我前端项目部署前及部署后没有清空node_modules等文件呢?一检查,发现果然。勾选
Delete workspace before build starts
及选择Delete workspace when build is done
完美解决!巨坑!二、eslint-plugin-sonarjs 规则忽略或修改
比如提示
11:41 error Refactor this function to reduce its Cognitive Complexity from 21 to the 20 allowed sonarjs/cognitive-complexity
在.eslintrc.js
中添加
module.exports = {
plugins: ["sonarjs"],
rules: {
// 添加如下一行
"sonarjs/cognitive-complexity": ["error",22]
}
}
具体可以参考 https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/cognitive-complexity.md 等一系列进行修改
三、 SonarQube规则忽略或修改
由于SonarQube默认的内置规则无法直接修改,我们需要复制它的规则后再进行修改,修改完后设置规则为默认即可
四、 SonarLint规则忽略或修改
可以连接配置好的SonarQube服务即可:
在SonarQube中生成令牌
在SonarLint中配置
五、 总结
1、如果发现google后没有任何线索,建议直接看引起报错的源码,也许可以从中找到线索。ps:之前也遇到过类似的问题,就是遇到报错,直接在源码里搜索日志,发现线索并解决了;
2、遇到问题与相关人员一起交流探讨,也许会立马找到思路;
3、多尝试和实践,也许你会发现一片新天地;