Skip to content

Commit cede793

Browse files
committed
workflow file added for sonarqube
1 parent 6efcd87 commit cede793

File tree

2 files changed

+68
-51
lines changed

2 files changed

+68
-51
lines changed

.github/workflows/main.yml

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CareerPath Workflow - (with SonarQube)
1+
name: CareerPath Workflow - (SonarCloud)
22

33
on:
44
push:
@@ -21,61 +21,35 @@ jobs:
2121
build:
2222
runs-on: ubuntu-latest
2323

24-
services:
25-
sonarqube:
26-
image: sonarqube:lts
27-
ports:
28-
- 9000:9000
29-
env:
30-
SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: "true"
31-
options: >-
32-
--health-cmd="exit 0"
33-
-e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true
34-
-e ES_JAVA_OPTS="-Xms512m -Xmx512m"
35-
3624
steps:
37-
25+
# Checkout
3826
- uses: actions/checkout@v5
27+
with:
28+
fetch-depth: 0 # SonarCloud needs full history
3929

30+
# Install .NET
4031
- name: Setup .NET
4132
uses: actions/setup-dotnet@v4
4233
with:
4334
dotnet-version: ${{ env.DOTNET_VERSION }}
4435

45-
- name: Wait for SonarQube to start
46-
run: |
47-
for i in {1..30}; do
48-
if curl -s http://localhost:9000 >/dev/null; then
49-
echo "SonarQube is up !!!"; break;
50-
fi
51-
echo "Waiting for SonarQube...."; sleep 10
52-
done
53-
54-
- name: Install SonarScanner
55-
uses: sonarsource/sonarqube-scan-action@v2
56-
57-
- name: Begin Sonar Analysis
58-
env:
59-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
60-
run: |
61-
dotnet sonarscanner begin \
62-
/k:"CareerPathApp" \
63-
/d:sonar.host.url="http://sonarqube:9000" \
64-
/d:sonar.login="${{ secrets.SONAR_TOKEN }}" \
65-
/d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml"
66-
67-
- name: Restore dependencies
36+
# Restore
37+
- name: Restore
6838
run: dotnet restore ./CareerPath.sln
6939

40+
# Build
7041
- name: Build
71-
run: dotnet build ./API/API.csproj --configuration Release
42+
run: dotnet build ./API/API.csproj --configuration Release --no-restore
7243

73-
- name: Test (Generate Coverage)
44+
# Test + Code Coverage
45+
- name: Test
7446
run: dotnet test ./API.Tests/API.Tests.csproj \
7547
--collect:"XPlat Code Coverage" \
76-
--results-directory "./TestResults"
48+
--results-directory "./TestResults" \
49+
--no-build
7750

78-
- name: Convert Coverage to OpenCover
51+
# Convert Cobertura → OpenCover
52+
- name: Convert Coverage
7953
run: |
8054
report=$(find ./TestResults -name "coverage.cobertura.xml" | head -n 1)
8155
echo "Found report: $report"
@@ -85,26 +59,22 @@ jobs:
8559
-targetdir:coveragereport \
8660
-reporttypes:opencover
8761
88-
- name: End Sonar Analysis
89-
env:
90-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
91-
run: |
92-
dotnet sonarscanner end \
93-
/d:sonar.login="${{ secrets.SONAR_TOKEN }}"
94-
62+
# Cleanup publish
9563
- name: Clean API publish output
9664
run: rm -rf ${{ env.BUILD_OUTPUT }}
9765

98-
- name: Publish
66+
# Publish App
67+
- name: Publish Project
9968
run: dotnet publish ./API/API.csproj -c Release -o ${{ env.BUILD_OUTPUT }} --no-build
10069

101-
- name: Upload artifact
70+
# Upload artifacts
71+
- name: Upload Build Artifact
10272
uses: actions/upload-artifact@v4
10373
with:
10474
name: dotnet-app
10575
path: ${{ env.BUILD_OUTPUT }}
10676

107-
- name: Upload Coverage Report (optional)
77+
- name: Upload Coverage Report
10878
uses: actions/upload-artifact@v4
10979
with:
11080
name: code-coverage
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: SonarQube
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
types: [opened, synchronize, reopened]
8+
jobs:
9+
build:
10+
name: Build and analyze
11+
runs-on: windows-latest
12+
steps:
13+
- name: Set up JDK 17
14+
uses: actions/setup-java@v4
15+
with:
16+
java-version: 17
17+
distribution: 'zulu' # Alternative distribution options are available.
18+
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
21+
- name: Cache SonarQube Cloud packages
22+
uses: actions/cache@v4
23+
with:
24+
path: ~\sonar\cache
25+
key: ${{ runner.os }}-sonar
26+
restore-keys: ${{ runner.os }}-sonar
27+
- name: Cache SonarQube Cloud scanner
28+
id: cache-sonar-scanner
29+
uses: actions/cache@v4
30+
with:
31+
path: ${{ runner.temp }}\scanner
32+
key: ${{ runner.os }}-sonar-scanner
33+
restore-keys: ${{ runner.os }}-sonar-scanner
34+
- name: Install SonarQube Cloud scanner
35+
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
36+
shell: powershell
37+
run: |
38+
New-Item -Path ${{ runner.temp }}\scanner -ItemType Directory
39+
dotnet tool update dotnet-sonarscanner --tool-path ${{ runner.temp }}\scanner
40+
- name: Build and analyze
41+
env:
42+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
43+
shell: powershell
44+
run: |
45+
${{ runner.temp }}\scanner\dotnet-sonarscanner begin /k:"Suraj-Varade_CareerPath-Clean-Architecture-.NET-9" /o:"suraj-varade" /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
46+
dotnet build
47+
${{ runner.temp }}\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"

0 commit comments

Comments
 (0)