Skip to content

Commit 53ad4df

Browse files
Moved sonar analysis out to it's own script file
1 parent a64c3fa commit 53ad4df

File tree

2 files changed

+136
-80
lines changed

2 files changed

+136
-80
lines changed
Lines changed: 72 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,73 @@
1-
name: Analysis stage
2-
3-
on:
4-
workflow_call:
5-
inputs:
6-
unit_test_dir:
7-
description: Directory containing the unit tests
8-
required: true
9-
type: string
10-
build_datetime:
11-
description: Build datetime
12-
required: true
13-
type: string
14-
build_timestamp:
15-
description: Build timestamp
16-
required: true
17-
type: string
18-
build_epoch:
19-
description: Build epoch
20-
required: true
21-
type: string
22-
nodejs_version:
23-
description: Node.js version
24-
required: true
25-
type: string
26-
python_version:
27-
description: Python version
28-
required: true
29-
type: string
30-
terraform_version:
31-
description: Terraform version
32-
required: true
33-
type: string
34-
version:
35-
description: Version of the software
36-
required: true
37-
type: string
38-
39-
jobs:
40-
download-test-coverage:
41-
name: Download test coverage
42-
runs-on: ubuntu-latest
43-
timeout-minutes: 2
44-
steps:
45-
- name: Checkout code
46-
uses: actions/checkout@v4
47-
with:
48-
submodules: true
49-
- name: Download coverage report
50-
uses: actions/download-artifact@v4
51-
with:
52-
name: test-coverage-report
53-
path: coverage
1+
name: "Perform static analysis"
2+
description: "Perform static analysis with SonarCloud for .NET projects"
3+
inputs:
4+
sonar_organisation_key:
5+
description: "Sonar organisation key, used to identify the project"
6+
required: true
7+
sonar_project_key:
8+
description: "Sonar project key, used to identify the project"
9+
required: true
10+
sonar_token:
11+
description: "Sonar token, the API key"
12+
required: true
13+
coverage_path:
14+
description: "Path to coverage reports"
15+
required: false
16+
default: "coverage"
17+
runs:
18+
using: "composite"
19+
steps:
20+
- name: Set up JDK 17
21+
uses: actions/setup-java@v4
22+
with:
23+
java-version: 17
24+
distribution: "zulu"
25+
26+
- name: Install .NET SDKs
27+
uses: actions/setup-dotnet@v4
28+
with:
29+
dotnet-version: |
30+
7.0.x
31+
8.0.x
32+
9.0.x
33+
34+
- name: Cache SonarQube packages
35+
uses: actions/cache@v4
36+
with:
37+
path: ~/.sonar/cache
38+
key: ${{ runner.os }}-sonar-${{ hashFiles('**/*.csproj') }}
39+
restore-keys: |
40+
${{ runner.os }}-sonar-
5441
55-
perform-static-analysis:
56-
name: Perform static analysis
57-
needs: [download-test-coverage]
58-
runs-on: ubuntu-latest
59-
permissions:
60-
id-token: write
61-
contents: read
62-
pull-requests: read
63-
timeout-minutes: 15
64-
steps:
65-
- name: Checkout code
66-
uses: actions/checkout@v4
67-
with:
68-
submodules: true
69-
fetch-depth: 0 # Full history for more accurate reporting
70-
- name: Download coverage report
71-
uses: actions/download-artifact@v4
72-
with:
73-
name: test-coverage-report
74-
path: coverage
75-
- name: Perform static analysis
76-
uses: ./.github/actions/perform-static-analysis
77-
with:
78-
sonar_organisation_key: ${{ vars.SONAR_ORGANISATION_KEY }}
79-
sonar_project_key: ${{ vars.SONAR_PROJECT_KEY }}
80-
sonar_token: ${{ secrets.SONAR_TOKEN }}
81-
coverage_path: "coverage"
42+
- name: Cache NuGet packages
43+
uses: actions/cache@v4
44+
with:
45+
path: ~/.nuget/packages
46+
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/*.sln') }}
47+
restore-keys: |
48+
${{ runner.os }}-nuget-
49+
50+
- name: Install SonarScanner
51+
shell: bash
52+
run: dotnet tool install --global dotnet-sonarscanner
53+
54+
- name: SonarCloud analysis
55+
shell: bash
56+
env:
57+
GITHUB_TOKEN: ${{ github.token }}
58+
SONAR_TOKEN: ${{ inputs.sonar_token }}
59+
run: |
60+
chmod +x ${{ github.workspace }}/scripts/reports/sonar-analysis.sh
61+
${{ github.workspace }}/scripts/reports/sonar-analysis.sh \
62+
"${{ inputs.sonar_project_key }}" \
63+
"${{ inputs.sonar_organisation_key }}" \
64+
"${{ inputs.sonar_token }}" \
65+
"${{ inputs.coverage_path }}" \
66+
"${{ github.token }}" \
67+
"${{ github.event_name }}" \
68+
"${{ github.head_ref }}" \
69+
"${{ github.base_ref }}" \
70+
"${{ github.event.pull_request.number }}" \
71+
"${{ github.repository }}" \
72+
"${{ github.ref }}" \
73+
"${{ github.sha }}"

scripts/reports/sonar-analysis.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Parameters
5+
SONAR_PROJECT_KEY="$1"
6+
SONAR_ORGANISATION_KEY="$2"
7+
SONAR_TOKEN="$3"
8+
COVERAGE_PATH="${4:-coverage}"
9+
GITHUB_TOKEN="$5"
10+
GITHUB_EVENT_NAME="$6"
11+
GITHUB_HEAD_REF="$7"
12+
GITHUB_BASE_REF="$8"
13+
GITHUB_EVENT_PR_NUMBER="$9"
14+
GITHUB_REPOSITORY="${10}"
15+
GITHUB_REF="${11}"
16+
GITHUB_SHA="${12}"
17+
18+
# Get PR information for SonarCloud
19+
if [[ "$GITHUB_EVENT_NAME" == "pull_request" || "$GITHUB_EVENT_NAME" == "pull_request_target" ]]; then
20+
PR_BRANCH="$GITHUB_HEAD_REF"
21+
PR_BASE="$GITHUB_BASE_REF"
22+
PR_KEY="$GITHUB_EVENT_PR_NUMBER"
23+
24+
echo "Running analysis for PR #${PR_KEY} from ${PR_BRANCH} into ${PR_BASE}"
25+
PR_ARGS="/d:sonar.pullrequest.key=${PR_KEY} /d:sonar.pullrequest.branch=${PR_BRANCH} /d:sonar.pullrequest.base=${PR_BASE} /d:sonar.pullrequest.github.repository=${GITHUB_REPOSITORY}"
26+
else
27+
BRANCH_NAME="${GITHUB_REF#refs/heads/}"
28+
if [[ "$BRANCH_NAME" != "main" && "$BRANCH_NAME" != "master" ]]; then
29+
echo "Running analysis for branch ${BRANCH_NAME}"
30+
PR_ARGS="/d:sonar.branch.name=${BRANCH_NAME}"
31+
else
32+
echo "Running analysis for main branch"
33+
PR_ARGS=""
34+
fi
35+
fi
36+
37+
# Debug info
38+
echo "GitHub event: $GITHUB_EVENT_NAME"
39+
echo "PR arguments: ${PR_ARGS}"
40+
41+
# Restore solution dependencies
42+
find . -name "*.sln" -exec dotnet restore {} \;
43+
44+
# Begin SonarScanner with coverage configuration and PR information
45+
dotnet sonarscanner begin \
46+
/k:"${SONAR_PROJECT_KEY}" \
47+
/o:"${SONAR_ORGANISATION_KEY}" \
48+
/d:sonar.token="${SONAR_TOKEN}" \
49+
/d:sonar.host.url="https://sonarcloud.io" \
50+
/d:sonar.cs.opencover.reportsPaths="${COVERAGE_PATH}/*.xml" \
51+
/d:sonar.cs.cobertura.reportsPaths="${COVERAGE_PATH}/cobertura.xml" \
52+
/d:sonar.coverage.exclusions="**/*Tests.cs,**/Tests/**/*.cs,**/test/**/*.ts,**/tests/**/*.ts,**/*.spec.ts,**/*.test.ts" \
53+
/d:sonar.tests="tests" \
54+
/d:sonar.test.inclusions="**/*.spec.ts,**/*.test.ts,**/tests/**/*.ts" \
55+
/d:sonar.verbose=true \
56+
/d:sonar.scm.provider=git \
57+
/d:sonar.scm.revision=${GITHUB_SHA} \
58+
${PR_ARGS}
59+
60+
# Build all solutions
61+
find . -name "*.sln" -exec dotnet build {} --no-restore \;
62+
63+
# End SonarScanner
64+
dotnet sonarscanner end /d:sonar.token="${SONAR_TOKEN}"

0 commit comments

Comments
 (0)