2

I have a CI workflow that integrates a linting job and then a code quality job. My Linting job is a docker runner launching my eslint script from the application code. Then my code quality job is supposed to start a sonar scanner docker instance, check my code and send the reports back to my sonarqube instance.

The problem is mainly with the fact that i can't launch correctly the sonar scanner with either solutions which are :

Sonar Scanner Docker https://github.com/newtmitch/docker-sonar-scanner
At this point, the runner runs the image but when starting its script (which is only sonar-scanner (with potential arguments) i get this error response :

sonar scanner unrecognized option -c

which i don't understand and have no control over since its an already made docker image pulled from the docker hub

Sonar Scanner installation from scratch in a docker container
Here what i do is installing sonar scanner by downloading it in the container like so:

Dockerfile

FROM java:alpine  
ENV SONAR_SCANNER_VERSION 3.3.0.1492

RUN apk add --no-cache wget && \  
    wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${SONAR_SCANNER_VERSION}-linux.zip && \  
    unzip sonar-scanner-cli-${SONAR_SCANNER_VERSION}-linux && \  
    cd /usr/bin && ln -s /sonar-scanner-cli-${SONAR_SCANNER_VERSION}-linux/bin/sonar-scanner sonar-scanner && \  
    apk del wget

COPY sonar-scanner-run.sh /usr/bin
RUN ["chmod", "+x", "/usr/bin/sonar-scanner-run.sh"]

Here I add wget to be able to download files, then I download the latest version of sonar-scanner from the link found on their official documentation. I then unzip it and then create a symlink to the binary file so that I can execute the script from anywhere. I finally clear the wget cache copy my shell script that will be executed from the gitlab-ci.yml and run a chmod command to bypass any permission problems.

sonar-scanner-run.sh

URL="https://mysonarqubeserver"
USER="myusertoken"
SONAR_PROJECT_KEY="myprojectkey"


COMMAND="sonar-scanner -Dsonar.host.url=\"$URL\" -Dsonar.login=\"$USER\" -Dsonar.projectKey=\"$SONAR_PROJECT_KEY\""

eval $COMMAND

the environment variables are all given by sonarqube after you create a project.

Here I have what I think is a "Linux Problem" where my symlink is not created since I get this error code in my gitlab ci logs :

Unkown command sonar-scanner

EDIT The symlink now works (problem was that the unziped folder name wasn't correct) but another message pops off. The sonar scanner actually works now here is the error:

INFO: ------------- Run sensors on module mytherapy
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=121ms
INFO: Sensor JavaSquidSensor [java]
INFO: Configured Java source version (sonar.java.source): none
INFO: JavaClasspath initialization
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 14.285s
ERROR: Error during SonarQube Scanner execution
INFO: Final Memory: 25M/284M
INFO: ------------------------------------------------------------------------
ERROR: Please provide compiled classes of your project with sonar.java.binaries property

My project is a react-native, therefore javascript project. I don't understand why it is requiring java compiled classes

Here is my gitlab-ci.yml file in case a problem might be from here:

gitlab.ci.yml

cache:
  paths:
  - node_modules/

stages:
  - analysis
  - test

lint:
  stage: analysis
  image: "node:latest"  
  script: npm i && npm run lint
  tags: ["nodejs"]

code quality:
  stage: analysis
  image: <My image from the registry>
  script: 
    - /usr/bin/sonar-scanner-run.sh
pass tests:
  stage: test
  image: "node:latest"
  script: npm i && npm run test
  tags: ["nodejs"]
Jaro
  • 1,587
  • 5
  • 20
  • 39
  • Maybe add a `ls -l` after the symlinking part, to check if everything is there. – bellackn May 22 '19 at 08:10
  • alright I fixed the problem but now there is another issue, i'll submit it in the question edit. – Jaro May 22 '19 at 08:40
  • 2
    `COMMAND="sonar-scanner -Dsonar.host.url=\"$URL\" -Dsonar.login=\"$USER\" -Dsonar.projectKey=\"$SONAR_PROJECT_KEY\"" eval $COMMAND` - anything wrong with just running the command? Add `set -x` to the script and see if you escaped it properly. Just run it as it is `sonar-scanner -Dsonar.host.url="$URL" -Dsonar.login="$USER" -Dsonar.projectKey="$SONAR_PROJECT_KEY"`... – KamilCuk May 22 '19 at 08:49
  • Problem solved. Sonar scanner binary was using embedded java files that were obselete. I made a new docker image based on openjdk:latest image, and modified the sonar scanner binaries so that it doesn't use the embedded files anymore. All will be in the edit for further infos. – Jaro May 22 '19 at 09:44
  • @Biffen My bad didn't actually realise i could answer my own question will do it. – Jaro May 22 '19 at 11:20

1 Answers1

0

After further investigations i can say that i made a working docker image for sonar scanner that can work with gitlab ci.

DOCKERFILE

FROM openjdk:8

LABEL maintainer="Aria Groult <aria.groult@outlook.fr>"

RUN apt-get update
RUN apt-get install -y curl git tmux htop maven sudo

# Install Node - allows for scanning of Typescript
RUN curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
RUN sudo apt-get install -y nodejs build-essential

WORKDIR /usr/src

RUN curl --insecure -o ./sonarscanner.zip -L https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.3.778-linux.zip && \
    unzip sonarscanner.zip && \
    rm sonarscanner.zip && \
    mv sonar-scanner-3.0.3.778-linux /usr/lib/sonar-scanner && \
  ln -s /usr/lib/sonar-scanner/bin/sonar-scanner /usr/local/bin/sonar-scanner

ENV SONAR_RUNNER_HOME=/usr/lib/sonar-scanner
COPY sonar-scanner-run.sh /usr/bin
RUN ["chmod", "+x", "/usr/bin/sonar-scanner-run.sh"]

You might get problems with the embedded JRE in sonar-scanner. If it happens, modify the binary by setting: useembeddedjava to false.

gitlab-ci.yml & sonar-scanner-run.sh are unchanged

sonar-project.properties

sonar.projectKey=projectkey
sonar.projectName=projectname
sonar.sourceEncoding=UTF-8
sonar.exclusions=node_modules/**,coverage/**
sonar.sources=./components
sonar.gitlab.project_id=linkToGit
sonar.host.url=hosturl
sonar.login=sonarqubeloginkey
sonar.exclusions=test/**, node_modules/**

It is important to specify that node_modules are excluded in a nodejs project since they include some java files that will create some disfonctionment in the sonar-scanner process. In general only include un-generated files in the sonar-scanner file list

Jaro
  • 1,587
  • 5
  • 20
  • 39
  • Mr. jaro,My Dockerfile has builded and pushed it to a private docker hub. When I commit code to gitlab, gitlab runner failed,because pull image failed... Failed to pull image with policy "always": Error response from daemon: read tcp 192.168.10.242:44888->192.168.10.242:15000: read: connection reset by peer (manager.go:203:0s) how can i do? – 君主不是你 Mar 03 '23 at 10:38