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.
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 for .NET, the SonarScanner CLI, or the Configuring the scanner for NPM pages.
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?

