This commit is contained in:
Leonid Pershin
2025-10-17 07:33:46 +03:00
parent d0da72cc10
commit 2336e2a0c2

View File

@@ -16,67 +16,64 @@ jobs:
build: build:
name: Build, Test, Coverage, and Analyze name: Build, Test, Coverage, and Analyze
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
PATH: ${{ env.PATH }}:/root/.dotnet/tools
steps: steps:
# 1. Убедимся, что git установлен, и клонируем код вручную - uses: actions/checkout@v4
- name: Checkout code with git
run: |
which git || (apt-get update && apt-get install -y git)
git config --global core.autocrlf false
git config --global safe.directory /workspace
git clone https://$GITEA_USERNAME:$GITEA_PASSWORD@$GITEA_SERVER/$GITEA_OWNER/$GITEA_REPO.git .
git checkout $CI_COMMIT_REF_NAME
# 2. Установка .NET SDK
- name: Setup .NET - name: Setup .NET
run: | uses: actions/setup-dotnet@v4
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb with:
dpkg -i packages-microsoft-prod.deb dotnet-version: ${{ env.DOTNET_VERSION }}
rm packages-microsoft-prod.deb
apt-get update
apt-get install -y apt-transport-https
apt-get install -y dotnet-sdk-${DOTNET_VERSION%.*} # например, 9.0
# 3. Восстановление зависимостей
- name: Restore dependencies - name: Restore dependencies
run: dotnet restore run: dotnet restore
# 4. Установка dotnet-coverage
- name: Install dotnet-coverage - name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
# 5. Установка SonarScanner
- name: Install SonarScanner
run: dotnet tool install -g dotnet-sonarscanner
# 6. Начало анализа SonarCloud
- name: Begin SonarCloud analysis
run: | run: |
dotnet tool install --global dotnet-coverage
echo "Adding .dotnet/tools to PATH..."
echo 'export PATH="$PATH:/root/.dotnet/tools"' >> ~/.bashrc
export PATH="$PATH:/root/.dotnet/tools" export PATH="$PATH:/root/.dotnet/tools"
dotnet-sonarscanner begin \ echo "dotnet-coverage location:"
/k:"${{ env.SONAR_PROJECT_KEY }}" \ which dotnet-coverage || echo "dotnet-coverage not found in PATH"
/o:"${{ env.SONAR_ORG }}" \ ls -la /root/.dotnet/tools/ | grep dotnet-coverage || echo "No dotnet-coverage in tools directory"
/d:sonar.host.url="${{ env.SONAR_HOST_URL }}" \
/d:sonar.token="${{ secrets.SONAR_TOKEN }}" \ - name: Install SonarCloud Scanner
/d:sonar.cs.vscoveragexml.reportsPaths="coverage.xml" run: dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
- name: Begin SonarCloud analysis
run: ./.sonar/scanner/dotnet-sonarscanner begin /k:"${{ env.SONAR_PROJECT_KEY }}" /o:"${{ env.SONAR_ORG }}" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="${{ env.SONAR_HOST_URL }}" /d:sonar.cs.vscoveragexml.reportsPaths="coverage.xml"
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
# 7. Сборка проекта
- name: Build - name: Build
run: dotnet build --no-incremental --configuration Release run: dotnet build --no-incremental
# 8. Запуск тестов с покрытием
- name: Run tests and collect coverage - name: Run tests and collect coverage
run: | run: |
export PATH="$PATH:/root/.dotnet/tools" echo "Running tests and collecting coverage..."
dotnet-coverage collect "dotnet test --no-build --configuration Release" -f xml -o coverage.xml echo "PATH: $PATH"
echo "Trying dotnet-coverage from PATH..."
if dotnet-coverage collect "dotnet test --no-build" -f xml -o "coverage.xml"; then
echo "dotnet-coverage succeeded from PATH"
elif /root/.dotnet/tools/dotnet-coverage collect "dotnet test --no-build" -f xml -o "coverage.xml"; then
echo "dotnet-coverage succeeded with full path"
else
echo "dotnet-coverage failed, trying built-in coverage collection..."
dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResults --no-build
echo "Looking for coverage files..."
find ./TestResults -name "*.xml" -exec echo "Found: {}" \;
# Convert to the expected format
find ./TestResults -name "coverage.cobertura.xml" -exec cp {} coverage.xml \;
fi
# 9. Отладочный вывод — проверим, создан ли файл покрытия - name: List files for debug
- name: Check coverage file run: ls -la
run: |
ls -la coverage.xml - name: Print coverage.xml for debug
cat coverage.xml | head -20 run: cat coverage.xml
# 10. Завершение анализа SonarCloud
- name: End SonarCloud analysis - name: End SonarCloud analysis
run: | run: ./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
export PATH="$PATH:/root/.dotnet/tools" env:
dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}" SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}