Could not find a default branch to fall back in Jenkins with sonarscanner cli image

Need some help understand how this occurs. We moved to sonarcloud and every repository that uses this shared jenkins pipeline has been scanning successfully for a month or so, but recently we have had a few repositories that have started to fail due to this error. These repos were working fine before. We did have a new repository that failed on its initial scan.
We don’t import them into sonarcloud. We use the sonar-scanner cli to initiate the scan.

I found another community message along these lines, but I’m still struggling to understand why this happens for previously working and new repos. Any help would be appreciated.

  • ALM used - Github
  • CI system used - Jenkins running sonar-scanner container image which uses the Docker plugin with shared volume so it has access to the source files and test outputs
  • Scanner command used
    docker run -t -d -u 1000:1000 -v /workspace:/usr/src -e " -Dsonar.login=****** -Dsonar.organization=my_org -Dsonar.projectName=project-ts -Dsonar.projectVersion=2.106.0 -Dsonar.projectKey=project-ts"
  • Languages of the repository
  • Error observed
13:28:53  + sonar-scanner
13:28:53  INFO: Scanner configuration file: /opt/sonar-scanner/conf/
13:28:53  INFO: Project root configuration file: /tmp/****-e9f83b7e/workspace/Fleet Management/hpnotiq-ts/
13:28:53  INFO: SonarScanner
13:28:53  INFO: Java 11.0.18 Alpine (64-bit)
13:28:53  INFO: Linux 4.19.0-16-cloud-amd64 amd64
13:28:53  INFO: -Dsonar.login=****** -Dsonar.organization=my_org -Dsonar.projectName=project-ts -Dsonar.projectVersion=2.106.0 -Dsonar.projectKey=project-ts
13:28:52  $ docker top c57a3e51593f78fbc057d5fd6a7dce81146cb85929b9162e38b293aeccaa52a9 -eo pid,comm
13:28:54  INFO: User cache: /opt/sonar-scanner/.sonar/cache
13:28:59  INFO: Analyzing on SonarCloud
13:28:59  INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
13:28:59  INFO: Load global settings
13:29:00  INFO: Load global settings (done) | time=563ms
13:29:00  INFO: Server id: 1BD809FA-AWHW8ct9-T_TB3XqouNu
13:29:00  INFO: User cache: /opt/sonar-scanner/.sonar/cache
13:29:00  INFO: Load/download plugins
13:29:00  INFO: Load plugins index
13:29:01  INFO: Load plugins index (done) | time=505ms
13:29:33  INFO: Load/download plugins (done) | time=28025ms
13:29:33  INFO: Loaded core extensions: developer-scanner
13:29:33  INFO: Found an active CI vendor: 'Jenkins'
13:29:33  INFO: Load project settings for component key: 'project-ts'
13:29:33  INFO: Process project properties
13:29:33  INFO: Execute project builders
13:29:33  INFO: Execute project builders (done) | time=1ms
13:29:33  INFO: Project key: project-ts
13:29:33  INFO: Base dir: /tmp/****-e9f83b7e/workspace/Project/project-ts
13:29:33  INFO: Working dir: /tmp/****-e9f83b7e/workspace/Project/project-ts/.scannerwork
13:29:33  INFO: Load project branches
13:29:33  INFO: Load project branches (done) | time=472ms
13:29:33  INFO: Check ALM binding of project 'project-ts'
13:29:33  INFO: Detected project binding: NONEXISTENT
13:29:33  INFO: Check ALM binding of project 'project-ts' (done) | time=474ms
13:29:33  INFO: Load project pull requests
13:29:33  INFO: Load project pull requests (done) | time=485ms
13:29:33  INFO: Load branch configuration
13:29:33  INFO: ------------------------------------------------------------------------
13:29:33  INFO: ------------------------------------------------------------------------
13:29:33  INFO: Total time: 38.594s
13:29:33  INFO: Final Memory: 20M/88M
13:29:33  INFO: ------------------------------------------------------------------------
13:29:33  ERROR: Error during SonarScanner execution
13:29:33  ERROR: Could not find a default branch to fall back on.
13:29:33  ERROR: 
13:29:33  ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.
  • Steps to reproduce
    Unbound repository
    All default settings and no analysis configuration setup
    Executing this command in the job or even run locally produces the same result
  • Potential workaround
    Running sonar-scanner with the same command but leaving the empty.

Hey there.

This typically happens when the first scan of a project is done before the project is created in SonarCloud, and that scan is of a short-lived branch.

Does the project already exist in SonarCloud?

So I’ve had a couple issues with a couple different repos.

  1. new repo, branch is being defined as a short lived and thus falling into this scenario. Is there a way to set the default long lived branches to be (develop,master,release/*)?

  2. existing repo, But I found the issue there as the projectKey was already set and our pipeline was massaging the key and changing due to a bug I introduced trying to fix issue 1.

Hey there.

It’s not possible to set the long-lived branch pattern before a project is created.

It sounds like this issue didn’t happen on other repos before because they were created in SonarCloud prior to analysis. Is that an option here?

(I would really like the error message to be more clear, and I’m advocating for that internally!)

Thanks for the responses @Colin.
For context we migrated from sonarqube internally hosted and all repos have sonar properties files. We have been largely inconsistent with setting the project key in these so we had a jenkins function that wrapped rerunning and executing sonar scanner runs.

Largely the issue I was trying to work around is that we don’t currently support pull request analysis, but want to at least get the main branch to be analysed before it gets deployed. We currently have our pipelines setup this way, but most don’t view develop as a MAIN branch or long term branch. So I need to likely delete our main branch and set that to develop and make sure this works across the board and set the long term branches according. I found how to set that all through the api so I should be good.