|
| 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