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."
More on SonarQube
What is SonarQube and how do you integrate it into a pipeline?BeginnerWhat does SonarQube analyse?BeginnerHow do you run SonarQube locally with Docker?BeginnerHow do you scan a project with sonar-scanner?BeginnerHow do you run SonarQube analysis for a Maven project?BeginnerWhat is a quality gate in SonarQube?Beginner