I’m trying to run sonar-scanner as part of my build pipeline but even though incl. it as a step in my pipeline that depends on my test job to generate the coverage report. I always runs as a separate pipeline that highjacks my MR.
sonarcloud-check:
stage: build
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
dependencies:
- "test"
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
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
script:
- sonar-scanner
rules:
- if: $CI_COMMIT_REF_NAME == 'main' || $CI_PIPELINE_SOURCE == 'merge_request_event'
It seems to be an issue with Gitlab CI, for some reason when adding the example sonarcloud job to my build pipeline I get 2 pipelines running for my MR one for the MR and one for the branch.
I solved this by add workflow rules to my pipeline definition and removing the constraints provided by the example.
For example:
test:
...
sonarcloud-check:
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
needs:
- test
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
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
script:
- sonar-scanner
rules:
- if: $CI_COMMIT_REF_NAME == 'main' || $CI_PIPELINE_SOURCE == 'merge_request_event'
becomes:
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH == 'master'
test:
...
sonarcloud-check:
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
needs:
- test
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
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
script:
- sonar-scanner