Question on : Analyzing new project - Unable to configure

ERROR: Error during SonarScanner execution 2ERROR: Could not find a default branch to fall back on. in the pipeline logs.

In Sonarcloud I also see this : " “master" branch has not been analyzed yet and you have multiple branches already. It looks like it is not your Main Branch, check your configuration.”

I’m getting the following error when running pipeline in Bitbucket:
“”

This is how my bitbucket-pipeline.yml looks like:

definitions:
 steps:
   - step: &sast
      name: Static Code Scanning & Unit Testing
      image: mcr.microsoft.com/dotnet/core/sdk:2.1-focal
      script:
      - apt update && apt install -yqqq default-jre-headless
      - export PATH=$PATH:$HOME/.dotnet/tools
      - dotnet tool install --global dotnet-sonarscanner
      - cp NuGet.Config.build NuGet.Config && export NUGET_PROVIDER_URI=${NUGET_PROVIDER_READ_URI}
      - dotnet sonarscanner begin /k:"${BITBUCKET_REPO_OWNER}_${BITBUCKET_REPO_SLUG}" /o:"${BITBUCKET_REPO_OWNER}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.login="${SONAR_TOKEN}"
      - dotnet build ${PROJECT_NAME}.sln
      - dotnet test ${PROJECT_NAME}.sln
      - dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"
   - step: &unit-test
      name: Unit Testing
      image: mcr.microsoft.com/dotnet/core/sdk:2.1-focal
      script:
      - cp NuGet.Config.build NuGet.Config && export NUGET_PROVIDER_URI=${NUGET_PROVIDER_READ_URI}
      - dotnet build ${PROJECT_NAME}.sln
      - dotnet test ${PROJECT_NAME}.sln
   - step: &build
      name: Build Container for Deploy
      trigger: automatic
      image:
       name: 568014775437.dkr.ecr.us-east-1.amazonaws.com/my-company/container-deployment:latest
       aws:
        access-key: $DEPLOYMENT_ACCESS_KEY_ID
        secret-key: $DEPLOYMENT_SECRET_ACCESS_KEY
      services:
       - docker
      script:
      # ECR Login
      - eval $(aws ecr get-login --region ${AWS_DEFAULT_REGION} --no-include-email)
      - echo "=========== Building Container ==========="
      - export AWS_REGISTRY_URL=${AWS_REGISTRY_URL_BASE}/$BITBUCKET_REPO_SLUG
      - if [[ ! -z $BITBUCKET_BRANCH ]]; then export BRANCH_ID=$(echo "$BITBUCKET_BRANCH" | tr -dc '[:alnum:]\n\r-_' | tr '/*' '_'| tr '[:upper:]' '[:lower:]'); fi && echo $BRANCH_ID
      - export COMMIT_HASH_SHORT=$(echo "$BITBUCKET_COMMIT" | head -c 7 )
      - export VERSION=v`xmllint --xpath 'string(/Project/PropertyGroup/Version)' ${PROJECT_NAME}/${PROJECT_NAME}.csproj` && echo $VERSION
      - if [[ ! -z ${BITBUCKET_TAG} ]] && [[ ${BITBUCKET_TAG} =~ $VERSION ]]; then export BUILD_ID=${VERSION}; else export BUILD_ID=${COMMIT_HASH_SHORT}; fi && echo $BUILD_ID
      - docker build --build-arg NUGET_PROVIDER_URI=$NUGET_PROVIDER_READ_URI --build-arg PROJECT_NAME=$PROJECT_NAME -t ${AWS_REGISTRY_URL}:$BUILD_ID -f ${PROJECT_NAME}/Dockerfile .
      - docker push ${AWS_REGISTRY_URL}:${BUILD_ID}
      - if [[ ! -z $BRANCH_ID ]]; then docker tag ${AWS_REGISTRY_URL}:${BUILD_ID} ${AWS_REGISTRY_URL}:${BRANCH_ID}; fi
      - if [[ ! -z $BRANCH_ID ]]; then docker push ${AWS_REGISTRY_URL}:${BRANCH_ID}; fi
   - step: &deploy
      name: Deploy Container to Dev
      deployment: Dev
      trigger: manual
      image:
       name: 568014775437.dkr.ecr.us-east-1.amazonaws.com/my-company/container-deployment:latest
       aws:
        access-key: $DEPLOYMENT_ACCESS_KEY_ID
        secret-key: $DEPLOYMENT_SECRET_ACCESS_KEY
      services:
       - docker
      script:
      # ECR Login
      - echo "=========== Deploying Container ==========="
      - export AWS_ACCESS_KEY_ID=${DEPLOYMENT_ACCESS_KEY_ID}
      - export AWS_SECRET_ACCESS_KEY=${DEPLOYMENT_SECRET_ACCESS_KEY}
      - export AWS_REGISTRY_URL=${AWS_REGISTRY_URL_BASE}/$BITBUCKET_REPO_SLUG
      - export BRANCH_ID=$(git branch --show-current | tr -dc '[:alnum:]\n\r-_' | tr '/*' '_'| tr '[:upper:]' '[:lower:]') && echo $BRANCH_ID
      - export COMMIT_HASH_SHORT=$(echo "$BITBUCKET_COMMIT" | head -c 7 )
      - export VERSION=v`xmllint --xpath 'string(/Project/PropertyGroup/Version)' ${PROJECT_NAME}/${PROJECT_NAME}.csproj`
      - if [[ ${BRANCH_ID} =~ "rel-" ]]; then export BUILD_ID=${VERSION}; else export BUILD_ID=${COMMIT_HASH_SHORT}; fi
      - aws eks update-kubeconfig --name ${EKS_CLUSTER}
      - kubectl -n ${NAMESPACE} set image $RESOURCE/${BITBUCKET_REPO_SLUG} ${BITBUCKET_REPO_SLUG}=${AWS_REGISTRY_URL}:${BUILD_ID}
   - step: &tag-check
      name: Check for existing release version
      image: alpine:latest
      script:
      - if [[ $BITBUCKET_BRANCH =~ "rel-" ]] && [[ $BITBUCKET_PR_DESTINATION_BRANCH == "master" ]]; then exit 0; fi
      - apk add git libxml2-utils
     # Get version from csproj file
      - export VERSION=v`xmllint --xpath 'string(/Project/PropertyGroup/Version)' ${PROJECT_NAME}/${PROJECT_NAME}.csproj`
      - echo "Version ${VERSION}"
     # Check to see if tag already exists in repo; if it does, error out.
      - export TAG_EXISTS=`git tag -l ${VERSION} | wc -l`
      - if [[ $TAG_EXISTS -gt 0 ]]; then exit 1; fi
   - step: &tag-release
      name: Tag version for release
      image: alpine:latest
      script:
      - apk add git libxml2-utils
     # set tag
      - export VERSION=v$(xmllint --xpath 'string(/Project/PropertyGroup/Version)' ${PROJECT_NAME}/${PROJECT_NAME}.csproj)
     # Check if tag exists
      - export GIT_TAG_COMMIT=$(git show-ref --tags -d | grep ${VERSION}\\^\{\} | awk '{print $1}')
      - echo "Tag commit is $GIT_TAG_COMMIT"
      - echo "Current commit is $BITBUCKET_COMMIT"
     # Bypass tag setting if the tag exists and is linked to the current commit. This allows for
     # PRs to merge from release branches->master without breaking things
      - if [[ $BITBUCKET_COMMIT == $GIT_TAG_COMMIT ]]; then exit 0; fi
      - echo "Setting Tag $VERSION"
      - git tag -a $VERSION -m "master merge auto-tagging" && git push --tags
   - step: &update-deployment-dashboard
      name: Update Deployment Dashboard Tag
      image: alpine:latest
      deployment: Test
      script:
        - echo "Setting deployed commit for environment"
   - step: &no-op
      script:
        - echo "PR from release branch requires no action"
 services:
   docker:
     memory: 2048
