Detailed Monorepo setup info for github

I have a monorepo in github with several python projects in subfolders. I tried follow the information in Monorepo Support | SonarCloud Docs but there is some information I’m struggling to figure out.
I created 8 projects in SonarCloud using the “monorepo” option as described in the document. After that the projects all ask to configure analysis. I followed the github Actions option, but not sure exactly how to do it.
It seems to need the sonar-project.properties in the root of the repo, but that makes me specify a single project and not the sub projects within the monorepo.

I also created the .github/workflows/build.yml and setup the path to one of the subprojects. I’m guessing I would create multiple .yml files, one for each project I want to scan.

Can anyone clarify what I’m missing to get the multiple projects in the monorepo to show up with the correct analysis for their subfolder?

Thanks

Hi,

Welcome to the community!

The underlying assumption with monorepo support is here in the docs:

each project occupies its own directory within the repository

So you would put each project’s sonar.properties file in its directory. The same, I think, for the yml files.

 
HTH,
Ann

Thanks Ann. I believe I tried that yesterday, but it didnt seem to find anything. I think Github requires the yml files to be in the root of the project. I’ll try again today to see if maybe I made a mistake when I tried before.
And yes, each project has its own individual directory within the repository with no crossing between project references.

I tried moving the sonar.properties file to the sub-project directory, but during the scan, it gets this error:

ERROR: You must define the following mandatory properties for ‘Unknown’: sonar.projectKey, sonar.organization

Those are defined in the sonar-project.properties file, so it seems it just didnt find it.

Hi,

The properties file needs to be in the directory analysis is started from. Is that the sub-project directory?

 
Ann

I’m not really sure. Sorry I’m a beginner with SonarCloud. I have a repo in github with several sub-projects. I followed the documents when creating the project in SonarCloud, which had me create a build.yml and sonar-project.properties file. I just created them as stated in the web site.

I’m guessing I need to specify a location somewhere in the build.yml? The documentation for setting up a monorepo is very sparse, so I’m not really sure what to look for.

I found I need to put the build.yml in the root of the repo (not project) since github is looking for them there.
.github/workflows/build-testproject.yml

build-testproject.yml for one of the projects looks like

name: Scan Test Project
on:
  push:
    branches:
      - main
    paths:
      - 'lambdas/test/**'
  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 }}

Ah, looking back at your message, I finally realized, I need to add this to the build.yml to set the projectBaseDir. This really should be added to the monorepo support docs, since its a critical setting. Without that it won’t work.

uses: SonarSource/sonarcloud-github-action@master
with:
projectBaseDir: lambdas/test

Thank you, Ann, for getting me pointed in the right direction.

Hi yes can I echo Mike’s comments, I’m new to all this and have had a nightmare today trying to get a monorepo to work (still isn’t but I think I’m getting there). The docs page and examples are hopeless, I have had to do a ton of web searches to try to work this out. Thanks

2 Likes

Ditto. This would have been really nice to specify in the monorepo support docs.

If anyone else is trying to set up a monorepo, where SQ analysis is needed for each project in the repo, and you’re orchestrating with Github Actions…

  1. Add a sonar-project.properties file at the repo root:
sonar.projectKey=myorg_myrepo
sonar.organization=myorg
  1. Add a sonar-project.properties file at the project root:
sonar.projectKey=myorg_myrepo_myproject
sonar.organization=myorg
sonar.sources=.
sonar.tests=./tests
sonar.python.coverage.reportPaths=./coverage.xml
  1. Add this step to the Github Action for your project in the monorepo:
      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        with:
          projectBaseDir: dir_for_my_project_in_monorepo # <-------------
  1. You do not need separate sonar cloud tokens for each project in the repo.
1 Like