Issue running analysis on a new project from BitBucket

Hello,

We’re using the latest version of the sonar-scanner through maven, here’s the command we run in our bitbucket-pipelines.yml

mvn compile org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \
-Dsonar.projectKey=$SONAR_ORGANIZATION_$BITBUCKET_REPO_SLUG \
-Dsonar.organization=$SONAR_ORGANIZATION \
-Dsonar.host.url=$SONAR_URL \
-Dsonar.login=$SONAR_TOKEN

The project is created in our SonarCloud organization but no analysis has ran against it.
We setup a bitbucket-pipelines.yml to analyse master branch.

image: maven:3.5.2-jdk-8
pipelines:
  default:
    - step:
        name: Build and run the SonarCloud Scanner
        script:
          - mvn --settings settings.xml -B compile org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.branch.name=$BITBUCKET_BRANCH -Dsonar.projectKey=$SONAR_ORGANIZATION_$BITBUCKET_REPO_SLUG -Dsonar.organization=$SONAR_ORGANIZATION -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_TOKEN

The sonar-scanner show the following error:

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project service-example: Project was never analyzed. A regular analysis is required before a branch analysis -> [Help 1]

This workflow was functional before.

Why is it defaulting to branch analysis instead of regular analysis for the master branch.

I ran some more tests, it appears that it works fine if I use the SonarCloud UI for the bitbucket integration to create the SonarCloud project and then run the analysis.

Hi Jean-Bernard,
Can you share the full analysis log (maven log) from your first post?

[INFO] --- sonar-maven-plugin:3.6.0.1398:sonar (default-cli) @ service-example ---
[INFO] Bitbucket Cloud Pipelines detected
[INFO] User cache: /root/.sonar/cache
[INFO] SonarQube version: 7.7.0
[INFO] Default locale: "en", source code encoding: "UTF-8" (analysis is platform dependent)
[INFO] Load global settings
[INFO] Load global settings (done) | time=583ms
[INFO] Server id: BD367519-AWHW8ct9-T_TB3XqouNu
[INFO] User cache: /root/.sonar/cache
[INFO] Load/download plugins
[INFO] Load plugins index
[INFO] Load plugins index (done) | time=306ms
[INFO] Load/download plugins (done) | time=367ms
[INFO] Loaded core extensions: developer-scanner
[INFO] No project and organization key detected from Bitbucket Cloud Pipelines.
[INFO] Process project properties
[INFO] Execute project builders
[INFO] Execute project builders (done) | time=4ms
[INFO] Project key: ops-service-example
[INFO] Base dir: /opt/atlassian/pipelines/agent/build
[INFO] Working dir: /opt/atlassian/pipelines/agent/build/target/sonar
[INFO] Load project settings for component key: 'ops-service-example'
[INFO] Load project branches
[INFO] Load project branches (done) | time=91ms
[INFO] Load project pull requests
[INFO] Load project pull requests (done) | time=93ms
[INFO] Load branch configuration
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.388 s
[INFO] Finished at: 2019-02-20T21:02:57Z
[INFO] Final Memory: 51M/761M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project service-example: Project was never analyzed. A regular analysis is required before a branch analysis -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Can you echo $SONAR_ORGANIZATION_$BITBUCKET_REPO_SLUG (which you supply as the projectKey) during the Bitbucket Pipeline execution and post it here? It’s pretty strange that there is an underscore in it but in the logs it is resolved to ops-service-example which has no underscore at all:

[INFO] Load project settings for component key: 'ops-service-example'

@Michal_Duda you are correct $SONAR_ORGANIZATION_$BITBUCKET_REPO_SLUG is not being interpolated correctly as bash think that the variable name is $SONAR_ORGANIZATION_ and not $SONAR_ORGANIZATION (it interpolate as empty value).

So the project key is wrong.
Can we get a more descriptive message on this error?

Sorry about that.
Thank you for your help!

1 Like

Thanks for feedback! I opened a ticket to improve the error message: https://jira.sonarsource.com/browse/SONARCLOUD-470

1 Like