지속적인 코드 품질(Continuous Code Quality) 관리 도구인 소나큐브(SonarQube) 사용법

sonarQube 6.2 기준


설치

  1. 다운로드

    wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.2.zip
    CODE
  2. 압축 해제

    unzip -l sonarqube-6.2.zip
    cd sonarqube-6.2
    CODE
  3. DBMS 계정 생성(Ex: MySQL 데이타베이스와 사용자 계정 생성하기)

    CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_bin;  
    GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonarPwd';
    CODE
  4. 설정 편집

     vi conf/sonar.properties
    CODE

    #위에서 생성한 계정 정보 입력

    sonar.jdbc.username=sonar
    sonar.jdbc.password=sonarPwd


    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance


    ## 앞에 웹서버를 reverse proxy 로 사용할 경우 아래 context 는 우회하도록 설정 필요

    sonar.web.context=/sonarqube

    ## PHP-FPM 사용시 포트 조정. 9001 은 sonarQube 내부의 elastic search 가 사용하는 포트이므로 주의

    sonar.web.port=9003


  5. context 를 설정했을 경우 http 와 연결 설정 (Securing the Server Behind a Proxy 참고)

    nginx https 설정 예

     location /sonarqube {
            proxy_pass   http://127.0.0.1:9003;
            proxy_set_header        Host                    $host;
            proxy_set_header        X-Real-IP               $remote_addr;
            proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Proto       $scheme;
     }
    CODE
  6. 실행

    ./bin/linux-x86-64/sonar.sh start
    CODE
  7. 로그를 통해 정상 실행 여부 확인

    tail -f logs/*
    CODE
  8. 브라우저로 연결한 후에 초기 계정인 admin/admin 으로 연결
  9. My Account → Security 에 들어가서 초기 암호 설정
     

설정

프로젝트 생성

  1. AdministrationProjectManagement 클릭
  2. 우측의 Create Project 클릭
  3. 프로젝트 정보를 입력. Branch 에는 형상 관리의 브랜치 이름 설정하며 NameKey 를 기억해 둘 것

  4. Create 클릭
  5. 생성된 프로젝트 정보에서 key 를 복사


Language plugin 설치

코드 품질을 측정할 수 있도록 언어별 플러그인 설치

  1. Administration → System → Update Center 클릭

  2. Available 을 클릭한 후에 목록에서 분석할 언어별 플러그인을 설치

sonar scanner 설치

  1. sonar scanner 다운로드 페이지에서 클릭해서 받거나 하단의 명령어 입력

    wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
    CODE
  2. 압축을 해제하고 bin 폴더를 PATH 에 등록

    export PATH=$PATH:/usr/local/sonar-scanner-2.8/bin
    CODE
  3. sonar-scanner 의 conf 폴더의 sonar-project.properties 수정.

    sonar-scanner/conf/sonar-scanner.properties

    #----- Default SonarQube server
    # sonarQube 를 / 가 아닌 별도의 context 로 설정했을 경우 localhost 대신 Full URL 로 설정 필요(Ex: https://qa.example.com/sonarqube)
    #sonar.host.url=http://localhost:9000
    
    #----- Default source code encoding
    #sonar.sourceEncoding=UTF-8
    BASH
  4. 검사할 소스 코드 체크아웃

  5. 소스 코드의 root 폴더에 sonar-project.properties  를 만들고 여기에 위에서 생성한 프로젝트 고유의 설정(key, name등) 입력

    # must be unique in a given SonarQube instance
    sonar.projectKey=avengers:awesome-project:develop
    
    # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
    sonar.projectName=My new project
    sonar.projectVersion=1.0
    
    
    # Encoding of the source code. Default is default system encoding
    sonar.sourceEncoding=UTF-8
    CODE
  6. 스캐너 구동. sonar-project.properties  를 덮어쓸 경우 -Dkey=name 형식으로 지정

    sonar-scanner  -Dsonar.projectKey=my:proj -Dsonar.sources=.
    BASH

참고