Github Actions "Project was never analyzed" issue

While trying to use Github actions and SonarCloud i’ve been hitting an issue with the first analysis of a project. I cooked up a small example to isolate the issue.

The project and workflow live in https://github.com/hbfernandes/my-app where the workflow is defined like this:

name: Java CI
on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - name: Build with Maven
      run: mvn -B package
    - name: Analyse with Sonar
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
      run: mvn -B sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=hbfernandes

I’ve created an organization is SonarCloud from my github account called “hbfernandes” which is completely empty. When the above workflow runs i get the following:

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.7.0.1746:sonar (default-cli) on project my-app: Project was never analyzed. A regular analysis is required before a branch analysis. -> [Help 1]

Full execution here https://github.com/hbfernandes/my-app/commit/8e4455a6ac86bd712cd44bad1a19bd5feef03ce1/checks?check_suite_id=334961355

If i run the same maven command locally i get a successful analysis posted so i’m guessing there is some property being filled by the the fact it’s running on Github actions. Is there a workaround for this, some property i can use perhaps?

I’ve tried using sonar.branch.name and sonar.branch.target to “master” but it made no difference.

Welcome to the community forum and thank you for providing a clear description!

Correct, we use the variables provided by Github to determine if we should run a pullrequest analysis or a regular branch analysis. I see that you are running the GitHub action in the context of a pullrequest. To run pullrequest analysis we have to be able to compare the changes to an already existing analysis of the target branch. That is the error you are seeing.

However there should not be any error anymore after you ran the scanner on the ‘master’ branch locally.

Setting the sonar.branch.name parameter to master should trigger a regular analysis of the master branch. Can you share the output of the command when you set that parameter?

Thanks,
Tom

Thank you for your reply!

I am confused when you say i’m running this on the context of a pull request. This is on master and the trigger was a push to master, there are no pull requests for this repo so how can it be ran in the context of a pull request?

I’ve added again the parameter you’ve asked, here is the full build output https://github.com/hbfernandes/my-app/commit/4189f9e33f9bbe5da443d7de26adcc004d3b7f61/checks?check_suite_id=335030457

If there is more information i can provide let me know.

Hello,

Sorry for that, you are right.

Anyway when branch analysis is triggered, we already expect there to be an analysis present. Running in github actions will automatically set the sonar.branch.name and will thus always trigger branch analysis.

This should however not be the case, I created a ticket for that here where you can follow the progress.

Thanks for the feedback!
Tom

Thanks a lot @TomVanBraband! I’ll follow that ticket.