STEP 1 : go to the Sonar page < https://sonarcloud.io/ > and click
Sonarcloud
sonar cloud integrate with gitlab
1. 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
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
2. update .gitlab-ci.yaml file
Update your .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
- deploy
build_project:
stage: build
script:
- echo "this is sample project, $GITLAB_USER_LOGIN!"
testing1:
stage: test
script:
- echo "This job test for user login sucessfully"
testing2:
stage: test
script:
- echo "This job tests userlog, but takes more time than test-job1."
- echo "After the echo commands complete, it runs the sleep command for 5 seconds"
- sleep 5
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
deploy-prod:
stage: deploy
script:
- echo "This job deploys for some usr files $CI_COMMIT_BRANCH branch."
3. 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
3. You should see the page refresh itself in a few moments with your analysis results if the pipeline runs successfully.
Comments (0)