Analyze new project via bitbucket pipelines

I’d like to find the way to analyze new sonar project automatically via pipeline without manually create from the UI, is it possible ?

current configuration:

  • SONAR_TOKEN is valid → generate and check from security page
  • ALM used Bitbucket Cloud
  • CI system used Bitbucket Cloud
  • Scanner command used
    - step: &scan
        image: node:20
        name: Build, test and analyze on SonarCloud
        size: 2x
        caches:
          - node
          - sonar
        script:
          - npm ci
          - npm run build
          - pipe: sonarsource/sonarcloud-scan:4.1.0
            variables:
              SONAR_PROJECT_KEY: $BITBUCKET_REPO_SLUG
              EXTRA_ARGS: [
                '-Dsonar.exclusions=node_modules/**,**/tests/**,**/*.spec.ts,**/*.test.ts',
                '-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info',
                '-Dsonar.typescript.tsconfigPath=tsconfig.json',
                '-Dsonar.javascript.node.maxspace=7168',
                '-Dsonar.organization=xxxxx'
              ]
              SONAR_SCANNER_OPTS: "-Xmx7168m"
        
  • Languages of the repository typescript
  • Error
08:40:22.993 INFO  Load/download plugins (done) | time=1439ms
08:40:23.487 INFO  Found an active CI vendor: 'Bitbucket Pipelines'
08:40:23.635 INFO  No project and organization key detected from Bitbucket Cloud Pipelines.
08:40:23.639 INFO  Load project settings for component key: 'xxxxx'
08:40:23.781 INFO  Process project properties
08:40:23.782 INFO  Execute project builders
08:40:23.783 INFO  Execute project builders (done) | time=0ms
08:40:23.785 INFO  Project key: xxxxx
08:40:23.785 INFO  Base dir: /opt/atlassian/pipelines/agent/build
08:40:23.786 INFO  Working dir: /opt/atlassian/pipelines/agent/build/.scannerwork
08:40:23.793 INFO  Load project branches
08:40:23.984 INFO  Load project branches (done) | time=191ms
08:40:23.987 INFO  Check ALM binding of project 'xxxxx'
08:40:24.125 INFO  Detected project binding: NONEXISTENT
08:40:24.126 INFO  Check ALM binding of project 'xxxxx' (done) | time=139ms
08:40:24.129 INFO  Load project pull requests
08:40:24.269 INFO  Load project pull requests (done) | time=140ms
08:40:24.273 INFO  Load branch configuration
08:40:24.275 INFO  Detected analysis for branch 'main'
08:40:24.277 INFO  Auto-configuring branch main
08:40:24.315 ERROR Could not find a default branch for project with key 'xxxxx'. Make sure project exists.
08:40:24.648 INFO  EXECUTION FAILURE
08:40:24.651 INFO  Total time: 12.385s

Hey there.

Does the user whose token you are providing for analysis have the Create Proejcts permission?

Yes, the token is generated from Admin account (with all permissions)

Thanks @Thanat_Maneenut and sorry for the delayed response.

There’s an odd quirk of SonarQube Cloud where, if the first analysis of a project that you intend to provision via analysis contains sonar.branch.name, the project provisioning fails with the error you mentioned. This is unavoidable in situations where sonar.branch.name is picked up automatically based on environment variables.

08:40:24.315 ERROR Could not find a default branch for project with key 'xxxxx'. Make sure project exists.

This is problem that was solved a while ago on SonarQube Server (SONAR-11155), but never made it to SonarQube Cloud.

I’ve flagged this for attention from our PMs, who are looking at ways to help users onboard organizations more easily.

In the meantime, creating the project first from the UI, or using Web APIs to provision the project before analysis ( POST api/projects/create and POST api/project_branches/rename).

Hi @Thanat_Maneenut

Thank you for your valuable feedback.

I have added this to our backlog of opportunities to consider for improvements.

Best,

Joan