SonarQube: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "=Introduction= This is how to install <syntaxhighlight lang="bash"> 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 </syntaxhighlight>"
 
 
(20 intermediate revisions by the same user not shown)
Line 2: Line 2:
This is how to install
This is how to install
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
docker pull sonarqube
docker volume create sonarqube-conf
docker volume create sonarqube-conf
docker volume create sonarqube-data
docker volume create sonarqube-data
docker volume create sonarqube-logs
docker volume create sonarqube-logs
docker volume create sonarqube-extensions
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
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
</syntaxhighlight>
Remember you can restart docker container if one exists with
<syntaxhighlight lang="bash">
docker restart <container>
</syntaxhighlight>
=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.
==docker-compose.yaml==
Here is the docker file
<syntaxhighlight lang="yaml">
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
</syntaxhighlight>
==Get the IP of the docker localhost==
Find the gateway. 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
<syntaxhighlight lang="bash">
docker network inspect bridge
</syntaxhighlight>
Here is the output
<syntaxhighlight lang="json">
[
    {
        "Name": "bridge",
        "Id": "bef7fdbdb9b786a6b356b9fcd168df85922899ced0feb3f19c6394b2a3eadab1",
        "Created": "2023-11-14T22:07:14.2042138+13:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
....
</syntaxhighlight>
==Add nginx configuration==
Add nginx config default.conf where the docker-compose.yaml is
<syntaxhighlight lang="yaml" highlight="20">
    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;
        }
    }
</syntaxhighlight>
==Version 2==
Whilst the solution worked I had problems with the OPEN IN IDE button. I ended up running this on host network. I changed the host to be my IP to stop using localhost. Now it all seems to work.
<syntaxhighlight lang="yaml">
version: '3.7'
services:
  sonarqube:
    image: sonarqube
    restart: always
    # networks:
    #  - sonarqube-net2
    network_mode: host
    container_name: sonarqube-bill
    environment:
      - SONAR_WEB_HOST=192.168.1.70
    volumes:
      - 'sonarqube-conf:/opt/sonarqube/conf'
      - 'sonarqube-data:/opt/sonarqube/data'
      - 'sonarqube-logs:/opt/sonarqube/logs'
      - 'sonarqube-extensions:/opt/sonarqube/extensions'
# networks:
#  sonarqube-net2:
#    driver: host
   
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
</syntaxhighlight>
=Setting up the Scanner=
<syntaxhighlight lang="bash">
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"
</syntaxhighlight>
I changed by .bashrc to have for next time
<syntaxhighlight lang="bash">
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"
</syntaxhighlight>
From there we can generate a token by going to http://localhost:9000/admin/users and look for the token column<br>
[[File:Sonar.jpg | 500px ]]<br>
Once you have one of there you can do
<syntaxhighlight lang="bash">
export SONAR_TOKEN=squ_3c567e0e8c92686895ddblahblah
sonar-scanner -Dsonar.projectKey=testvite2 -Dsonar.sources=. -Dsonar.host.url=http://localhost:9000
</syntaxhighlight>
Parameters to set in Sonar
<syntaxhighlight lang="bash">
"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"
</syntaxhighlight>
And testing
<syntaxhighlight lang="bash">
"sonar.exclusions": "**/myTests/**",
"sonar.tests": "./src/myTests",
"sonar.test.inclusions": "./src/myTests/**/*.test.tsx,./src/myTests/**/*.test.ts"
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 23:38, 18 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.

docker-compose.yaml

Here is the docker file

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

Get the IP of the docker localhost

Find the gateway. 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

Here is the output

[
    {
        "Name": "bridge",
        "Id": "bef7fdbdb9b786a6b356b9fcd168df85922899ced0feb3f19c6394b2a3eadab1",
        "Created": "2023-11-14T22:07:14.2042138+13:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
....

Add nginx configuration

Add nginx config default.conf where the docker-compose.yaml is

    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;
        }
    }

Version 2

Whilst the solution worked I had problems with the OPEN IN IDE button. I ended up running this on host network. I changed the host to be my IP to stop using localhost. Now it all seems to work.

version: '3.7'
services:
  sonarqube:
    image: sonarqube
    restart: always
    # networks:
    #   - sonarqube-net2
    network_mode: host
    container_name: sonarqube-bill
    environment:
      - SONAR_WEB_HOST=192.168.1.70
    volumes:
      - 'sonarqube-conf:/opt/sonarqube/conf'
      - 'sonarqube-data:/opt/sonarqube/data' 
      - 'sonarqube-logs:/opt/sonarqube/logs' 
      - 'sonarqube-extensions:/opt/sonarqube/extensions'

# networks:
#   sonarqube-net2:
#     driver: host
    
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

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"