I’m learning how to integrate SonarCloud with Github Actions in a personal project. The Github repository that I am using can be accessed by clicking here.
The solution I am developing was created with version 8.0 of .NET. By default, all source code is present in the src directory. All test projects (unit, integration and functional) are present in the tests directory.
My Github Actions workflow that is integrating with SonarCloud is called Develop Workflow, and can be accessed by clicking here.
name: Develop Workflow
on:
push:
branches: ["develop"]
permissions:
contents: write
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
- name: Restore dependencies
run: dotnet restore
working-directory: src
- name: Build solution
run: dotnet build --configuration Release --no-restore
working-directory: src
unit_test:
name: Unit Tests
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
- name: Restore dependencies
run: dotnet restore
working-directory: tests/Postech.PhaseOne.GroupEight.TechChallenge.UnitTests
- name: Build test project
run: dotnet build --no-restore
working-directory: tests/Postech.PhaseOne.GroupEight.TechChallenge.UnitTests
- name: Run tests
run: dotnet test --verbosity normal
working-directory: tests/Postech.PhaseOne.GroupEight.TechChallenge.UnitTests
functional_test:
name: Functional Tests
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
- name: Restore dependencies
run: dotnet restore
working-directory: tests/Postech.PhaseOne.GroupEight.TechChallenge.FunctionalTests
- name: Build test project
run: dotnet build --no-restore
working-directory: tests/Postech.PhaseOne.GroupEight.TechChallenge.FunctionalTests
- name: Run tests
run: dotnet test --verbosity normal
working-directory: tests/Postech.PhaseOne.GroupEight.TechChallenge.FunctionalTests
sonarcloud:
name: SonarCloud Scan
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
- name: Install Java 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
path: ~sonarcache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache SonarCloud scanner
id: cache-sonar-scanner
uses: actions/cache@v3
with:
path: ..sonarscanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner
- name: Install SonarCloud scanner
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
run: |
mkdir -p ./.sonar/scanner
dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
- name: Install dotnet coverage
run: dotnet tool install --global dotnet-coverage
- name: Solution Scan
env:
SONAR_TOKEN: ${{secrets.SONAR_TOKEN}}
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
./.sonar/scanner/dotnet-sonarscanner begin /k:"lucasdirani_GithubActionsTest" /o:"tech-challenge-arquitetura-sistemas-net" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml /d:sonar.sources="src/" /d:sonar.tests="tests/" /d:sonar.host.url="https://sonarcloud.io"
dotnet build src/Postech.PhaseOne.GroupEight.TechChallenge.sln --no-incremental
~/.dotnet/tools/dotnet-coverage collect "dotnet test src/Postech.PhaseOne.GroupEight.TechChallenge.sln" -f xml -o "coverage.xml"
./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
As my test and source code directories are separate, I followed the recommendations from the official SonarCloud documentation, and indicated their respective locations in the dotnet-sonarscanner begin command.
./.sonar/scanner/dotnet-sonarscanner begin /k:"lucasdirani_GithubActionsTest" /o:"tech-challenge-arquitetura-sistemas-net" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml /d:sonar.sources="src/" /d:sonar.tests="tests/" /d:sonar.host.url="https://sonarcloud.io"
However, during the execution of the pipeline (the last execution I performed can be found by clicking here), for some reason the scanner is pointing out the indexing error shared right after.
ERROR: File
src/Postech.PhaseOne.GroupEight.TechChallenge.Api/Setup/RepositorySetup.cs
can’t be indexed twice. Please check that inclusion/exclusion patterns
produce disjoint sets for main and test files
I’m confused by the error since the RepositorySetup file is only being referenced in a single project in my solution. I would like to ask for help to resolve the problem in question. I tried several alternatives, but the error persists regardless of what I do.