GuilhermeStracini/POC-GHActions-CI-NetFramework

View on GitHub
.github/workflows/build-deploy.yml

Summary

Maintainability
Test Coverage
name: Build & Deploy

on:
  push:
    branches: [ main ]
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: Build
    runs-on: windows-latest

    steps:

      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set environment variables
        run: |
          echo "solution=$([io.path]::GetFileNameWithoutExtension($(Get-ChildItem -Path .\* -Include *.sln)))" | Out-File -FilePath $env:GITHUB_ENV -Append
          echo "sonar_key=$("${{ github.repository }}" -replace "/","_")" | Out-File -FilePath $env:GITHUB_ENV -Append

      - name: Set up Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: 21

      - name: Cache SonarCloud packages
        uses: actions/cache@v4
        with:
          path: ~\sonar\cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar

      - name: Cache SonarCloud scanner
        id: cache-sonar-scanner
        uses: actions/cache@v4
        with:
          path: .\.sonar\scanner
          key: ${{ runner.os }}-sonar-scanner
          restore-keys: ${{ runner.os }}-sonar-scanner

      - name: Install SonarCloud scanner
        if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
        shell: powershell
        run: |
          New-Item -Path .\.sonar\scanner -ItemType Directory
          dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner

      - name: Setup MSBuild Path
        uses: microsoft/setup-msbuild@v2
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: true

      - name: Setup Nuget
        uses: NuGet/setup-nuget@v2
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: true

      - name: Setup VSTest
        uses: darenm/Setup-VSTest@v1.3

      - name: DotNet Tool Install Coverlet
        run: dotnet tool install --global coverlet.console

      - name: Restore NuGet Packages
        run: nuget restore "${{ env.solution }}.sln"

      - name: Build & Test
        run: |
          .\.sonar\scanner\dotnet-sonarscanner begin /k:"${{ env.sonar_key }}" /o:"$("${{ github.repository_owner }}".ToLower())" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths="Tests\${{ env.solution }}.UnitTests\coverage.cobertura.xml"
          msbuild "${{ env.solution }}.sln" /p:Configuration=Release
          coverlet "Tests\${{ env.solution }}.UnitTests\bin\Release\${{ env.solution }}.UnitTests.dll" -t "vstest.console.exe" -a "Tests\${{ env.solution }}.UnitTests\bin\Release\${{ env.solution }}.UnitTests.dll" -o "Tests\${{ env.solution }}.UnitTests\\" -f cobertura
          .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
      
      - name: Install DeepSource scanner
        run: |
          curl -L https://github.com/DeepSourceCorp/cli/releases/download/v0.8.6/deepsource_0.8.6_windows_x86_64.tar.gz > deepsource.tar.gz
          tar -xzf deepsource.tar.gz

      - name: Analyze
        env:
          DEEPSOURCE_DSN: ${{ secrets.DEEPSOURCE_DSN }}
        run: .\deepsource report --analyzer test-coverage --key csharp --value-file "Tests\${{ env.solution }}.UnitTests\coverage.cobertura.xml"
      
      - name: Run
        run: Src\GHActionsCI\bin\Release\GHActionsCI.exe

      - name: Upload artifact
        uses: actions/upload-artifact@v4.4.3
        with:
          name: release
          path: Src\GHActionsCI\bin\Release\

  deploy:
    name: Deploy
    needs: build
    runs-on: ubuntu-latest

    steps:

      - uses: trstringer/manual-approval@v1
        with:
          secret: ${{ github.TOKEN }}
          approvers: guibranco

      - name: Download release
        uses: actions/download-artifact@v4
        with:
          name: release

      # - name: Upload to test FTP
      #   uses: sebastianpopp/ftp-action@releases/v2
      #   continue-on-error: true
      #   with:
      #     host: ${{ secrets.FTP_SERVER }}
      #     user: ${{ secrets.FTP_USERNAME }}
      #     password: ${{ secrets.FTP_PASSWORD }}

      # - name: Connect to SSH
      #   uses: appleboy/ssh-action@v1.0.3
      #   with:
      #     host: ${{ secrets.SSH_SERVER }}
      #     username: ${{ secrets.SSH_USERNAME }}
      #     password: ${{ secrets.SSH_PASSWORD }}
      #     port: ${{ secrets.SSH_PORT }}
      #     script: |
      #       mkdir test
      #       cd test
      #       touch test.txt
      #       rm test.txt
      #       cd ..
      #       rmdir test
      #       echo Done!

  cleanup:
    name: Cleanup
    needs: deploy
    runs-on: ubuntu-latest

    steps:

      - name: Remove artifacts
        uses: geekyeggo/delete-artifact@v5
        with:
          name: "*"