Latest
Sonar 1 min read

Run SonarQube Analysis in GitHub Actions

Run SonarQube Analysis in GitHub Actions
Sonar · August 31, 2026

Version note: references describe SonarQube Server 2025.2 behaviour. Check your server, scanner and edition requirements. These examples were not run against a live SonarQube server.

What this guide covers

GitHub Actions can run tests and send a code-analysis report to SonarQube after a push. The job must be able to reach the server and must wait for the quality-gate result if it is used to block changes.

Before you start

You need a working project scan, a Node.js project with npm run test:coverage, and repository secrets named SONAR_HOST_URL and SONAR_TOKEN. The workflow below must also install SonarScanner CLI; it is not included automatically on GitHub runners.

Step 1: Prepare the project

Commit sonar-project.properties, including the project key, source paths and coverage-report path. Confirm the scan works from a trusted machine. Make the SonarQube HTTPS address reachable from the runner without disabling certificate checks.

Step 2: Create the workflow

Create .github/workflows/sonar.yml. The example runs only on pushes to main. Set SONAR_SCANNER_VERSION and SONAR_SCANNER_SHA256 as repository variables for a reviewed Linux x64 scanner ZIP, using the official release checksum.

name: Sonar quality
on:
  push:
    branches: [main]
permissions:
  contents: read
jobs:
  quality:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v4
        with:
          node-version: '22'
          cache: npm
      - run: npm ci
      - run: npm run test:coverage
      - run: test -s coverage/lcov.info
      - name: Install SonarScanner CLI
        env:
          VERSION: ${{ vars.SONAR_SCANNER_VERSION }}
          SHA256: ${{ vars.SONAR_SCANNER_SHA256 }}
        run: |
          set -eu
          : "${VERSION:?Set SONAR_SCANNER_VERSION}"
          : "${SHA256:?Set SONAR_SCANNER_SHA256}"
          curl --fail --location --proto '=https' --tlsv1.2 \
            -o scanner.zip "https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${VERSION}-linux-x64.zip"
          printf '%s  scanner.zip\n' "$SHA256" | sha256sum --check
          unzip -q scanner.zip -d "$RUNNER_TEMP/sonar-scanner"
          echo "$RUNNER_TEMP/sonar-scanner/sonar-scanner-${VERSION}-linux-x64/bin" >> "$GITHUB_PATH"
      - name: Analyze and wait for the quality gate
        run: sonar-scanner -Dsonar.qualitygate.wait=true
        env:
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

Step 3: Read the job result

Push a trusted change. Open the Actions job and check that tests pass, the LCOV report exists and scanner installation succeeds before analysis starts. The token is supplied only to the analysis step. Review the actions and pin approved full commit SHAs for production workflows.

Step 4: Require the check when needed

If this job must block a merge, configure the corresponding required check in repository rules and add an appropriate trusted pull-request workflow. Pull-request analysis depends on SonarQube edition. Do not expose the token to untrusted pull-request code.

Check the result

The Actions job and SonarQube project should show the same change and gate result. Check DNS, network access and certificate trust from the runner if a local scan works but the hosted job fails.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Test and Publish a Docker Image with GitHub Actions

Docker September 1, 2026 1 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.