Secbyte / dotnet-sonarscanner

dotnet sonarscanner GitHub Action
GNU General Public License v3.0
19 stars 16 forks source link

How to connect another container while scanning #10

Closed newbe36524 closed 4 years ago

newbe36524 commented 4 years ago

I can setup a mysql in github action and connect to it directly, but failed int sonar scanning process.

This is the yml for connecting directly

name: Claptrap build

on:
  push:
    branches: 
      - feature/database_mongodb

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      with:
        # Disabling shallow clone is recommended for improving relevancy of reporting
        fetch-depth: 0
    - uses: getong/mongo-action@v1.1
      with:
        mongo version: '4.2-bionic'
        host port: 27999
        mongo username: 'root'
        mongo password: 'claptrap'
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.101
    - name: Build
      run: |
        dotnet build $GITHUB_WORKSPACE/src --configuration Release
    - name: Test
      run: |
        dotnet test $GITHUB_WORKSPACE/src --collect:"XPlat Code Coverage" --settings $GITHUB_WORKSPACE/src/coverlet.runsettings -- NUnit.Where="Category == MongoDB"

But, it will fail by throwing timeout to connection exception while scanning code by sonar cloud. Yaml as beflow:

name: Claptrap build

on:
  push:
    branches: 
      - master
      - develop
      - feature/ci
      - feature/ut
  pull_request: 
    branches: 
      - master
      - develop

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      with:
        # Disabling shallow clone is recommended for improving relevancy of reporting
        fetch-depth: 0
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.101

    - uses: getong/mongo-action@v1.1
      with:
        mongo version: '4.2-bionic'
        host port: 27999
        mongo username: 'root'
        mongo password: 'claptrap'

    - uses: mirromutth/mysql-action@v1.1
      with:
        host port: 33306 # Optional, default value is 3306. The port of host
        container port: 3306 # Optional, default value is 3306. The port of container
        character set server: 'utf8mb4' # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld
        collation server: 'utf8mb4_general_ci' # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld
        mysql version: '8.0' # Optional, default value is "latest". The version of the MySQL
        mysql database: 'claptrap' # Optional, default value is "test". The specified database which will be create
        mysql root password: claptrap # Required if "mysql user" is empty, default is empty. The root superuser password
        # mysql user: 'claptrap' # Required if "mysql root password" is empty, default is empty. The superuser for the specified database. Can use secrets, too
        # mysql password: claptrap # Required if "mysql user" exists. The password for the "mysql user"

    - name: Setup Postgres database
      uses: Daniel-Marynicz/postgresql-action@master
      with:
        postgres_image_tag: 12-alpine
        app_user: claptrap
        app_user_password: claptrap
        app_db: claptrap
        postgres_extensions: uuid-ossp ltree
        exposed_postgres_port: 35432

    - name: Install dependencies
      run: |
        cd $GITHUB_WORKSPACE/src
        dotnet restore
    - name: Sonarscanner for dotnet
      uses: Secbyte/dotnet-sonarscanner@v2.3
      with:
        buildCommand: dotnet build $GITHUB_WORKSPACE/src --configuration Release
        testCommand: dotnet test $GITHUB_WORKSPACE/src --collect:"XPlat Code Coverage" --settings $GITHUB_WORKSPACE/src/coverlet.runsettings -- NUnit.Where="namespace =~ ^Newbe"
        projectKey: newbe36524_Newbe.Claptrap
        projectName: Newbe.Claptrap
        sonarOrganisation: newbe36524
        beginArguments: >
            /d:sonar.cs.opencover.reportsPaths='"src/Newbe.Claptrap.Tests/TestResults/*/coverage.opencover.xml"'
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    - name: Codecov
      uses: codecov/codecov-action@v1.0.6
      # with:
      #   # User defined upload name. Visible in Codecov UI
      #   name: # optional
      #   # Repository upload token - get it from codecov.io. Required only for private repositories
      #   token: # optional
      #   # Path to coverage file to upload
      #   file: # optional
      #   # Flag upload to group coverage metrics (e.g. unittests | integration | ui,chrome)
      #   flags: # optional
      #   # Specify whether or not CI build should fail if Codecov runs into an error during upload
      #   fail_ci_if_error: # optional

    - name: Codacy Coverage Reporter
      uses: codacy/codacy-coverage-reporter-action@0.2.0
      with:
        # Project token for the Codacy project you want to send coverage information
        project-token: ${{ secrets.CODACY_TOKEN }}
        # Optional comma separated list of coverage reports to send to Codacy
        coverage-reports: src/*.Tests/TestResults/*/coverage.opencover.xml # optional, default is 
