From cad2421c7260797646cfaa3ce29232d48832144e Mon Sep 17 00:00:00 2001 From: "Md. Nasir Uddin" Date: Wed, 28 Oct 2020 12:18:10 +0600 Subject: [PATCH] feat: added sonarQube for code quality --- .gitignore | 3 +++ README.md | 19 +++++++++++++++++++ docker-compose.yml | 28 +++++++++++++++++++++++++++- docker/sonar-scanner/Dockerfile | 28 ++++++++++++++++++++++++++++ sonar-scanner.properties | 28 ++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 docker/sonar-scanner/Dockerfile create mode 100644 sonar-scanner.properties diff --git a/.gitignore b/.gitignore index 26b9e01..8483fa1 100644 --- a/.gitignore +++ b/.gitignore @@ -391,3 +391,6 @@ Temporary Items dist local/ documentation + +# SonarQube +.scannerwork diff --git a/README.md b/README.md index f076617..b6bef70 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ To view sample implementations based on this starter-kit, please visit the [nest | Logging | nestjs-pino | Done | | Request Validation | class-validator | Done | | Docker Ready | Dockerfile | Done | +| Continuous Code Quality | SonarQube | Done | | Auto-generated OpenAPI | - | Done | | Auto-generated ChangeLog | - | WIP | @@ -97,8 +98,26 @@ $ npm run migration:run # revert migration $ npm run migration:revert + ``` +## SonarQube locally analyse + +To locally analyse your code it is enough to run + +```bash +# build image +$ docker-compose up + +# run container from image +$ docker-compose exec sonarscanner sonar-scanner + +``` + +The results can be viewed in local instance of sonarqube [http://localhost:9000/sonarqube/dashboard?id=nest-typescript-starter](http://localhost:9000/sonarqube/dashboard?id=nest-typescript-starter). + +Local instance of sonarqube uses `sonar-scanner.properties` file. + ## Docker ```bash diff --git a/docker-compose.yml b/docker-compose.yml index 3faaf84..5f1df13 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: "3.8" +version: '3.8' services: app: @@ -44,6 +44,32 @@ services: depends_on: - mysqldb + sonarqube: + container_name: sonarqube + image: sonarqube:8-community + command: '-Dsonar.web.context=/sonarqube' + volumes: + - sonarqube_data:/opt/sonarqube/data + - sonarqube_extensions:/opt/sonarqube/extensions + - sonarqube_logs:/opt/sonarqube/logs + - sonarqube_temp:/opt/sonarqube/temp + ports: + - 9000:9000 + sonarscanner: + container_name: sonar-scanner + build: + context: . + dockerfile: ./docker/sonar-scanner/Dockerfile + volumes: + - .:/usr/src/ + - ./sonar-scanner.properties:/usr/lib/sonar-scanner/conf/sonar-scanner.properties + network_mode: host + command: tail -f /dev/null + volumes: node_modules: mysqldata: + sonarqube_data: + sonarqube_extensions: + sonarqube_logs: + sonarqube_temp: diff --git a/docker/sonar-scanner/Dockerfile b/docker/sonar-scanner/Dockerfile new file mode 100644 index 0000000..abde90e --- /dev/null +++ b/docker/sonar-scanner/Dockerfile @@ -0,0 +1,28 @@ +FROM openjdk:8-alpine + +ARG RELEASE=4.0.0.1744 + +RUN apk add --no-cache curl grep sed unzip bash nodejs nodejs-npm + +RUN npm install -g typescript +ENV NODE_PATH "/usr/lib/node_modules/" + +# Set timezone to CST +ENV TZ=Europe/Warsaw +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +WORKDIR /usr/src + +RUN curl --insecure -o ./sonarscanner.zip -L https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$RELEASE-linux.zip && \ + unzip sonarscanner.zip && \ + rm sonarscanner.zip && \ + mv sonar-scanner-$RELEASE-linux /usr/lib/sonar-scanner && \ + ln -s /usr/lib/sonar-scanner/bin/sonar-scanner /usr/local/bin/sonar-scanner + +ENV SONAR_RUNNER_HOME=/usr/lib/sonar-scanner + +# ensure Sonar uses the provided Java for musl instead of a borked glibc one +RUN sed -i 's/use_embedded_jre=true/use_embedded_jre=false/g' /usr/lib/sonar-scanner/bin/sonar-scanner + +ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait +RUN chmod +x /wait diff --git a/sonar-scanner.properties b/sonar-scanner.properties new file mode 100644 index 0000000..8fd555c --- /dev/null +++ b/sonar-scanner.properties @@ -0,0 +1,28 @@ +#Configure here general information about the environment, such as SonarQube DB details for example +#No information about specific project should appear here + +#----- Default SonarQube server +sonar.host.url=http://localhost:9000/sonarqube + +#----- 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 + +# H2 database from Docker Sonar container +sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar +sonar.projectKey=nest-typescript-starter +sonar.projectName=nest-typescript-starter +sonar.projectVersion=1 +sonar.projectBaseDir=/usr/src +sonar.sources=src +sonar.exclusions=**/*.spec.ts +sonar.tests.inclusions=**/*.spec.ts +sonar.javascript.lcov.reportPaths=coverage/lcov.info +sonar.typescript.lcov.reportPaths=coverage/lcov.info + +# Exclude node_modules for JS/TS-based scanning - this is provided in default configuration from +# the server, but in case you want to provide it locally and/or override it here: +sonar.exclusions=**/node_modules/**