How do you integrate SonarQube with GitHub Actions?

IntermediateHands-onSonarQube
Answer

Short answer

Store SONAR_TOKEN and SONAR_HOST_URL as repository secrets, run the official SonarQube scan action after tests (so coverage exists), and add the quality gate action to fail the workflow when the gate fails.

Workflow

name: ci
on: [push, pull_request]
jobs:
  sonar:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }                 # full history for blame / new-code detection
      - uses: actions/setup-node@v4
        with: { node-version: 22, cache: npm }
      - run: npm ci && npm test -- --coverage
      - uses: SonarSource/sonarqube-scan-action@v4
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
      - uses: SonarSource/sonarqube-quality-gate-action@v1
        timeout-minutes: 5
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

For Maven projects, replace the scan action with mvn verify sonar:sonar.

Pull request decoration

Developer Edition+ (and SonarCloud) can comment results directly on the PR and analyse PR branches separately.

What to say in the interview

"Tests with coverage first, then the SonarQube scan action with token and URL from secrets, then the quality-gate action to fail the job. fetch-depth 0 helps SonarQube detect new code accurately."

All Security Tools questions Roadmap: SonarQube — code quality & SAST

More on SonarQube