Search code examples
sonarqubemonoreposonarcloud

How to set up a monorepo in SonarCloud with a Java + Angular application


I have developed an application where the backend is developed using Java language (with maven) and the frontend is using Angular language. I host both parts in one project in github.

Now I am interested on the use of SonarQube on SonarCloud. For this purpose, I am following the information gathered from the community sonarsource and the standard documentation from sonarcloud. The idea is to use GiHub Actions for analyzing the projects.

What I have created is a .github/workflows/build.yml on the root folder with the content:

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: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Cache SonarCloud packages
        uses: actions/cache@v1
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache Maven packages
        uses: actions/cache@v1
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2
      - name: Build and analyze
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=kendo-tournament-backend
        with:
          projectBaseDir: ./backend/
      - name: SonarCloud Frontend 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 }}
        with:
          projectBaseDir: ./frontend/

Where basically, I create some steps for executing the maven project, and another extra step to execute the frontend. On both of them, I included the projectBaseDir with the path to both projects' folders as specified here.

Also, as suggested on the documentation, I have included a sonar-project.properties on the root folder of the frontend folder with:

sonar.projectKey=kendo-tournament-frontend
sonar.organization=softwaremagico

# This is the name and version displayed in the SonarCloud UI.
#sonar.projectName=Kendo Tournament Manager Frontend
#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
#sonar.sources=.

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

And for the backend, I have updated the root pom.xml with the:

<properties>
  <sonar.organization>softwaremagico</sonar.organization>
  <sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>

As required.

But, no analyses scan is launch for any of both projects. And SonarCloud looks like is ignoring the configuration.

enter image description here

Probably, something is missing but I cannot imagine what. What steps are needed to set up a monorepository correctly using Java and Angular in Github?


Solution

  • Ok, after the example obtained from here. The changes I have made are:

    • Two different workflows on github, one for backend and one for frontend. Not one workflow with all steps together.

    • Include two different sonar-project.properties. One inside the backend folder, and one inside the frontend folder. Now I have added the sonar.sources line as follows:

    sonar.projectKey=kendo-tournament-backend
    sonar.organization=softwaremagico
    sonar.sources=.
    

    That ensures that is only for this folder.

    For launching CircleCi with Sonar (for backend) edit file .circleci/config.yml:

    version: 2.1
    
    jobs:
      build:
        docker:
          - image: 'circleci/openjdk:11-jdk'
        working_directory: ~/KendoTournamentManager/backend
        steps:
          - checkout:
              path: ~/KendoTournamentManager
          - run:
              name: Analyze on SonarCloud
              command: mvn verify sonar:sonar -Dsonar.projectKey=kendo-tournament-backend
    
    workflows:
      main:
        jobs:
          - build:
              context: SonarCloud
    
    

    And now seems working fine:

    enter image description here