问题
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"]
回答1:
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
来源:https://stackoverflow.com/questions/56251725/launching-sonar-scanner-from-a-gitlab-docker-runner