Adding analysis to GitHub Actions workflow
Adding SonarQube analysis to your GitHub Actions workflow.
Once you have create your project in SonarQube Server, you can add the SonarQube Server 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.
If you use a monorepo, see the If you use a monorepo section for more details.
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.
Specific to GitHub Actions is the setting of sonar.token
and sonar.host.url
: With GitHub Actions, you can configure these parameters in GitHub. This may be done at the global level by the system administrator, or at the project level by the Project Administrator as explained below . It makes sense to store the server URL at the global level.
In addition, starting from the Developer Edition, SonarScanners running in GitHub Actions can automatically detect branches and pull requests being built so you don’t need to specifically pass them as parameters to the scanner. See Introduction to branch analysis and Introduction to pull request analysis for more information.
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.
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.
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.
Preventing pull request merges when the quality gate fails
In GitHub, you can block pull requests from being merged if it is failing the quality gate. To do this:
In GitHub, go to your repository Settings > Branches > Branch protection rules and select either the Add rule or Edit button if you already have a rule on the branch you wish to protect.
Complete the Branch protection rule form:
Define the Branch name pattern (the name of the branch you wish to protect)
Select Require status checks to pass before merging to open supplementary form fields.
In the Search for status checks in the last week for this repository field, select Require branches to be up to date before merging, then find
SonarQube Code Analysis
and add it to the list of required checks.
Failing the workflow when the quality gate fails
You can use the SonarQube Server 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 Server quality gate Check Action, you can instruct the scanner to wait for the SonarQube Server 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 Server 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). It should not be used to report the quality gate status in a pull request, as this is already done with pull request decoration.
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.
If you use a monorepo
The monorepo feature is supported starting in the Enterprise Edition provided the GitHub integration with SonarQube Server has been properly set up. See Setting up integration at global level for more details.
To add the SonarQube Server analysis to your monorepo workflow:
Step 1: Configure the analysis parameters for each project
For each project in the monorepo, set the analysis parameters: See Configuring the project analysis parameters above. Specific to the monorepo set up is the setting of the sonar.token
property explained below.
You must create the Sonar tokens used to authenticate to the SonarQube Server during the analysis of the monorepo projects and store them securely in GitHub secrets. You can either use one single global-level token for the monorepo or a project-level token for each project in the monorepo.
Proceed as follows:
Generate the token in SonarQube Server:
For project tokens, create a token for each project you need the Administer permission on the project. Go to the Security page of your SonarQube Server account and create a Project analysis token.
For a global token, ask your administrator. The procedure is similar but you need the global Administer system permission.
In your GitHub repository, go to Settings > Secrets.
Select New repository secret.
In the Name field:
If you use a global token: enter
SONAR_TOKEN
.Otherwise: enter
SONAR_TOKEN_1
(or another unique identifier within the monorepo) for the token of your first project in the monorepo.
In the Value field, enter the corresponding token value.
Select Add secret.
If you use project-level tokens, repeat steps 3 to 6 for each additional project in the monorepo.
Step 2: Configure the build.yml file of the monorepo
In the build.yml
file of your monorepo:
Define the paths to the projects.
Add a job for each project in the monorepo.
See the file example below.
name: Build
on:
push:
branches:
- master # main branch name
paths:
- 'PROJECT1_PATH/**' # monorepo projects paths from the monorepo root directory
- 'PROJECT2_PATH/**'
pull_request:
types: [opened, synchronize, reopened]
jobs:
sonarQubeScan1:
name: sonarQubeScan1
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
- name: Cache SonarQube packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: SonarQube Scan 1
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN_1 }} # analysis token associated to your project
SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }}
run: |
cd PROJECT1_PATH/
mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=SONAR_PROJECT1_KEY -Dsonar.projectName='SONAR_PROJECT1_NAME'
# Replace variables with project path, key and name
sonarQubeScan2:
name: sonarQubeScan2
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
- name: Cache SonarQube packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: SonarQube Scan 2
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN_2 }} # analysis token associated to your project
SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }}
run: run: |
cd PROJECT2_PATH/
mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=SONAR_PROJECT2_KEY -Dsonar.projectName='SONAR_PROJECT2_NAME'
# Replace variables with project path, key and name
# Add other scan jobs if you wish to scan more projects in the monorepo
Last updated
Was this helpful?