LoginSignup
0
4

More than 5 years have passed since last update.

sonarQubeを使ったソースコード解析手順

Posted at

はじめに

sonarQubeを使ったソースコード解析の環境構築手順を残します。
以下の環境を使って構築しました。

環境 Version
OS macOS Mojave 10.14.3
Docker 18.09.2

sonarQubeはDBの構築も必要なアプリケーションだったため、直接インストールせず
docker-composeを使って、複数のコンテナ上に載せました。
docker-composeのymlファイルはこちらを使わせていただきました。

なぜ

なぜソースコード解析か?

技術的負債の返済の必要性について開発者だけでなく、ビジネスも含めた関係者間で認識を合わせる必要がありました。
そのためにはビジネスとしての言語に翻訳する、つまり定量的な指標に落とし込むことが必要であり、そのための解析ツールを探していた次第です。

なぜsonarQubeか?

当初Code ClimateというSaaSのソースコード解析を利用しようとしたのですが
外部サーバに置かれることに関して社内でNGとなり、ローカルで確認可能な解析ツールを探したところ、sonarQubeを見つけました。

なぜDockerか?

上記のとおりビジネス側と早急に言語を合わせる必要があるため、お手軽に立ち上げ可能なDockerを選定しました。

手順

Dockerはすでにインストールされているものとして記載します。

1.事前準備

docker composeを実行するための以下のように作業フォルダおよびファイルを作成します。

.
├── containers
│   └── datastore
│       └── Dockerfile
└── docker-compose.yml

2.docker-compose用のymlファイル、Dockerイメージファイルを作成

docker-compose.yml
version: "2"

services:
  sonarqube:
    image: sonarqube
    ports:
      - "9000:9000"
    networks:
      - sonarnet
    environment:
      - sonar.jdbc.url=jdbc:postgresql://db:5432/sonar
    volumes:
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions

  db:
    image: postgres
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      # This needs explicit mapping due to https://github.com/docker-library/postgres/blob/4e48e3228a30763913ece952c611e5e9b95c8759/Dockerfile.template#L52
      - postgresql_data:/var/lib/postgresql/data

networks:
  sonarnet:
    driver: bridge

volumes:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_extensions:
  postgresql:
  postgresql_data:

Dockerfileは以下のようになります。

FROM busybox:latest

MAINTAINER takashi.iwasaki <takashi.iwasaki@example.com>

# sonarqube
VOLUME /opt/sonarqube/conf
VOLUME /opt/sonarqube/data
VOLUME /opt/sonarqube/extensions
VOLUME /opt/sonarqube/lib/bundled-plugins
ADD sonar.properties /opt/sonarqube/conf/sonar.properties
# postgresql
VOLUME /var/lib/postgresql
VOLUME /var/lib/postgresql/data

CMD /bin/sh

3.docker-compose起動

$ docker-compose up

4.sonarQubeログイン

http://localhost:9000にアクセスし、以下のID、パスワードでログインします。

項目
ID admin
Password admin

5.解析前準備

 (1)「Create New Project」で任意のプロジェクト名を入力、「SetUp」
 (2)sonarQubeを利用するためのトークンを発行するために「Generate」

6.sonar scannerのインストール

sonar scannerをインストールします。
インストールしたzipファイルを解凍し、以下のディレクトリ構成で格納します。

.
├── containers
│   └── datastore
│       └── Dockerfile
├── docker-compose.yml
└── sonar-scanner-3.3.0.1492-macosx
    ├── bin
    │   ├── sonar-scanner
    │   └── sonar-scanner-debug
    └── conf
        └── sonar-scanner.properties

7.sonar scanner実行のための準備

プロパティファイルを以下のように作成します。

#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here

#----- Default SonarQube server
#sonar.host.url=http://localhost:9000

#----- Default source code encoding
#sonar.sourceEncoding=UTF-8

#----- Default SonarQube server
sonar.host.url=http://localhost:9000

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- postgres
sonar.jdbc.url=jdbc:postgresql://localhost/sonar

# Project identification
sonar.projectName=projectName # 5で登録したprojectName
sonar.projectKey=projectKey   # 5で登録したprojectKey
sonar.projectVersion=1.0

# Info required for Sonar
sonar.sources=project  # ソースコード解析したいプロジェクト

# Comma-separated paths to directories with sources (required)
sonar.language=java

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

8.sonar scanner実行

$ bin/sonar-scanner

今後

今回はあるタイミングのスナップショットでの解析となりましたが、
今後は開発プロセスに組み込み、Pull Request単位での解析を行いコードレビューを自動化あるいは平準化したいと思います。

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4