From 4fc832fea52c771cd66bfbd0600994d9afe1b92b Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Mon, 30 Nov 2026 07:00:30 +0100 Subject: [PATCH 1/2] ci(sonar): only what goes into master is analysed The Free plan of SonarQube Cloud analyses the main branch and the pull requests into it. The pull requests into staging only queued Sonar runs that took the runners for hours without a result, and the scan of the branch staging (#522) needs branch analysis, a paid feature. - build.yml (the compilation database for the analysis) runs for pull requests into master only, so sonar.yml isn't started for the others and posts no status on their commits - sonar.yml: a push is analysed only on master; the scan of staging is gone --- .github/workflows/build.yml | 6 ++++++ .github/workflows/sonar.yml | 25 +++---------------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9a2d3aee5..6a9c24bff 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,11 +5,17 @@ name: Build +# Only what goes into master is analysed: the Free plan of SonarQube Cloud analyses the main branch and the pull +# requests into it, not other branches nor the pull requests into them (staging). A pull request into another branch +# doesn't run this workflow at all, so sonar.yml isn't started for it and posts no status on its commit. A pull +# request moved to master later is analysed from its next push. on: push: branches: - master pull_request: + branches: + - master types: [ opened, reopened, synchronize ] permissions: {} diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 58e852370..adbef1119 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -141,14 +141,14 @@ jobs: if [ "$event" != "pull_request" ]; then # A push: the branch comes from the trusted event, not from the artifact. Only - # master and staging are pushed to with a build (build.yml); anything else is refused - # rather than analysed as a branch of its own. + # master is pushed to with a build (build.yml); anything else is refused rather than + # analysed as a branch of its own (branch analysis isn't in the Free plan). if [ "$RUN_EVENT" != "push" ] || [ "$RUN_REPO" != "$REPO" ]; then echo "::error::branch build that isn't a push to this repository ($RUN_EVENT, $RUN_REPO)" exit 1 fi case "$RUN_BRANCH" in - master|staging) echo "branch=$RUN_BRANCH" >> "$GITHUB_OUTPUT" ;; + master) echo "branch=$RUN_BRANCH" >> "$GITHUB_OUTPUT" ;; *) echo "::error::no analysis for a push to $RUN_BRANCH"; exit 1 ;; esac echo "Branch build of $RUN_BRANCH, no PR parameters." @@ -218,25 +218,6 @@ jobs: --define sonar.qualitygate.wait=true --define sonar.scm.revision=${{ github.event.workflow_run.head_sha }} - # staging as a branch of its own next to master, so its issues and its quality gate show - # before staging is merged into master. SonarQube Cloud keeps it as a long-lived branch - # only if its name matches the project's long-lived branch pattern (Administration > - # Branches and Pull Requests); otherwise it is a short-lived one, deleted when inactive. - - name: SonarQube Cloud scan (staging) - if: steps.meta.outputs.event != 'pull_request' && steps.meta.outputs.branch == 'staging' - uses: SonarSource/sonarqube-scan-action@fd88b7d7ccbaefd23d8f36f73b59db7a3d246602 # v6.0.0 - env: - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - SONAR_HOST_URL: https://sonarcloud.io - with: - args: > - --define sonar.cfamily.compile-commands=${{ env.BUILD_WRAPPER_OUT_DIR }}/compile_commands.json - --define sonar.cfamily.taintAnalysis=false - --define sonar.exclusions=${{ env.SONAR_EXCLUSIONS }} - --define sonar.qualitygate.wait=true - --define sonar.scm.revision=${{ github.event.workflow_run.head_sha }} - --define sonar.branch.name=staging - - name: Report success if: success() env: From 4187171859078566e921b3d72ffd5446215ebd9f Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Tue, 1 Dec 2026 17:11:58 +0100 Subject: [PATCH 2/2] ci(sonar): analyse every pull request again, one analysis per pull request at a time The premise of the previous commit was wrong: pull requests into staging are analysed and get a quality gate (#566 today). The run of #558 that gave no result was cancelled in the queue, which is about an hour per analysis with many pull requests at once. - build.yml runs for pull requests into any branch again, as on staging - sonar.yml: a concurrency group per pull request with cancel-in-progress, so a newer commit's Build stops the analysis of the older one. workflow_run.pull_requests is empty for a pull request from a fork, so the fork and its branch name the group then. A push to master gets a group of its own commit and isn't cancelled - sonar.yml: timeout-minutes 120 on Analyze, and an error status on the commit when the analysis is cancelled, so it doesn't stay "Analysis in progress" - the scan of staging stays removed: build.yml pushes only on master, so it could never run --- .github/workflows/build.yml | 6 ------ .github/workflows/sonar.yml | 30 +++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6a9c24bff..9a2d3aee5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,17 +5,11 @@ name: Build -# Only what goes into master is analysed: the Free plan of SonarQube Cloud analyses the main branch and the pull -# requests into it, not other branches nor the pull requests into them (staging). A pull request into another branch -# doesn't run this workflow at all, so sonar.yml isn't started for it and posts no status on its commit. A pull -# request moved to master later is analysed from its next push. on: push: branches: - master pull_request: - branches: - - master types: [ opened, reopened, synchronize ] permissions: {} diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index adbef1119..2e8f83add 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -13,6 +13,18 @@ on: workflows: [ "Build" ] types: [ requested, completed ] +# One analysis per pull request at a time: a newer commit's Build (its "requested" event already) cancels the +# analysis of the older commit, which would otherwise hold a runner for about an hour for a result nobody reads. +# workflow_run.pull_requests is empty for a pull request from a fork, so the fork and its branch name the group then. +# A push to master gets a group of its own commit and is never cancelled, as in build.yml. +concurrency: + group: >- + ${{ github.workflow }}-${{ github.event.workflow_run.event == 'pull_request' + && format('pr-{0}', github.event.workflow_run.pull_requests[0].number + || format('{0}:{1}', github.event.workflow_run.head_repository.full_name, github.event.workflow_run.head_branch)) + || github.event.workflow_run.head_sha }} + cancel-in-progress: ${{ github.event.workflow_run.event == 'pull_request' }} + # Default to nothing; each job opts in to exactly what it needs. permissions: {} @@ -63,6 +75,9 @@ jobs: name: Analyze if: github.event.action == 'completed' && github.event.workflow_run.conclusion == 'success' runs-on: ubuntu-24.04 + # an analysis takes about an hour; one stuck in the scan or in the wait for the quality gate is stopped + # rather than holding the runner for the default 6 hours + timeout-minutes: 120 permissions: contents: read # fetch trusted config actions: read # download artifact from the Build run @@ -142,7 +157,7 @@ jobs: if [ "$event" != "pull_request" ]; then # A push: the branch comes from the trusted event, not from the artifact. Only # master is pushed to with a build (build.yml); anything else is refused rather than - # analysed as a branch of its own (branch analysis isn't in the Free plan). + # analysed as a branch of its own. if [ "$RUN_EVENT" != "push" ] || [ "$RUN_REPO" != "$REPO" ]; then echo "::error::branch build that isn't a push to this repository ($RUN_EVENT, $RUN_REPO)" exit 1 @@ -239,3 +254,16 @@ jobs: -f context="SonarQube Cloud" \ -f description="Quality gate failed or analysis errored" \ -f target_url="$RUN_URL" + + # cancelled by a newer commit of the pull request (concurrency above), or stopped by timeout-minutes: + # without this the commit would keep "Analysis in progress" for good + - name: Report cancellation + if: cancelled() + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh api -X POST "repos/${{ github.repository }}/statuses/$SHA" \ + -f state=error \ + -f context="SonarQube Cloud" \ + -f description="Analysis cancelled: a newer commit, or over the time limit" \ + -f target_url="$RUN_URL"