sonar扫描的$BRANCH在扫描工具是其他类型的时候不生效

sonar-scanner
-Dsonar.projectKey=payment
-Dsonar.sources=.
-Dsonar.host.url=https://sonar.xxxx.com
-Dsonar.branch.name=$BRANCH
-Dsonar.login=sqp_d51d2a98d57fe
-Dsonar.inclusions=‘/src/
-Dsonar.java.binaries=‘/src/

嗯,目前只是扫描工具是 SonarQube 的时候支持 $BRANCH 参数

希望在”其他“类型的扫描工具里面,增加更多的环境变量, 这样扩展性就非常大了

因为我用同样的参数扫描, 自带的sonarqube扫描报错

2022-08-30T10:56:44.632Z        ====================== Scanning Start ======================
2022-08-30T10:56:44.632Z        Installing Dependency Packages.
2022-08-30T10:56:44.632Z        Install ended. Duration: 0.00 seconds.
2022-08-30T10:56:44.632Z        Cloning Repository.
2022-08-30T10:56:44.635Z        git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /workspace/all-in-one/.git/
2022-08-30T10:56:44.641Z        git fetch origin +refs/heads/master --depth=1
From https://git.ekuaibao.com/whispered/all-in-one
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
2022-08-30T10:56:45.305Z        git checkout -qf FETCH_HEAD
2022-08-30T10:56:45.328Z        git submodule update --init --recursive
Submodule 'backend' (https://oauth2:********@git.xxx.com/whispered/backend.git) registered for path 'backend'
Submodule 'buildscript' (https://oauth2:********@git.xxx.com/exile/buildscript.git) registered for path 'buildscript'
Submodule 'charging' (https://oauth2:********@git.xxx.com/whispered/charging.git) registered for path 'charging'
Submodule 'collector' (https://oauth2:********@git.xxx.com/whispered/collector.git) registered for path 'collector'

Cloning into '/workspace/all-in-one/router'...
Cloning into '/workspace/all-in-one/sensorsdata'...
Cloning into '/workspace/all-in-one/supplier'...
Cloning into '/workspace/all-in-one/tpp'...
Cloning into '/workspace/all-in-one/vphoto'...
Submodule path 'backend': checked out '1c592d64ea88b922af35612af994861d2957e01e'
Submodule path 'buildscript': checked out 'aff877c9126c0d97f1e80f281f5e9c5870e659de'
Submodule path 'charging': checked out '3c247ece1cff063e6c808e0e6fd169d9c6743b30'
Submodule path 'collector': checked out 'df487358d13b9db45ac2862f1a1534fcb381adb4'

2022-08-30T10:57:52.664Z        git --no-pager log --oneline -1
ad0f60f Finish Hotfix-20220829
2022-08-30T10:57:52.665Z        Clone ended. Duration: 67.62 seconds.
2022-08-30T10:57:52.670Z        Executing SonarQube Scanning process.
2022-08-30T10:57:52.670Z        Writing sonar-project.properties for repo: all-in-one
INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /workspace/all-in-one/sonar-project.properties
INFO: SonarScanner 4.7.0.2747
INFO: Java 11.0.15 Alpine (64-bit)
INFO: Linux 5.4.119-1-tlinux4-0009-public-eks amd64
INFO: User cache: /opt/sonar-scanner/.sonar/cache
INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /workspace/all-in-one/sonar-project.properties
INFO: Analyzing on SonarQube server 9.6.1.59531
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=123ms
INFO: Server id: 39E0EBC1-AX9Ty5kLJWdqjxcudrDi
INFO: User cache: /opt/sonar-scanner/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=47ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: Load/download plugins (done) | time=7388ms
INFO: Process project properties
INFO: Process project properties (done) | time=0ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=2ms
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 10.165s
INFO: Final Memory: 9M/160M
INFO: ------------------------------------------------------------------------
2022-08-30T10:58:03.313Z        Failed to run: failed to execute sonar scanning process, the error is: exit status 2.
2022-08-30T10:58:03.313Z        Job Status: fail
2022-08-30T10:58:03.313Z        ====================== Scanning End. Duration: 78.27 seconds ======================```


但是用“其他”同样的参数是没问题的

#!/bin/bash

SonarQube

set -e

sonar-scanner
-Dsonar.projectKey=all-in-one-all
-Dsonar.sources=.
-Dsonar.host.url=https://sonar.xxxxxx.com
-Dsonar.login=sqp_8cab4230xxxxxxxxc3953
-Dsonar.inclusions=‘/src/
-Dsonar.java.binaries=‘/src/

具体是哪些变量呢?用于什么场景?方便分享下吗?

目前就是分支扫描, pr扫描
https://docs.sonarqube.org/latest/analysis/pull-request/
例如这些

@Fansi404 这个问题还在看吗, 我为了使用sonar.branch.name=$BRANCH这个参数, 必须使用系统自带的扫描方式, 但是扫描java一直失败, 没有具体报错。而用“其他”通过sonar-scanner扫描,相同的参数是没问题的。 但是sonar-scanner不支持$BRANCH这个变量, 所现在卡住了,希望得到回复, 谢谢

我也遇到和你同样的问题,后来通过修改sonar.java.binaries配置解决了。
sonar.java.binaries=/usr/lib/jvm/java-11-openjdk 改成这样。 官方源镜像中指定的JAVA_HOME地址。 你可以试试

1 个赞

我测试了一下, 可以了, 非常感谢~