pipelines:
  pull-requests:
    '**':
      - step: *tag-check
      - step: *sast
    rel-*:
      - step: *no-op
  custom:
    set-deployed-test:
      - step: *update-deployment-dashboard
    set-deployed-stg:
      - step:
          <<: *update-deployment-dashboard
          deployment: Staging
    set-deployed-prod:
      - step:
          <<: *update-deployment-dashboard
          deployment: Production
  branches:
    feature-*:
      - step: *unit-test
      - step: *build
      - step:
          <<: *deploy
          trigger: manual
    rel-*:
      - step: *tag-release
  tags:
    v[0-9]*:
      - step:
          <<: *build
          trigger: automatic
          after-script:
              - if [ $BITBUCKET_EXIT_CODE == 1 ]; then git push origin --delete $VERSION; fi

Extra info:

Main branch is called “master” in my company.

ANY possible solution for this ?

Hey there.

  - dotnet sonarscanner begin /k:"${BITBUCKET_REPO_OWNER}_${BITBUCKET_REPO_SLUG}" /o:"${BITBUCKET_REPO_OWNER}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.login="${SONAR_TOKEN}"

I have two questions:

  • Do you already have a project in your organization matching exactly the values generated for your project key (/k)?
  • Is this pipeline running against your master branch? I don’t see it in the list of branches defined for this pipeline
pipelines:
  pull-requests:
    '**':
      - step: *tag-check
      - step: *sast
    rel-*:
      - step: *no-op
  custom:
    set-deployed-test:
      - step: *update-deployment-dashboard
    set-deployed-stg:
      - step:
          <<: *update-deployment-dashboard
          deployment: Staging
    set-deployed-prod:
      - step:
          <<: *update-deployment-dashboard
          deployment: Production
  branches:
    feature-*:
      - step: *unit-test
      - step: *build
      - step:
          <<: *deploy
          trigger: manual
    rel-*:
      - step: *tag-release