Run sonarqube scanner with gitlab ci

前端 未结 2 1575
猫巷女王i
猫巷女王i 2021-02-04 20:30

I am trying to put together a CI environment for a .NET application using the following stack (just the relevant ones):

  • Debian + mono
  • Docker
  • Gitl
相关标签:
2条回答
  • 2021-02-04 21:15

    I have projects written in PHP but that shouldn't matter. Here's what I did.

    1. I enabled a private registry hosted on my GitLab installation
    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):

      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:

    #!/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:

      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

    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.

    0 讨论(0)
  • 2021-02-04 21:23

    You need to install sonar-scanner first. 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:

    I only add to do this :

    npm install --save sonar-scanner
    

    Then I needed to add this in my package.json

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

    This is my job in .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.

    0 讨论(0)
提交回复
热议问题