newbe36524 commented 4 years ago

Hi, I got the job done. please feel free to close this issue or we can add some document about what I faced and how to solve in readme ?

I just check the document named "Creating PostgreSQL service containers" out. And made some modification on my yaml as below:

  1. add jobs.build.container. In this way, CI command will be luanch in the container instead of on the vm directly
  2. add jobs.build.services to host my database instead of using github action from marketplace. Since github action actually is a container too.
  3. connect to the database by service name as a host name. e.g. I add a postgres service as a postgresql server, then I can connect to it by hostname postgres in my database connection string.

This is my full yaml which works find in my project ( I am using mysql/mongo and postgresql ):

name: Claptrap build

on:
  push:
    branches: 
      - master
      - develop
      - feature/ci
      - feature/ut

  pull_request: 
    branches: 
      - master
      - develop

jobs:
  build:

    runs-on: ubuntu-latest
    container: mcr.microsoft.com/dotnet/core/sdk:3.1.301-bionic
    services:
      mongo:
        image: mongo
        ports:
          - 27017
        env:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: claptrap
      mysql:
        image: mysql
        ports:
          - 3306
        env:
          MYSQL_ROOT_PASSWORD: claptrap
          MYSQL_DATABASE: claptrap
          MYSQL_USER: claptrap
          MYSQL_PASSWORD: claptrap
      postgres:
        image: postgres:12.3-alpine
        env:
          POSTGRES_PASSWORD: claptrap
          POSTGRES_DB: claptrap
        ports:
          - 5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
    - uses: actions/checkout@v2
      with:
        # Disabling shallow clone is recommended for improving relevancy of reporting
        fetch-depth: 0
    - name: Install dependencies
      run: |
        cd $GITHUB_WORKSPACE/src
        dotnet restore
    - name: Sonarscanner for dotnet
      uses: Secbyte/dotnet-sonarscanner@v2.3
      with:
        buildCommand: dotnet build $GITHUB_WORKSPACE/src --configuration Release
        testCommand: dotnet test $GITHUB_WORKSPACE/src --collect:"XPlat Code Coverage" --settings $GITHUB_WORKSPACE/src/coverlet.runsettings -- NUnit.Where="namespace =~ ^Newbe"
        projectKey: newbe36524_Newbe.Claptrap
        projectName: Newbe.Claptrap
        sonarOrganisation: newbe36524
        beginArguments: >
            /d:sonar.cs.opencover.reportsPaths='"src/Newbe.Claptrap.Tests/TestResults/*/coverage.opencover.xml"'
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    - name: Codecov
      uses: codecov/codecov-action@v1.0.6

    - name: Codacy Coverage Reporter
      uses: codacy/codacy-coverage-reporter-action@0.2.0
      with:
        # Project token for the Codacy project you want to send coverage information
        project-token: ${{ secrets.CODACY_TOKEN }}
        # Optional comma separated list of coverage reports to send to Codacy
        coverage-reports: src/*.Tests/TestResults/*/coverage.opencover.xml # optional, default is 
joshuaduffy commented 4 years ago

Thank you for posting the solution, extremely helpful to myself and others, aiming to provide some more comprehensive documentation and tighten things up a bit so thank you for your feedback also!