Scan not pushing correct branch names and PR details

Running the scan with GitHub - SonarSource/sonarqube-scan-action does not detect the correct branch name / pull request.

My workflow looks like the following:

name: Lint, Test, & Scan

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - ready_for_review
  push:
    tags:
      - v*
    branches:
      - main

  lint:
    # Details not relevant...
  test:
    # Details not relevant...

  scan:
    name: scan
    needs: [ lint, test ]
    runs-on: ubuntu-latest
    steps:
      # Checkout code
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      # Download test report
      - name: Download test report
        uses: actions/download-artifact@v2
        with:
          name: test-report
          path: ./reports/
      # Download lint report
      - name: Download lint report
        uses: actions/download-artifact@v2
        with:
          name: lint-report
          path: ./reports/
      # Scan with SonarQube
      - name: Run SonarQube scanner
        uses: sonarsource/sonarqube-scan-action@master
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

All the steps run successfully but the scan results are always pushed to the master branch in SonarQube, ignoring completely the actual details from GitHub.

Shouldn’t this action be able to detect the branch name, the PR details, and process that info accordingly? If not, how do you suggest that I fix this?

I’m using SonarQube Developer Edition, version 8.7.1.42226.

Anyone able to help on this topic?

Hi Eduardo,

Yes, the scanner should auto configure branches and pull request analysis based on environment variables present in github builds. Specifically, GITHUB_ACTIONS, GITHUB_REF, GITHUB_HEAD_REF and GITHUB_BASE_REF.
Do you have the logs of the scanner, preferrably with debug enabled? Can you confirm if those variables are present when the action runs?

The env vars in the workflow when I’m about to analyse a branch:

  echo $GITHUB_ACTIONS
  echo $GITHUB_REF
  echo $GITHUB_HEAD_REF
  echo $GITHUB_BASE_REF
> true
> refs/heads/release-v1
> 
> 

And the gist for sonarqube action with debug enabled is here.

That looks correct for a branch and the SonarQube scanner should be setting the branch name correctly.
Could you please provide the logs of the scanner with debug enabled?

I believe the Gist provided has that. This is how the step is setup:

      - name: Run SonarQube scanner
        uses: sonarsource/sonarqube-scan-action@master
        with:
          args: >
            -Dsonar.verbose=true
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

Am I missing something?

Thanks, I missed that.

The auto detection of branches and PRs in Github actions was only introduced in v8.8: https://jira.sonarsource.com/browse/SONAR-14591.
You can either upgrade or script the generation of the scanner parameters based on the environment variables.

Awesome! I’ve Upgraded SonarQube to 8.9.1 and I can confirm that all worked out of the box.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.