SonarQube: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 74: Line 74:
</syntaxhighlight>
</syntaxhighlight>
And nginx config
And nginx config
<syntaxhighlight lang="yaml" highlight="13">
<syntaxhighlight lang="yaml" highlight="20">
     server {
     server {
         listen 80;
         listen 80;

Revision as of 22:59, 16 November 2023

Introduction

This is how to install

docker pull sonarqube

docker volume create sonarqube-conf
docker volume create sonarqube-data
docker volume create sonarqube-logs
docker volume create sonarqube-extensions

docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 -v sonarqube-conf:/opt/sonarqube/conf -v sonarqube-data:/opt/sonarqube/data -v sonarqube-logs:/opt/sonarqube/logs -v sonarqube-extensions:/opt/sonarqube/extensions sonarqube

Remember you can restart docker container if one exists with

docker restart <container>

Move to docker-compose

Now got is working I move to docker-compose. We can use the existing volumes from above with external keyword. We need to have a reverse proxy with nginx.

version: '3.7'
services:
  sonarqube:
    image: sonarqube
    restart: always
    networks:
      - sonarqube-net
    container_name: sonarqube-bill
    ports:
      - '9000:9000'
      - '9092:9000'
    volumes:
      - 'sonarqube-conf:/opt/sonarqube/conf'
      - 'sonarqube-data:/opt/sonarqube/data' 
      - 'sonarqube-logs:/opt/sonarqube/logs' 
      - 'sonarqube-extensions:/opt/sonarqube/extensions'

  reverse_proxy:
    container_name: reverse_proxy
    depends_on:
      - sonarqube
    image: nginx
    restart: always
    networks:
      - sonarqube-net
    ports:
      - 9080:9080
      - 9443:9443
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - /etc/localcerts/192.168.1.70/server.crt:/etc/ssl/certs/sonarqube.crt
      - /etc/localcerts/192.168.1.70/server.key:/etc/ssl/private/sonarqube.key    

networks:
  sonarqube-net:
    driver: bridge
    
volumes:
  sonarqube-conf:
    external: true
    name: sonarqube-conf
  sonarqube-data:
    external: true
    name: sonarqube-data
  sonarqube-logs:
    external: true
    name: sonarqube-logs
  sonarqube-extensions:
    external: true
    name: sonarqube-extensions

We need to get the ip from docker as localhost is not our localhost but the containers. If you have more than one just use the docker plugin in vs code

docker network inspect bridge

And nginx config

    server {
        listen 80;
        server_name localhost;

        location / {
            return 301 https://$host$request_uri;
        }
    }

    server {
        large_client_header_buffers 4 32k;

        listen 443 ssl;
        server_name localhost;

        ssl_certificate /etc/ssl/certs/sonarqube.crt;
        ssl_certificate_key /etc/ssl/private/sonarqube.key;

        location / {
            proxy_pass         http://172.17.0.1:9000;
            proxy_redirect     off;
            proxy_http_version 1.1;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            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;
            proxy_set_header   X-Forwarded-Host $server_name;
            proxy_buffer_size           128k;
            proxy_buffers               4 256k;
            proxy_busy_buffers_size     256k;
        }
    }

Setting up the Scanner

export SONAR_SCANNER_VERSION=4.7.0.2747
export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-${SONAR_SCANNER_VERSION}-linux
curl --create-dirs -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip
unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/
export PATH=$SONAR_SCANNER_HOME/bin:$PATH
export SONAR_SCANNER_OPTS="-server"

I changed by .bashrc to have for next time

export SONAR_SCANNER_VERSION=4.7.0.2747
export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-${SONAR_SCANNER_VERSION}-linux
export PATH=$SONAR_SCANNER_HOME/bin:$PATH
export SONAR_SCANNER_OPTS="-server"

From there we can generate a token by going to http://localhost:9000/admin/users and look for the token column

Once you have one of there you can do

export SONAR_TOKEN=squ_3c567e0e8c92686895ddblahblah
sonar-scanner -Dsonar.projectKey=testvite2 -Dsonar.sources=. -Dsonar.host.url=http://localhost:9000

Parameters to set in Sonar

"sonar.exclusions": "**/*.test.tsx",
"sonar.tests": "./src",
"sonar.test.inclusions": "**/*.test.tsx,**/*.test.ts",
"sonar.typescript.lcov.reportPaths": "coverage/lcov.info",
"sonar.testExecutionReportPaths": "test-report.xml"

And testing

"sonar.exclusions": "**/myTests/**",
"sonar.tests": "./src/myTests",
"sonar.test.inclusions": "./src/myTests/**/*.test.tsx,./src/myTests/**/*.test.ts"