Monorepo and sonar-project.properties

Template for a good new topic, formatted with Markdown:

  • Github
  • Github Actions
  • Ruby, JavaScript

We have a monorepo using a Ruby on Rails backend and a JavaScript client in the same repository. I’m trying to get them both set up for code coverage & analysis, and the monorepo page (https://sonarcloud.io/documentation/advanced-setup/monorepo-support/) gets me most of the way there, but I feel like I’m missing the last mile.

I’ve created a Github Action to run tests/lint for each project. In SonarCloud, I’ve created a Sonar project for each project, and a corresponding sonar-project.properties file with the relevant key & identifier. I’ve got the different tokens set up as Github secrets. I then attempt to set up the Github Actions using the guides on your website, by adding a build.yml and using the provided Github Action SonarSource/sonarcloud-github-action:

name: Build
on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  sonarcloud:
    name: SonarCloud
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

However, when I run this, I get:

INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 45.815s
ERROR: Error during SonarScanner execution
INFO: Final Memory: 7M/24M
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.organization
ERROR: 
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.
INFO: ------------------------------------------------------------------------

This actually makes sense to me, because I don’t have a top-level sonar-project.properties nor even a top-level job. My mental model is that there are 2 completely independent projects, each running under a different folder within the monorepo (backend/ and web/), each with their own sonar-project.properties. Presumably, there will also need to be 2 different SonarCloud scans.

If I want to trigger a SonarCloud scan of these projects, do I use the Github action (sonarsource/sonarcloud-github-action)? Or does that github action not work in monorepos, and I’ll need to manually download & run the sonar-scanner executable?

Hello @davigoli ,

Welcome to the community!

You can find an example set up here and you should be able to continue using the GitHub Action. From what I can see, adding the following lines to the SonarCloud Scan job should make it work (and replace backend with web for the other projects’ GitHub Action):

      with:
        projectBaseDir: backend

That way the project-level sonar-project.properties files will be detected correctly.

Thanks, that seems to be doing something! But now I get a different error:

INFO: Load project pull requests
INFO: Load project pull requests (done) | time=128ms
INFO: Load branch configuration
INFO: Github event: pull_request
INFO: Auto-configuring pull request 19467
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 37.217s
INFO: Final Memory: 7M/27M
ERROR: Error during SonarScanner execution
ERROR: Could not find a default branch to fall back on.
ERROR: 
INFO: ------------------------------------------------------------------------
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.

Ah, turns out I was using the wrong projectKey. It’s working now. Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.