SonarScanner CLI Docker in GitHub Actions

When using SonarQube’s SonarScanner CLI in GitHub Actions it may not always be possible to use the official public action, perhaps your organisation doesn’t permit public actions, or only approves specific ones.

In such cases you may look to create your own internal action and SonarQube provide their SonarScanner CLI tool in a Docker image for easy consumption. The documentation gives you an example on how to create a container using their image:

docker run \
    --rm \
    -e SONAR_HOST_URL="http://${SONARQUBE_URL}" \
    -e SONAR_SCANNER_OPTS="-Dsonar.projectKey=${YOUR_PROJECT_KEY}" \
    -e SONAR_TOKEN="myAuthenticationToken" \
    -v "${YOUR_REPO}:/usr/src" \

I tried this in a GitHub Action workflow and all initially seemed fine, the code scan completed and analysis was successfully pushed to SonarQube and results returned to the workflow. Upon checking it in the UI though, I noticed that the results had been pushed to the main branch and there were no signs of SonarQube having knowledge of any branches in the GitHub repository.

After much troubleshooting, I eventually tracked this down to auto detection of branch existence and configuration not taking place by the SonarScanner CLI in the workflow. I was able to compare a scan using the Docker image vs a scan with the official GitHub Action, which uses the same Docker image under the hood. With the official Action, something similar to the following will appear in the logs:

INFO: Load project branches
INFO: Load project branches (done) | time=22ms
INFO: Load branch configuration
INFO: Detected branch/PR in 'GitHub Action'
INFO: Auto-configuring branch 'test-branch-1'
INFO: Load branch configuration (done) | time=5ms

This was not occuring in the scan with the Docker image, instead something similar to this would be observed:

INFO: Load project branches
INFO: Load project branches (done) | time=31ms
INFO: Load branch configuration
INFO: Load branch configuration (done) | time=7ms

I discovered that it was possible to manually set the branch name by supplying, however this was less than optimal, possibly wouldn’t work for PR requests and I wanted to get to the bottom of why auto detection was not occuring.

I found this SonarQube community post which put me on the track that the SonarScanner CLI was looking for built in CI environment variables and they need to be supplied to the docker run command. The post was about GitLab, not GitHub, but I figured something similar would be needed. I noticed that when the Docker container in the official Action was run, the following GitHub environment variables were supplied to it, i.e. pretty much all of them!


I tried this in my workflow and branch auto detection returned. By process of elimination and lots of workflow runs I was able to get this down to a more manageable list which appear to be the required variables and formed the end result:

docker run \
    --rm \
    -v $GITHUB_WORKSPACE:/usr/src \
    -e SONAR_HOST_URL="${{ inputs.sonar_host_url }}" \
    -e SONAR_TOKEN="${{ inputs.sonar_token }}" \
    -Dsonar.projectKey=${{ inputs.project_key }} " \

Hopefully this might get added to the official documentation some day, although based on a SonarQube rep’s comment it seems perhaps not likely: