LoginSignup
1
0

DockerのSonarQubeでTypeScriptの静的解析をローカル環境で行ったメモ

Last updated at Posted at 2024-06-17

概要

SonarQubeを使ってTypeScriptをローカルで静的解析してみた。
Windows上でDocker越しSonarQubeを起動して、ローカルソースをコード検証するの手順を基本的に踏襲する

ソースコード

環境

  • Windows 11 Home 23H2 22631.3737
  • Docker Desktop Docker version 26.1.1, build 4cf5afa

コードの構造

- app
  + src
- sonar-project.properties
- docs
  - sonarQube
    - docker-compose.yml
    - bin
      - .env
      - up.sh
      - cli.sh 
    + reports

ソースコード

postgresql:16を利用している。
参考の通りにしたら、postgresql | 2024-06-17 16:40:12.168 UTC [40] FATAL: role "root" does not exist のエラーがでるのでUオプションでユーザ名を指定している。

  • version3.9未満の場合、depeneds_onを配列にすると動く

参考:eccube4がdocker-composeで立ち上がらない【services.ec-cube.depends_on contains an invalid type, it should be an array】とか出る

- version: '3.9'
+ version: '3'

    depends_on:
-      sonarqube_db:
-        condition: service_healthy
+      - sonarqube_db
docs/sonarQube/docker-compose.yml
version: '3.9'
services:
  sonarqube:
    image: sonarqube:community
    hostname: sonarqube
    container_name: sonarqube
    depends_on:
      sonarqube_db:
        condition: service_healthy
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://sonarqube_db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    ports:
      - "9000:9000"
  sonarqube_db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U sonar"]
      interval: 10s
      timeout: 5s
      retries: 5
    hostname: postgresql
    container_name: postgresql
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
      POSTGRES_DB: sonar
    volumes:
      - sonarqube_postgresql:/var/lib/postgresql
      - sonarqube_postgresql_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  sonarqube_postgresql:
  sonarqube_postgresql_data:

docker-compose upで起動すると http://localhost:9000 に立ち上がるので、手順に沿ってプロジェクトを作成したら環境変数を埋める。Yakumiプロジェクトを作成した。

.env
SONAR_TOKEN=sqp_mytoken
SONAR_PASS=hoge
sonar-project.properties
sonar.projectKey=Yakumi
sonar.sources=app/src
sonar.tests=app/tests
sonar.sourceEncoding=UTF-8

参考にしている手順と異なる点として、cliもdockerで動かしている。なお、HOST_URLはhost.docker.internalでローカルホストを指している。

docs/sonarQube/bin/cli.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
parent_dir=$(cd $bin_dir/.. && pwd)
work_dir=$(cd $bin_dir/../../.. && pwd)

source $bin_dir/.env
cd $work_dir

docker run \
    --rm \
     -v sonar_cache:/opt/sonar-scanner/.sonar/cache \
    -e SONAR_HOST_URL="http://host.docker.internal:9000/"  \
    -e SONAR_TOKEN="$SONAR_TOKEN" \
    -v ".:/usr/src" \
    sonarsource/sonar-scanner-cli

cd $parent_dir && curl -s -u admin:$SONAR_PASS -G -d "component=Yakumi" -d "metricKeys=cognitive_complexity" http://localhost:9000/api/measures/component_tree | jq '.baseComponent' > ./reports/cognitiveComplexity.json

動作確認

静的解析の結果が表示された

image.png

参考

Windows上でDocker越しSonarQubeを起動して、ローカルソースをコード検証する
SonarQubeさんに怒られたら
SonarSource - docker-compose
Docker - networking

1
0
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
1
0