Could not find ref 'main' in refs/heads, refs/remotes/upstream or refs/remotes/origin

Template for a good new topic, formatted with Markdown:

  • ALM used - Bitbucket Cloud
  • CI system used - Jenkins
  • Scanner command used when applicable - see below
  • Languages of the repository - C#, JavaScript, T-SQL
  • Error observed - see below
  • Steps to reproduce - see below
  • Potential workaround - none?

I’m seeing this error in our PR scans:

Could not find ref ‘main’ in refs/heads, refs/remotes/upstream or refs/remotes/origin. You may see unexpected issues and changes. Please make sure to fetch this ref before pull request analysis.

I found a topic somewhere (on this forum, I believe) that suggested to issue a git fetch on the base branch before scanning, so since I was having some SonarCloud issues this morning (other thread), I decided to also tackle this issue. Easy enough to add a git fetch. See below my new pipeline step (git fetch is added in my latest PR):

        stage('SonarScanner Start PR Scan') {
            when { changeRequest() }
            steps {
                bat "git fetch origin ${CHANGE_TARGET}"
                bat "dotnet sonarscanner begin /k:\"${SONAR_PROJECT}\" /o:\"myowner\" /s:\"${WORKSPACE}\\SonarQube.Analysis.xml\" \
                        / \
                        /d:sonar.login=\"${SONAR_TOKEN}\" \
                        /d:sonar.pullrequest.key=${CHANGE_ID} \
                        /d:sonar.pullrequest.branch=${CHANGE_BRANCH} \
                        /d:sonar.pullrequest.base=${CHANGE_TARGET} \
                        /d:sonar.pullrequest.provider=bitbucketcloud \
                        /d:sonar.pullrequest.bitbucketcloud.owner=${REPO_OWNER} \
                        /d:sonar.pullrequest.bitbucketcloud.repository=${REPO_NAME} \

Jenkins log for this step below:

E:\jenkins_build_nodes\build01\workspace\PR-124bf85f2af>git fetch origin main 
 * branch                  main       -> FETCH_HEAD
[Pipeline] bat
Warning: A secret was passed to "bat" using Groovy String interpolation, which is insecure.
		 Affected argument(s) used the following variable(s): [SONAR_TOKEN]
		 See for details.

E:\jenkins_build_nodes\build01\workspace\PR-124bf85f2af>dotnet sonarscanner begin /k:"mykey" /o:"myowner" /s:"E:\jenkins_build_nodes\build01\workspace\PR-124bf85f2af\SonarQube.Analysis.xml"                          /d:sonar.verbose=true /                         /d:sonar.login="****"                         /d:sonar.pullrequest.key=124                         /d:sonar.pullrequest.branch=feature/sonar-fetch-base-branch                         /d:sonar.pullrequest.base=main                         /d:sonar.pullrequest.provider=bitbucketcloud                         /d:sonar.pullrequest.bitbucketcloud.owner=myowner                         /d:sonar.pullrequest.bitbucketcloud.repository=myrepo                         /d:sonar.cs.dotcover.reportsPaths=coverage.html 
SonarScanner for MSBuild 5.7.2
Using the .NET Core version of the Scanner for MSBuild
Loading analysis properties from E:\jenkins_build_nodes\build01\workspace\PR-124bf85f2af\SonarQube.Analysis.xml
sonar.verbose=true was specified - setting the log verbosity to 'Debug'
Pre-processing started.
Preparing working directories...
Using environment variables to determine the download directory...

… yet if I look at the scan for this PR, I still see the error!

Any thoughts?

I think I got it - my syntax was slightly off it seems. Should have been:

bat "git fetch origin ${CHANGE_TARGET}:refs/remotes/origin/${CHANGE_TARGET}"

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