First we need to create  basic pipeline can be created on GitLab. following the below link
How to create a basic CI/CD Pipeline on GitLab in Ubuntu 20.04 (thelinuxfaq.com)

STEP1: Goto sonarCloud login --> click on Import an organization from GitLab
then select import my personal GitLab group , paste your Personal Accesss Token (your GitLab Access Token)
click on continue , fill your import organization details and choose your plan.


STEP 2: create new project
 

A- Analyse with Gitlab CI/CD pipeline
 
1. Add environment variables  < go to repository setting --> select CI/CD --> Expand variable>
2. Click on add variable in the KEY Field enter SONAR_TOKEN
3. In the value field enter your generate token ex:5ga7ajs8465hgdyte889jjnnj
4. Make sure that the protect variable checkbox is unticked
5. Make sure that the mask variable checkbox is ticked
 
B. Define the sonarcloud URL environment variable 
 
1. Click on add variable in the key field enter SONAR_HOST_URL
2. In the value field enter https://sonarcloud.io
3. Make sure that the protect variable checkbox is unticked
4. Make sure that the mask variable checkbox is ticked



STEP 3 : update .gitlab-ci.yaml file 



Update your existing  .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:
  stage : sonar
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
 
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner 
  only:
    - merge_requests
    - master
    - develop


Marking line are sonar lines adding to the .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

stages: 
  - build 
  - test  
  - sonar

build:
  stage: build 
  script: 
    - echo "Building" 
    - mkdir build 
    - touch build/info.txt 
  artifacts: 
    paths: 
      - build/ 
test: 
  stage: test 
  script: 
    - echo "Testing" 
    - test -f "build/info.txt" 


sonarcloud-check:
  stage: sonar
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  cache: 
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner 
  only:
    - merge_requests
    - master
    - develop


STEP 4: . Crete a sonar-project.properties  file

Create a configuration file in the root directory of the project and name is sonar-project.properties
sonar.projectKey=sample -application
sonar.organization=sample
 
# This is the name and version displayed in the SonarCloud UI.
sonar.projectName=sample
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=.
 
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

view GitLab Properties file 

STEP 5: build Pipeline 
 view the Pipeline section 

Click a pipeline to open the Pipeline Details page and show the jobs that were run for that pipeline.


pipeline running sucessfully


 View the terminal result  Job succeeded.