From 0fba67a932a54b0c59443d800ea54f30c6fc08b3 Mon Sep 17 00:00:00 2001 From: Chris Sherlock Date: Tue, 19 Mar 2024 11:46:37 +0000 Subject: [PATCH] Add initial SonarCloud scan workflow --- .../continuous-integration-dotnet.yml | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 .github/workflows/continuous-integration-dotnet.yml diff --git a/.github/workflows/continuous-integration-dotnet.yml b/.github/workflows/continuous-integration-dotnet.yml new file mode 100644 index 0000000..301470b --- /dev/null +++ b/.github/workflows/continuous-integration-dotnet.yml @@ -0,0 +1,76 @@ +name: .NET Build and Test + +on: + push: + branches: + - main + pull_request: + branches: [ main ] + types: [ opened, synchronize, reopened ] + +env: + DOTNET_VERSION: '6.0.403' + JAVA_VERSION: '17' + +jobs: + build-and-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.ref }} + fetch-depth: 0 # Shallow clones disabled for a better relevancy of SC analysis + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + + - name: Setup JDK + uses: actions/setup-java@v4 + with: + distribution: 'microsoft' + java-version: ${{ env.JAVA_VERSION }} + + - name: Cache SonarCloud packages + uses: actions/cache@v4 + with: + path: ~\sonar\cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + + - name: Cache SonarCloud scanner + id: cache-sonar-scanner + uses: actions/cache@v3 + with: + path: .\.sonar\scanner + 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: dotnet tool install --global dotnet-sonarscanner + + - name: Install dotnet reportgenerator + run: dotnet tool install --global dotnet-reportgenerator-globaltool + + - name: Add nuget package source + run: dotnet nuget add source --username USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/DFE-Digital/index.json" + + - name: Restore tools for tests + run: dotnet tool restore + + # - name: Restore dependencies + # run: dotnet restore TramsDataApi.sln + + - name: Build, Test and Analyze + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + ConnectionStrings__DefaultConnection: ${{ env.CONNECTION_STRING }} + run: | + dotnet-sonarscanner begin /k:"DFE-Digital_identifiers-api" /o:"dfe-digital" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.coverageReportPaths=CoverageReport/SonarQube.xml + dotnet build --no-restore + dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" + reportgenerator -reports:./**/coverage.cobertura.xml -targetdir:./CoverageReport -reporttypes:SonarQube + dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"