Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit f69d120

Browse files
author
Jamie Brynes
authored
Add sonarcloud analysis (#1363)
1 parent fa5dfa2 commit f69d120

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

.buildkite/premerge.steps.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ windows: &windows
1313
- "environment=production"
1414
- "permission_set=builder"
1515
- "platform=windows"
16-
- "queue=${WINDOWS_BUILDER_QUEUE:-v4-20-03-27-114536-bk10005-d340403f-d}"
16+
- "queue=${WINDOWS_BUILDER_QUEUE:-v4-20-05-14-120002-bk11367-f5a4805a-d}"
1717
- "scaler_version=2"
1818
- "minimum_instances=1"
1919
- "agent_count=1"
@@ -77,6 +77,13 @@ steps:
7777
artifact_paths:
7878
- logs/**/*
7979

80+
- label: ":windows: ~ sonar scanner :sonarqube:"
81+
depends_on:
82+
- step: "test-windows"
83+
command: bash -c ci/sonar-scanner.sh
84+
soft_fail: true # TODO: Remove once we've ran with this a bit more.
85+
<<: *windows
86+
8087
- label: ":darwin: ~ test"
8188
id: "test-darwin"
8289
command: bash -c ci/test.sh

ci/sonar-scanner.sh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env bash
2+
3+
set -e -u -o pipefail
4+
5+
if [[ -n "${DEBUG-}" ]]; then
6+
set -x
7+
fi
8+
9+
cd "$(dirname "$0")/../"
10+
11+
PROJECT_DIR="$(pwd)"
12+
13+
source .shared-ci/scripts/pinned-tools.sh
14+
15+
# TODO: Add a more specific secret-type to vault and swap these around
16+
TOKEN=$(imp-ci secrets read --environment="production" --buildkite-org="improbable" --secret-type="generic-token" --secret-name="gdk-for-unity-bot-sonarcloud-token" --field="token")
17+
18+
args=()
19+
args+=("-k:spatialos_gdk-for-unity")
20+
args+=("-o:spatialos")
21+
args+=("-d:sonar.login=${TOKEN}")
22+
args+=("-d:sonar.project_key=spatialos_gdk-for-unity")
23+
args+=("-d:sonar.host.url=https://sonarcloud.io")
24+
args+=("-d:sonar.buildString=${BUILDKITE_MESSAGE}")
25+
args+=("-d:sonar.log.level=${SONAR_LOG_LEVEL:-"INFO"}")
26+
27+
# Exclude all generated code from analysis.
28+
args+=("-d:sonar.exclusions=Assets/Generated/Source/**/*.cs")
29+
30+
if [[ -n "${DEBUG-}" ]]; then
31+
args+=("-d:sonar.verbose=true")
32+
fi
33+
34+
if [[ -n "${SONAR_PROJECT_DATE:-}" ]]; then
35+
# For historical analysis. Note - can only supply a date later than the most recent one in the database.
36+
args+=("-d:sonar.projectDate=${SONAR_PROJECT_DATE}")
37+
fi
38+
39+
if [[ -n "${SONAR_REVISION:-}" ]]; then
40+
# Override revision. Useful during historical analysis (i.e. override it to a tag that's being analysed).
41+
args+=("-d:sonar.scm.revision=${SONAR_REVISION}")
42+
fi
43+
44+
if [[ -n "${BUILDKITE_PULL_REQUEST:-}" && "${BUILDKITE_PULL_REQUEST}" != "false" ]]; then
45+
# PR analysis, relies on BK building PRs
46+
# https://docs.sonarqube.org/latest/analysis/pull-request/
47+
args+=("-d:sonar.pullrequest.key=${BUILDKITE_PULL_REQUEST}")
48+
args+=("-d:sonar.pullrequest.branch=${BUILDKITE_BRANCH}")
49+
args+=("-d:sonar.pullrequest.base=${BUILDKITE_PULL_REQUEST_BASE_BRANCH}")
50+
else
51+
# Branch analysis, allows for diff-level reporting on short-lived branches
52+
# https://docs.sonarqube.org/latest/branches/overview/
53+
args+=("-d:sonar.branch.name=${BUILDKITE_BRANCH}")
54+
fi
55+
56+
# The way Sonar Scanner for MSBuild/dotnet works is:
57+
# 1. You need to run `dotnet-sonarscanner begin` which installs hooks into MSBuild and applies your quality profiles (from sonarcloud.io)
58+
# 2. Build the project using `msbuild`
59+
# 3. Run `dotnet-sonarscanner end` which runs post-processing. (This looks like it runs an embedded version of the generic sonar-scanner CLI which requires the JRE).
60+
#
61+
# To enable this to work with Unity, we first need to generate the `.sln` and `.csproj` files in order to build them with `msbuild`
62+
# For ease of use, we execute msbuild through `dotnet`!
63+
pushd "workers/unity"
64+
65+
echo "--- Downloading Buildkite artifacts :buildkite:"
66+
67+
# Download test coverage reports from previous build step.
68+
buildkite-agent artifact download \
69+
logs\\coverage-results\\*.xml \
70+
. \
71+
--step ":windows: ~ test"
72+
73+
# This finds all .xml files in the logs/ directory and concatentates their relative path together, separated by comma:
74+
# E.g. - -d:sonar.cs.opencover.reportsPath=./logs/coverage-results/my-first-report.xml,./logs/coverage-results/my-second-report.xml
75+
# Wildcards don't appear to play nice with this.
76+
args+=("-d:sonar.cs.opencover.reportsPaths=$(find ./logs -name "*.xml" | paste -sd "," -)")
77+
78+
echo "--- Generate csproj & sln files :csharp:"
79+
dotnet run -p "${PROJECT_DIR}/.shared-ci/tools/RunUnity/RunUnity.csproj" -- \
80+
-batchmode \
81+
-projectPath "${PROJECT_DIR}/workers/unity" \
82+
-quit \
83+
-executeMethod UnityEditor.SyncVS.SyncSolution
84+
85+
echo "--- Execute sonar-scanner :sonarqube:"
86+
dotnet-sonarscanner begin "${args[@]}"
87+
dotnet msbuild ./unity.sln -t:Rebuild -nr:false
88+
dotnet-sonarscanner end "-d:sonar.login=${TOKEN}"
89+
popd
90+

0 commit comments

Comments
 (0)