简体   繁体   English

用gitlab ci运行sonarqube扫描仪

[英]Run sonarqube scanner with gitlab ci

I am trying to put together a CI environment for a .NET application using the following stack (just the relevant ones): 我正在尝试使用以下堆栈(仅相关的堆栈)为.NET应用程序组合CI环境:

  • Debian + mono Debian + mono
  • Docker 搬运工人
  • Gitlab CI Gitlab CI
  • Gitlab-multi-runner (as a docker container) Gitlab-multi-runner(作为docker容器)
  • Sonarqube + Postgre Sonarqube + Postgre

I've used docker-compose to create the container for sonarqube and postgre, both are running and working. 我已经使用docker-compose为sonarqube和postgre创建容器,两者都在运行和工作。 I am sadly stuck with executing sonarqube analysis for my build executed by the gitlab runner and all examples I found were using Maven. 我很遗憾地执行了由gitlab运行程序执行的构建的sonarqube分析以及我发现使用Maven的所有示例。 I've tried to use sonar-scanner as well, no luck so far. 我也试过使用声纳扫描仪,到目前为止没有运气。

Here are the contents of my gitlab-ci.yml: 以下是我的gitlab-ci.yml的内容:

image: mono:latest

cache:
  paths:
  - ./src/T_GitLabCi/packages/

stages:
  - build

.shared: &restriction
  only:
    - master
  tags:
    - docker

build:
  <<: *restriction
  stage: build
  script:
    - nuget restore ./src/T_GitLabCi
    - MONO_IOMAP=case xbuild /t:Build /p:Configuration="Release" /p:Platform="Any CPU" ./src/T_GitLabCi/T_GitLabCi.sln
    - mono ./tools/NUnitConsoleRunner/nunit3-console.exe ./src/T_GitLabCi/T_GitLabCi.sln --work=./src/T_GitLabCi/test --config=Release
    - << EXECUTE SONAR ANALYSIS >>

I am definitely missing something here. 我在这里肯定遗漏了一些东西。 Could somebody point me the right direction? 有人能指出正确的方向吗?

I have projects written in PHP but that shouldn't matter. 我有用PHP编写的项目,但这无关紧要。 Here's what I did. 这就是我做的。

  1. I enabled a private registry hosted on my GitLab installation 我启用了在我的GitLab安装上托管的私有注册表
  2. In this registry I have a "sonar-scanner" image built from this Dockerfile (it's based on one of the images available on Docker hub): 在这个注册表中,我有一个从这个Dockerfile构建的“声纳扫描器”图像(它基于Docker hub上可用的一个图像):

     FROM java:alpine ENV SONAR_SCANNER_VERSION 2.8 RUN apk add --no-cache wget && \\ wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-${SONAR_SCANNER_VERSION}.zip && \\ unzip sonar-scanner-${SONAR_SCANNER_VERSION} && \\ cd /usr/bin && ln -s /sonar-scanner-${SONAR_SCANNER_VERSION}/bin/sonar-scanner sonar-scanner && \\ apk del wget COPY files/sonar-scanner-run.sh /usr/bin 

and here's the files/sonar-scanner-run.sh file: 这是files/sonar-scanner-run.sh文件:

#!/bin/sh

URL="<YOUR SONARQUBE URL>"
USER="<SONARQUBE USER THAT CAN ACCESS THE PROJECTS>"
PASSWORD="<USER PASSWORD>"

if [ -z "$SONAR_PROJECT_KEY" ]; then
  echo "Undefined \"projectKey\"" && exit 1
else
  COMMAND="sonar-scanner -Dsonar.host.url=\"$URL\" -Dsonar.login=\"$USER\" -Dsonar.password=\"$PASSWORD\" -Dsonar.projectKey=\"$SONAR_PROJECT_KEY\""

  if [ ! -z "$SONAR_PROJECT_VERSION" ]; then
    COMMAND="$COMMAND -Dsonar.projectVersion=\"$SONAR_PROJECT_VERSION\""
  fi

  if [ ! -z "$SONAR_PROJECT_NAME" ]; then
    COMMAND="$COMMAND -Dsonar.projectName=\"$SONAR_PROJECT_NAME\""
  fi
  if [ ! -z $CI_BUILD_REF ]; then
    COMMAND="$COMMAND -Dsonar.gitlab.commit_sha=\"$CI_BUILD_REF\""
  fi
  if [ ! -z $CI_BUILD_REF_NAME ]; then
    COMMAND="$COMMAND -Dsonar.gitlab.ref_name=\"$CI_BUILD_REF_NAME\""
  fi
  if [ ! -z $SONAR_BRANCH ]; then
    COMMAND="$COMMAND -Dsonar.branch=\"$SONAR_BRANCH\""
  fi
  if [ ! -z $SONAR_ANALYSIS_MODE ]; then
    COMMAND="$COMMAND -Dsonar.analysis.mode=\"$SONAR_ANALYSIS_MODE\""
    if [ $SONAR_ANALYSIS_MODE="preview" ]; then
      COMMAND="$COMMAND -Dsonar.issuesReport.console.enable=true"
    fi
  fi

  eval $COMMAND
fi
  1. Now in my project in .gitlab-ci.yml I have something like this: 现在我在.gitlab-ci.yml项目中有这样的事情:

     SonarQube: image: <PATH TO YOUR IMAGE ON YOUR REGISTRY> variables: SONAR_PROJECT_KEY: "<YOUR PROJECT KEY>" SONAR_PROJECT_NAME: "$CI_PROJECT_NAME" SONAR_PROJECT_VERSION: "$CI_BUILD_ID" script: - /usr/bin/sonar-scanner-run.sh 

That't pretty much all. 这几乎不是全部。 The above example of .gitlab-ci.yml is simplified since I'm using diffrent builds for master and other branches (like when: manual ) and I use this plugin to get feedback in GitLab: https://gitlab.talanlabs.com/gabriel-allaigre/sonar-gitlab-plugin 上面的.gitlab-ci.yml是简化的,因为我正在为master和其他分支使用不同的构建(比如when: manual ),我使用这个插件在GitLab中获取反馈: https ://gitlab.talanlabs.com /加布里埃尔-allaigre /声纳-gitlab-插件

Feel free to ask if you have any questions. 如果您有任何疑问,请随时询问。 It took me some time to put this all together the way I want it :) Actually I'm still finetuning it. 我花了一些时间把它放在一起,就像我想要的那样:)实际上我还在调整它。

You need to install sonar-scanner first. 您需要先安装sonar-scanner You can find portage of sonar-scanner for almost any recent language, for example for npm you don't have to use directly the java executor: 你可以找到几乎任何最近语言的声纳扫描器的移植,例如对于npm你不必直接使用java执行器:

I only add to do this : 我只是添加这样做:

npm install --save sonar-scanner

Then I needed to add this in my package.json 然后我需要在package.json添加它

"scripts": {
  "sonar-scanner": "node_modules/sonar-scanner/bin/sonar-scanner"
}

This is my job in .gitlab-ci.yml : 这是我在.gitlab-ci.yml

job_testmaster:
  stage: test
  script:
    - PACKAGE_VERSION=$(node -p "require('./package.json').version")
    - echo sonar.projectVersion=${PACKAGE_VERSION} >> sonar-project.properties
    - npm run build
    - npm run sonar-scanner -- -Dsonar.login=${SONAR_LOGIN}
  only:
    - master
  tags:
    - docker

With this, I am able to start sonar analysis, but I am not able to use the quality gates after. 有了这个,我就能够开始进行声纳分析,但之后我无法使用质量门。

Hope this help. 希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM