I created a basic test project on SonarCloud with Circle CI. Following the tutorial on the documentation, it worked perfectly, both on scanning the default branch and any PR.
Now I am trying to do the same thing in a “real” project, but this time I have this error.
ERROR: Error during SonarQube Scanner execution ERROR: Parameter ‘sonar.pullrequest.branch’ is mandatory for a pull request analysis
Below is the output of the sonar job (let me know if you need anything more). You can notice it fails just after the configuration of the branch. I am a little bit lost since, on my test project, it goes through the whole process without any issue. The only difference is that, for both Github and Sonarcloud, we use a plan, since it is for a company.
#!/bin/bash -eo pipefail
set -e
VERSION=4.1.0.1829
SONAR_TOKEN=$SONAR_TOKEN
SCANNER_DIRECTORY=/tmp/cache/scanner
export SONAR_USER_HOME=$SCANNER_DIRECTORY/.sonar
OS="linux"
echo $SONAR_USER_HOME
if [[ ! -x "$SCANNER_DIRECTORY/sonar-scanner-$VERSION-$OS/bin/sonar-scanner" ]]; then
curl -Ol https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$VERSION-$OS.zip
unzip -qq -o sonar-scanner-cli-$VERSION-$OS.zip -d $SCANNER_DIRECTORY
fi
chmod +x $SCANNER_DIRECTORY/sonar-scanner-$VERSION-$OS/bin/sonar-scanner
chmod +x $SCANNER_DIRECTORY/sonar-scanner-$VERSION-$OS/jre/bin/java
$SCANNER_DIRECTORY/sonar-scanner-$VERSION-$OS/bin/sonar-scanner
/tmp/cache/scanner/.sonar
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 40.4M 100 40.4M 0 0 11.0M 0 0:00:03 0:00:03 --:--:-- 11.0M
INFO: Scanner configuration file: /tmp/cache/scanner/sonar-scanner-4.1.0.1829-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/circleci/project/sonar-project.properties
INFO: SonarQube Scanner 4.1.0.1829
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 4.15.0-1052-aws amd64
INFO: User cache: /tmp/cache/scanner/.sonar/cache
INFO: SonarQube server 8.0.0
INFO: Default locale: "en", source code encoding: "UTF-8"
INFO: Load global settings
INFO: Load global settings (done) | time=613ms
INFO: Server id: 74E9293
INFO: User cache: /tmp/cache/scanner/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=133ms
INFO: Load/download plugins (done) | time=20884ms
INFO: Loaded core extensions: developer-scanner
INFO: Process project properties
INFO: Execute project builders
INFO: Execute project builders (done) | time=6ms
INFO: Project key: myrepo_myproject
INFO: Base dir: /home/circleci/project
INFO: Working dir: /home/circleci/project/.scannerwork
INFO: Load project settings for component key: 'myrepo_myproject'
INFO: Load project settings for component key: 'myrepo_myproject' (done) | time=138ms
INFO: Found an active CI vendor: 'CircleCI'
INFO: Load project branches
INFO: Load project branches (done) | time=129ms
INFO: Load project pull requests
INFO: Load project pull requests (done) | time=124ms
INFO: Load branch configuration
INFO: Auto-configuring pull request 8359
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 25.673s
INFO: Final Memory: 7M/200M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: Parameter 'sonar.pullrequest.branch' is mandatory for a pull request analysis
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.
Exited with code 2
Is it possible that you did not import this project through GitHub, and that you did do that with the test project? A Github API is called to determine the branch of the PR, but that can only be done when SonarCloud knows the location of the repository on GitHub side.
You can easily check if this is the case by going to your project page on SonarCloud, you should see a Github icon next to the project name if it is linked to Github.
Yes I see the GitHub icon next to the project name. The integration should be good because before trying to integrate Sonar into our Circle CI, I tested the “Autoscan” beta feature and it worked well.
Just to be sure I also went to the Pull Request menu on Sonar, and set the Provider to Github and the repo identifier, though I did not have to do that in my test project.
so what was the issue? the missing sonar.organization?
I came here via google search cause I have same issue but I have sonar.organiszation property set. the issue happens in a PR scan. The PR is comming from a fork repository.
Yes it was missing configuration on the sonar project configuration file.
However, regarding your stacktrace, it reminds me that sometimes, for PR, I had to add that in my circle ci file:
command: |
if [[ -z ${CIRCLE_PULL_REQUEST} ]]; then \
sonar-scanner; \
else \
sonar-scanner -Dsonar.pullrequest.key=${CIRCLE_PULL_REQUEST##*/} -Dsonar.pullrequest.branch=${CIRCLE_BRANCH}; \
fi