Adding analysis to GitHub Actions workflow
Adding SonarQube analysis to your GitHub Actions workflow.
Once you have created your project in SonarQube Community Build, you can add the SonarQube Community Build analysis to your GitHub Actions workflow:
Configure the project analysis parameters.
Add the analysis to your GitHub Actions workflows.
Commit and push your code to start the analysis.
Considerations about upgrading to GitHub Action v6
When updating to SonarQube Scan GitHub action v6
, you might have to update your workflow to change how arguments are quoted because the args
input is parsed differently. See this release note for more information.
Considerations about upgrading to GitHub Action v5
v3.1.0
and below of the GitHub Action are based on Docker: at every execution of the action, a dedicated docker container is spawned.
The advantage of using container are primarily:
Isolation, since the SonarScanner gets only access to the directory where the project is checked out.
Full control of the environment where the SonarScanner is executed, in terms of required utilities such as
wget
andkeytool
.
The use of Docker comes, however, with multiple disadvantages regarding SonarQube analysis:
Issues with analyzers requiring access to a system-level directory, such as cache of dependencies in Java or Dart.
Issues with DockerHub rate limit on peak workload scenarios.
Requirement by GitHub to run as root user.
Support for Docker-based actions limited to Linux - no support of Windows nor MacOS.
v5
doesn't have the Docker dependency, making the action composite. The action now runs in the environment of the runner executing the GitHub workflow.
Prerequisites
Configuring the project analysis parameters
For general information, see Analysis parameters and the respective SonarScanner section: SonarScanner for Maven, SonarScanner for Gradle, Using the scanner, SonarScanner CLI, and Configuring the scanner.
The setting of sonar.token
and sonar.host.url
is specific to GitHub Actions: With GitHub Actions, you can configure these parameters in GitHub. This may be done at the global level, or at the project level by the Project Administrator as explained below. It makes sense to store the server URL at the global level.
Storing the authentication token in GitHub for your project
The authentication token used in GitHub Actions workflows should be securely stored in a GitHub secret: see GitHub’s documentation on Encrypted secrets for more information.
Proceed as follows
In the SonarQube Community Build UI, generate a SonarQube Community Build token for your project.
Create a repository secret in GitHub with:
Name: SONAR_TOKEN
Value: the token you generated in the previous step.
Storing the SonarQube Server URL in GitHub for your project
Create an organization variable in GitHub with:
Name: SONAR_HOST_URL
Value: SonarQube Server URL
Configuring the build.yml file
This section shows you how to configure your .github/workflows/build.yml
file.
SonarQube Community Build doesn’t support multiple branches, so you should only analyze your main branch. You can restrict analysis to your main branch by setting it as the only branch in your on.push.branches
configuration in your workflow YAML file, and not using on.pull_request
.
Click the scanner you’re using below to expand the example configuration:
The errors "Missing blame information…" and "Could not find ref…" can be caused by checking out with a partial or shallow clone, or when using Git submodules. You should disable git shallow clone to make sure the scanner has access to all of your history when running analysis with GitHub Actions.
For more information, see the GitHub Actions Checkout README.
Failing the workflow when the quality gate fails
You can use the SonarQube quality gate check GitHub Action to ensure your code meets your quality standards by failing your workflow when your quality gate fails.
If you do not want to use the SonarQube Community Build quality gate Check Action, you can instruct the scanner to wait for the SonarQube Community Build quality gate status at the end of the analysis. To enable this, pass the -Dsonar.qualitygate.wait=true
parameter to the scanner in the workflow YAML file.
This will make the analysis step poll SonarQube Community Build regularly until the quality gate is computed. This will increase your workflow duration. Note that, if the quality gate is red, this will make the analysis step fail, even if the actual analysis itself is successful. We advise only using this parameter when necessary (for example, to block a deployment workflow if the quality gate is red).
You can set the sonar.qualitygate.timeout
property to an amount of time (in seconds) that the scanner should wait for a report to be processed. The default is 300 seconds.
Last updated
Was this helpful?