#!/usr/bin/env bash # Run a SonarQube analysis from this machine, using the same # sonar-project.properties that CI uses. # # Publishes to a SCRATCH project key by default, and that default is the point # of this script. The server is SonarQube Community, which has no branch # analysis: every scan overwrites the single "main" analysis of whatever project # key it is given. Scanning a feature branch under the real key would replace # CI's picture of main with your working tree, silently. # # ./scripts/scan-local.sh # -> redefined-designs-local # ./scripts/scan-local.sh redefined-designs # -> the real project, deliberate # # Needs SONARQUBE_URL and SONARQUBE_TOKEN in the environment, and Docker. The # scanner runs in a container because it needs Java 11+, and the Java on the dev # machine is 8. set -euo pipefail PROJECT_KEY="${1:-redefined-designs-local}" REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" if [[ -z "${SONARQUBE_URL:-}" || -z "${SONARQUBE_TOKEN:-}" ]]; then echo "SONARQUBE_URL and SONARQUBE_TOKEN must be set." >&2 exit 1 fi if [[ "$PROJECT_KEY" == "redefined-designs" ]]; then echo "Publishing to the REAL project — this replaces CI's analysis of main until the next CI run." echo "Ctrl-C within 5s to abort." sleep 5 fi # Docker needs a Windows-style path here; MSYS_NO_PATHCONV stops Git Bash # rewriting the container-side path. HOST_PATH="$REPO_ROOT" if [[ "$(uname -s)" == MINGW* || "$(uname -s)" == MSYS* ]]; then HOST_PATH="$(cd "$REPO_ROOT" && pwd -W)" fi echo "Scanning $HOST_PATH -> $PROJECT_KEY on $SONARQUBE_URL" MSYS_NO_PATHCONV=1 docker run --rm \ -e SONAR_HOST_URL="$SONARQUBE_URL" \ -e SONAR_TOKEN="$SONARQUBE_TOKEN" \ -v "$HOST_PATH:/usr/src" \ sonarsource/sonar-scanner-cli:5 \ -Dsonar.projectKey="$PROJECT_KEY" \ -Dsonar.working.directory=/tmp/scannerwork \ "${@:2}"