Gitlab integration with Sonar Cloud to analyze the Merge request

Hi I have GitLab repo with Gradle Java 17 configured with Sonar Cloud

  • ALM used (GitLab)
  • CI system used (GitLab)
  • Languages of the repository: Gradle Java 17

I am trying to analyze the Merge request, but facing issues with the Gitlab Pipeline
The pipeline fails and the merge request is not visible in Sonar cloud.

Can you please help me with this issue?

BR,
Ajinkya

With what error?

Hi @Colin

The pipeline fails with the error: Task ’ -Dsonar.organization=ads*****’ not found in root project ‘demo-sonar-test’ and its subprojects.

What Gradle command are you running? I’d suggest sharing your full GitLab CI YML file.

variables:
  SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
  GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
sonarcloud-check:
  tags:
    - cibuild
  image: gradle:jdk17
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
    - gradle sonar -Dsonar.token=$SONAR_TOKEN \
        -Dsonar.organization=ads***** \
        -Dsonar.projectKey=ads*****_demo-sonar-test \
        -Dsonar.host.url=$SONAR_HOST_URL \
        -Dsonar.verbose=true --info > log.txt
  artifacts:
    paths:
      - log.txt
    expire_in: 1 week
  only:
    - merge_requests
    - master
    - develop

I think you just need quotes around your strings.

> gradle sonar -Dsonar.token=$SONAR_TOKEN \
>         -Dsonar.organization="ads*****" \
>         -Dsonar.projectKey="ads*****_demo-sonar-test" \
>         -Dsonar.host.url=$SONAR_HOST_URL \
>         -Dsonar.verbose=true --info

I tried with quotes but still getting the same error

Task ’ -Dsonar.organization=ads*****’ not found in root project ‘ads*****_demo-sonar-test’

Hi @Colin ,

I have attached debug logs for your reference

log.txt (2.4 MB)

Hey there.

I think the issue is coming down to your multiline approach. I suggest, as a debugging step, removing the line breaks.

  script: 
    - gradle sonar -Dsonar.token=$SONAR_TOKEN -Dsonar.organization=ads***** -Dsonar.projectKey=ads*****_demo-sonar-test -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.verbose=true --info > log.txt

Hi @Colin ,

I tried without the line breaks still it fails I have attached the logs for your reference

BR,
Ajinkya
log 2.txt (3.4 MB)

The logs appear to show that the sonar task is running, but eventually, you hit a 403.

2024-10-14T10:17:28.700+0000 [DEBUG] [org.sonarqube.gradle.SonarTask] ← 403 https://sonarcloud.io/api/alm_integration/show_pullrequest?project=ads*****_demo-sonar-test&pullrequestKey=1 (292ms, unknown-length body)

Is your project bound, and is the GitLab token up to date in your global Administration > Organization settings?

Hi @Colin ,

The project is bound, I have attached a screenshot for your reference
SonarCloud

We created a user called cicd-bot@ads*****.com in Gitlab and created personal token for him with api scope and it is set in SonarCloud

one of my colleague can see the token as valid while for me it is showing invalid

I am not sure how is this possible

Please let me know how to proceed with this

P.S We followed this post

Best Regards,
Ajinkya

Hi @Colin ,

We were able to resolve the issue.

We made cicd-bot@ads*****.com the owner of organization in Gitlab and that resolved the issue.

Thanks for all the help and support

Best Regards,
Ajinkya

1 Like

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