I can’t find an example of a working pullrequest decorator with gitlab?
I’ve a c# project which runs the sonarscanner inside a docker container build which is triggered from the gitlab pipelines
All example I found are basically for github and it seems like its workinug there automatically?
unfortunately not.
I can see the branch under branches but pull requests is 0
Maybe it has to do with the build strategy. I integrated sonarcloud by running the sonarscanner cli in a docker build file (which also builds the whole application)
RUN dotnet sonarscanner begin \
/k:"$SONAR_PROJECT_KEY" \
/o:"$SONAR_OGRANIZAION_KEY" \
/d:sonar.host.url="$SONAR_HOST_URL" \
/d:sonar.login="$SONAR_TOKEN" \
/d:sonar.branch.name="$BRANCH_NAME" \
/d:sonar.coverageReportPaths="/sonarqubecoverage/SonarQube.xml" \
/d:sonar.dotnet.excludeTestProjects=true \
/v:"$APP_VERSION"
//THEN Build, Test
RUN dotnet sonarscanner end /d:sonar.login="$SONAR_TOKEN"
my gitlab script looks like this
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
- notify
variables:
DOCKER_BUILDKIT: 1
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
# Building app for dev server
build:
stage: build
image: docker:20.10
services:
- docker:20.10-dind
script:
- cd src
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build --build-arg BRANCH_NAME=$CI_COMMIT_BRANCH --cache-from $CI_REGISTRY/myCompany/myImage:latest-dev -t $CI_REGISTRY/myCompany/myImage:latest-dev -t $CI_REGISTRY/myCompany/myImage:$CI_PIPELINE_IID-dev .
This is probably the issue. When you manually configure branch analysis, we flip auto-detection for branches/merge requests off. This means when you run a Merge Request build (I assume you run a build/analysis on each MR), the “wrong” kind of analysis is being performed.
I would suggest removing this, and relying on the presence of these environment variables being available in your scanner environment.
I changed it this way. But now it doesn’t recognize the branch when I push to a branch. I can see that
CI_COMMIT_REF_NAME contains the correct name.
When I set /d:sonar.branch.name="$CI_COMMIT_REF_NAME" manually the branchname is resolved correctly. So I assume that the env vars are set as expected. Do I have to do anything so they get collected?
I added the envs as args, passed them from the gitlab build and set them then as envs