diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 58e852370..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 @@ -141,14 +156,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. 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 +233,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: @@ -258,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"