Github Actions
Configure an analysis of your SonarQube Cloud project using GitHub Actions.
To configure an analysis of your project using GitHub Actions, you will use the SonarQube Scan GitHub Action.
Prerequisites
From SonarQube Scan GitHub Action version 5.0.0 (sonarqube-scan-action):
If your runner is GitHub-hosted, all required utilities should be already provided by default.
If your runner is self-hosted, you need to ensure that the following utilities are installed and available in the
PATH:unzip,wgetorcurl,gpganddirmngr.
SonarQube Scan GitHub Action update notes
Analysis setup overview
You should follow the in-product tutorial when creating a new project. When it’s time to Choose your Analysis Method during setup, select With GitHub Actions. You can also access the tutorials for an existing project by going to Your Project > Administration > Analysis Method.
The tutorial will walk you through the precise steps to set up the analysis but the basic steps are these:
Define the
SONAR_TOKENenvironment variable in your repository by setting up a GitHub Secret. TheSONAR_TOKENidentifies and authenticates you to SonarQube Cloud. The tutorial will provide the precise value for your specific account. To generate the token, see:From the Team plan: Scoped Organization Tokens.
With the Free plan: Managing personal access tokens.
Set the parameters used to connect to the instance and identify the project. See:
The tutorial will be populated with the correct values for your specific account. The parameters are set differently depending on your project type:
In the
pom.xmlfor Java Maven projects.In the
build.gradlefile for Java Gradle projects.In the SonarScanner command line for .NET projects.
In the
sonar-project.propertiesfile for other types of projects.
You can also add additional analysis parameters to further specify your analysis details. For more information about analysis parameters setup, see Configuration overview.
Set up your workflow file that defines the steps of your build. In addition to the usual steps that build your project, you need to invoke the SonarScanner to perform the analysis of your code. For more information, see below.
Setting up your workflow file
This section shows you how to configure your .github/workflows/build.yml file.
GitHub Actions can build specific branches and pull requests if you use on.push.branches and on.pull-requests configurations as shown in the examples below.
In the tabs below, click the scanner you’re using to expand the example configuration:
For Maven projects: SonarScanner for Maven
For Gradle projects: SonarScanner for Gradle
For .NET projects: SonarScanner for .NET
For other projects: SonarScanner CLI
Write the following in your workflow YAML file.
Activate the SonarScanner for Gradle in your build. See Activate the scanner in your build.
Write the following in your workflow YAML file.
Write the following in your workflow YAML file.
You can easily set up a basic configuration using the SonarQube Scan GitHub action, for all languages, including C, C++, Objective-C, and Dart.
You’ll find the GitHub Actions and configuration instructions page on the GitHub Marketplace.
For C, C++, and Objective-C projects relying on Build Wrapper to generate the compilation database (see the CFamily Prerequisites page), use the sonarqube-scan-action/install-build-wrapper sub-action to install the Build Wrapper.
Preventing pull request merges when the quality gate fails
Failing the workflow when the quality gate fails
You fail a GitHub Actions workflow on quality-gate failure by making the scanner wait for the Quality Gate result and exit non-zero if it’s failed.
To do so, use the sonar.qualitygate.wait analysis parameter (optionally with sonar.qualitygate.timeout). For more information about these parameters, see Quality gate.
Analyzing Monorepo Projects: Build Configuration
The example below shows how you could set up a yml file for multiple projects in a monorepo. If you want to analyze a monorepo that contains more than one project ensure that you specify the paths to each sub-project for analysis in your build file.
To ensure that your monorepo works as expected, you need to build each project in the monorepo separately with a unique project key for each one.
GitHub Actions .yml file
Analyzing pull requests from forked repositories
GitHub doesn't expose repository secrets to workflows triggered by a pull request from a fork. Because your SONAR_TOKEN is a secret, a single combined build-and-analyze workflow can't run on fork pull requests.
Automatic Analysis supports fork pull requests without any extra setup, but doesn't produce coverage data. If you need CI-based analysis with coverage on fork pull requests, split the work across three workflows: one for regular (non-fork) pull requests, one that builds fork code without exposing secrets, and one that runs the Sonar analysis with secrets but never executes fork-provided code.
Checking out a fork's code in a workflow that has access to secrets is a sensitive pattern, even when that code is never executed. Validate every value you pull from the fork's pull request before using it, and keep the analysis job's permissions as narrow as possible.
Workflow 1: build and analyze non-fork pull requests
This workflow behaves like a standard CI workflow. Restrict it to non-fork pull requests, and analyze directly since the build runs in a trusted context:
Workflow 2: build fork pull requests without secrets
This workflow builds and tests fork pull requests with no secret access, then uploads the build output and the PR metadata the next workflow needs as artifacts. Interpolating untrusted values (branch names, ref names) directly into a run: shell command is a script-injection risk. Instead, pass PR metadata through an env: block:
Workflow 3: run the Sonar analysis for fork pull requests
This workflow triggers on workflow_run once workflow 2 finishes, so it runs in the base repository's trusted context with secrets available. It downloads the build output and PR metadata, checks out the fork's code without executing it, and runs the scan:
sonar.scm.revision is what makes the pull request check and decoration appear — without it, the analysis runs but SonarQube Cloud can't match it to the pull request.
Security considerations
Pass any value that comes from the fork's pull request (branch names, ref names, the head SHA) through an
env:block, never by interpolating it directly into arun:shell command.Validate extracted values (the head SHA, head branch, PR number, base ref) with a regex before using them in the checkout or scan steps.
Don't quote the scan action's
args:values that reference${{ }}expressions. Quoting can change how the action parses individual arguments.Keep the analysis workflow's permissions as narrow as the steps above require, and never run fork-provided build commands (like
mvn verify) directly in this workflow.
For a full working example, see this community thread and the referenced example repository workflow files.
Managing certificates for the SonarQube Cloud scan GitHub Action
If you use the sonarqube-scan-action for your GitHub Action and SonarQube Cloud is behind a secured proxy with certificates that need to be recognized by the GitHub runner, you’ll need to set the SONAR_ROOT_CERT environment variable in GitHub.
Troubleshooting
Scanner cannot resolve file paths in test coverage report
When using GitHub Action, the SonarScanner fails to resolve the paths within the test coverage report and raises the warning "Could not resolve <n> file paths in <file>".
You may resolve this problem by switching off relative_paths=True in the coverage settings.
"Container action is only supported on Linux" error
You may encounter this error if you use the SonarQube Scan GitHub Action before version 4, i.e. sonarcloud-github-action. This action is based on Docker and is only supported on Linux runners. In that case, move to sonarqube-scan-action (see Prerequisites).
"Container action is only supported on Linux" error
You may encounter this error if you use the SonarQube Scan GitHub Action before version 4, i.e. sonarcloud-github-action. This action is based on Docker and is only supported on Linux runners. In that case, move to sonarqube-scan-action (see Preqrequisites above).
"The job was not started because recent account payments have failed" error
You may encounter this GitHub error if your GitHub options are set to use a GitHub-hosted runner instead of your self-hosted runner. In this case, we recommend checking your GitHub options to ensure your self-hosted runner is selected.
"Failed to import SonarSource public key from all keyservers."
You may encounter this error if dirmngr is not installed in the runner. It is confirmed by those logs:
Ensure that dirmngr is installed on the runner, or disable signature verification by setting skipSignatureVerification: true on your pipeline (not recommended).
Related pages
Related online courses
Last updated
Was this helpful?

