name: SonarQube Analysis on: push: pull_request: types: [opened, synchronize, reopened] concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: sonarqube: name: Build, Test & Analyse runs-on: ubuntu-latest timeout-minutes: 15 steps: - name: Checkout repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up JDK 17 uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin' - name: Make Maven wrapper executable run: chmod +x mvnw - name: Cache Maven packages uses: actions/cache@v4 with: path: ~/.m2/repository key: maven-${{ runner.os }}-${{ hashFiles('**/pom.xml') }} restore-keys: maven-${{ runner.os }}- - name: Cache SonarQube analysis data uses: actions/cache@v4 with: path: ~/.sonar/cache key: sonar-${{ runner.os }} restore-keys: sonar-${{ runner.os }} - name: Validate required secrets env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} SONAR_PROJECT_KEY: ${{ secrets.SONAR_PROJECT_KEY }} run: | [[ -n "$SONAR_TOKEN" ]] || { echo "::error::SONAR_TOKEN is not set"; exit 1; } [[ -n "$SONAR_HOST_URL" ]] || { echo "::error::SONAR_HOST_URL is not set"; exit 1; } [[ -n "$SONAR_PROJECT_KEY" ]] || { echo "::error::SONAR_PROJECT_KEY is not set"; exit 1; } - name: Build and test run: | ./mvnw -B verify \ -Dtest='!PostgresIntegrationTests,!MySqlIntegrationTests' - name: SonarQube analysis env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} SONAR_PROJECT_KEY: ${{ secrets.SONAR_PROJECT_KEY }} run: | ./mvnw -B org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar \ -Dsonar.projectKey="${SONAR_PROJECT_KEY}" \ -Dsonar.host.url="${SONAR_HOST_URL}" \ -Dsonar.token="${SONAR_TOKEN}" \ -Dsonar.java.source=17 \ -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml - name: Quality Gate check env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} SONAR_PROJECT_KEY: ${{ secrets.SONAR_PROJECT_KEY }} run: | echo "Waiting for SonarQube to process the analysis..." for i in $(seq 1 24); do RESPONSE=$(curl -sf -u "${SONAR_TOKEN}:" \ "${SONAR_HOST_URL}/api/qualitygates/project_status?projectKey=${SONAR_PROJECT_KEY}" || true) STATUS=$(echo "$RESPONSE" | jq -r '.projectStatus.status' 2>/dev/null || echo "NONE") if [[ "$STATUS" =~ ^(OK|ERROR|WARN)$ ]]; then break; fi echo " Status: ${STATUS:-pending} — retrying in 5s..." sleep 5 done echo "Quality Gate status: $STATUS" [[ "$STATUS" != "ERROR" ]] || { echo "::error::Quality Gate FAILED"; exit 1; }