Dotnet Sonarcloud end failed - msbuild

I trying to integrate my Solution based on .net6 with SonarCloud and Github actions.The problem is that the action build failed on the sonar scanner end.I tried to change working dirs but with the same effect.The project is public HERE
The SonarScanner for MSBuild integration failed: SonarCloud was unable to collect the required information about your projects.
Possible causes:
The project has not been built - the project must be built in between the begin and end steps
An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0.25420.1 and higher are supported.
The begin, build and end steps have not all been launched from the same folder
None of the analyzed projects have a valid ProjectGuid and you have not used a solution (.sln) SonarScanner for MSBuild 5.7.2 Using
the .NET Core version of the Scanner for MSBuild Post-processing
started. 10:38:06.016 Generation of the sonar-properties file failed.
Unable to complete the analysis. 10:38:06.024 Post-processing failed.
Exit code: 1 Error: Process completed with exit code 1.
name: build-all
# Controls when the action will run.
on:
push:
branches:
- main
env:
DOTNET_VERSION: 6.0.x
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build-windows:
# The type of runner that the job will run on
runs-on: windows-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v2
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
- uses: actions/setup-dotnet#v1
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- uses: microsoft/setup-msbuild#v1
- uses: actions/setup-java#v2
with:
distribution: 'adopt'
java-version: '11'
- name: Restore NuGet packages
run: |
cd App
nuget restore App.sln
- name: Begin Sonar scan
run: |
cd App
dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /o:vladimirpetukhov /k:vladimirpetukhov_Musement_CLI /d:sonar.login=${{ secrets.SONAR_TOKEN }} /d:sonar.host.url=https://sonarcloud.io
- name: Build Api
run: |
cd ./App/App.API
dotnet build App.API.csproj --no-restore
# dotnet test App.API.csproj --no-build --no-restore --verbosity normal -p:CollectCoverage=true -p:CoverletOutputFormat=opencover
- name: Build Main
run: |
cd ./App/App.Main
dotnet build App.Main.csproj --no-restore
# dotnet test App.Main.csproj --no-build --no-restore --verbosity normal -p:CollectCoverage=true -p:CoverletOutputFormat=opencover
- name: End Sonar scan
run: |
cd App
dotnet sonarscanner end /d:sonar.login=${{ secrets.SONAR_TOKEN }}

Related

Accessing agent computer in azure devops

I am new in azure devops services, and i am trying to run a pipeline that process an asp.net core application.
I have a task for restoring the project dependencies. Its code is dotnet Restore
.
The piepeline fail on that command with this message :
1:5>C:\Program Files\dotnet\sdk\5.0.410\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(141,5): error NETSDK1045: The current .NET SDK does not support targeting .NET 6.0. Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports .NET 6.0. [D:\a\1\s\AspNetProjectOnDocker.csproj]
Done executing task "NETSdkError" -- FAILED.
1:5>Done building target "_CheckForUnsupportedNETCoreVersion" in project "AspNetProjectOnDocker.csproj" -- FAILED.
1:5>Done Building Project "D:\a\1\s\AspNetProjectOnDocker.csproj" (_GenerateRestoreGraphProjectEntry target(s)) -- FAILED.
1>Done executing task "MSBuild" -- FAILED.
1>Done building target "_GenerateRestoreGraph" in project "AspNetProjectOnDocker.csproj" -- FAILED.
1>Done Building Project "D:\a\1\s\AspNetProjectOnDocker.csproj" (Restore target(s)) -- FAILED.
AspNetProjectOnDocker is the name of my .net project
My Questions :
How to access to the folder : C:\Program Files\dotnet\sdk\5.0.410\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets specified in the error message above.
Thanks.
My Questions :
How to access to the folder : C:\Program
Files\dotnet\sdk\5.0.410\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets
specified in the error message above.
Answer to your question:
This is not a folder, it is a file. You can access it via the following YAML pipeline definition.
trigger:
- none
pool:
vmImage: windows-2019
steps:
- script: |
echo Check target file
C:
cd C:\Program Files\dotnet\sdk\5.0.410\Sdks\Microsoft.NET.Sdk\targets
more Microsoft.NET.TargetFrameworkInference.targets
displayName: 'Check target file'
# if you encounter some issue such as can't find directory, you can use the following method to check what happens.
# - script: |
# echo Check target file step by step
# dir
# C:
# dir
# cd Program Files
# dir
# cd dotnet
# dir
# cd sdk
# dir
# cd 5.0.410
# dir
# cd Sdks
# dir
# cd Microsoft.NET.Sdk
# dir
# cd targets
# dir
# more Microsoft.NET.TargetFrameworkInference.targets
# displayName: 'Check target file step by step'
Successfully get the file content:

AttributeError when using Github self-hosted runners to run unit test

Hello~ I am trying to use Github workflow to run the unit test for the code in my repository.
So I wrote a yaml file, its function is when I push my code to my repository, it can let me use my local environment to execute my code on Github, and the purpose of these codes is to run the unit tests.
But I can't run this workflow successfully, this error message always appears. I'm curious why I can execute unit test successfully on the local IDE, but it doesn't work when I use workflow to automatically execute it for me.
============================== warnings summary ===============================
..\..\..\..\anaconda3\lib\site-packages\pyreadline\py3k_compat.py:8
C:\Users\COA\anaconda3\lib\site-packages\pyreadline\py3k_compat.py:8: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
return isinstance(x, collections.Callable)
-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
ERROR test/test_createds_for_fnn.py - AttributeError: type object 'h5py.h5.H5...
ERROR test/test_fnn.py - AttributeError: type object 'h5py.h5.H5PYConfig' has...
ERROR test/test_unet.py - AttributeError: type object 'h5py.h5.H5PYConfig' ha...
!!!!!!!!!!!!!!!!!!! Interrupted: 3 errors during collection !!!!!!!!!!!!!!!!!!!
======================== 1 warning, 3 errors in 2.76s =========================
Error: Process completed with exit code 1.
My environment:
Windows 10 OS
Python 3.8.8
Tensorflow-gpu 2.7.0
pytest 6.2.3
h5py 3.6.0
Workflow
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: self-hosted
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!
# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project
echo train the fnn
pytest

NETSDK1045: The current .NET SDK does not support 'newer version' as a target

I've created a simple ASP.NET CORE 6 Web API. I then pushed it to Github. When I try to create a pipeline in Azure Devops, I'm getting the error.
C:\Program Files\dotnet\sdk\5.0.403\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET
.TargetFrameworkInference.targets(141,5): error NETSDK1045: The current .NET SDK does not support
targeting .NET 6.0. Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports
.NET 6.0.
I've just downloaded VS 2022 community edition. I've installed .Net SDK 6.
Here's my csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>
I chose what Azure devops suggested, so this is my yml file
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package
/p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true
/p:PackageLocation="$(build.artifactStagingDirectory)"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest#2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
I've followed what's said in this documentation, but it's not working. The build is still failing like this
[![enter image description here][2]][2]
Thanks for helping
After reading this 2 links, it there were 2 issues with the template from Azure devops.
the restore packages kept on looking for SDK 5. So this task fixed the issue. I've to include a step that mentions SDK 6. https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/package/nuget?view=azure-devops
also it kept on targeting Visual Studio 2019, which doesn't support .NET 6.0.x. Therefore, instead of vmImage: 'windows-latest', using vmImage: 'windows-2022' allow the build to succeed https://github.com/dotnet/core/issues/6907.
I ended up using the classic template so that I can understand better how the pipeline works. 1) Either using ubuntu-latest or windows-2022. 2) add a step for the version of SDK.
I chose what Azure devops suggested, so this is my yml file
Unfortunately it's easy to confuse things, since Microsoft's naming is anything but consistent.
You need to use .NET Core guide for this: Build, test, and deploy .NET Core apps
To save you time, here is a simple YAML pipeline to build ASP.NET Core applications:
trigger:
branches:
include:
- master
# Setup pipeline-level variables to keep things DRY
variables:
configuration: Release
projects: '**/*.csproj'
publish_dir: $(Build.ArtifactStagingDirectory)
vm_image: ubuntu-latest
stages:
- stage: Build
jobs:
- job: dotnet
displayName: .NET
pool:
vmImage: $(vm_image)
# Run builds in latest .NET 6 SDK container (Debian 11)
# This simplifies things and allows to easily target different SDKs:
# https://hub.docker.com/_/microsoft-dotnet-sdk
container: mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim
workspace:
clean: all
# Build tasks a separated (restore/build/test/publish)
# to make it easier to catch/debug issues
steps:
- task: DotNetCoreCLI#2
displayName: .NET | Restore [$(configuration)]
inputs:
command: restore
projects: $(projects)
- task: DotNetCoreCLI#2
displayName: .NET | Build [$(configuration)]
inputs:
command: build
projects: $(projects)
arguments: --configuration $(configuration) --no-restore
- task: DotNetCoreCLI#2
displayName: .NET | Test [$(configuration)]
inputs:
command: test
projects: $(projects)
publishTestResults: true
arguments: --configuration $(configuration) --no-restore --no-build
- task: DotNetCoreCLI#2
displayName: .NET | Publish [$(configuration)]
inputs:
command: publish
publishWebProjects: true
zipAfterPublish: true
modifyOutputPath: true
arguments: --configuration $(configuration) --output $(publish_dir) --no-restore --no-build
# Publish zipped build results so they can be downloaded from the pipeline UI
- publish: $(publish_dir)
displayName: Artifact | Publish
artifact: $(Build.DefinitionName)

Code Coverage Tab Not Showing a Report in Azure DevOps

I have a build pipeline for my .NET Core 2.2 web application which has a .NET Core task for running the tests. However, after running my pipeline and checking the code coverage tab I got the message below.
Is there anything missing that I forgot to do or set up?
You can try below steps to publish the code coverage.
1, First you need to make sure your project reference to nuget package coverlet.msbuild.
<PackageReference Include="coverlet.msbuild" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
2, Download the report generator by running a custom dotnet command install --tool-path . dotnet-reportgenerator-globaltool See below example in yaml format.
- task: DotNetCoreCLI#2
inputs:
command: custom
custom: tool
arguments: install --tool-path . dotnet-reportgenerator-globaltool
displayName: Install ReportGenerator tool
3, Then in your dotnet test task to enable CollectCoverage by adding below arguments:
arguments: '/p:CollectCoverage=true /p:CoverletOutput=$(Build.SourcesDirectory)\TestResult\ /p:CoverletOutputFormat=cobertura'
4, And install task Report Generator to your organization, and in reportgenerator task specify the reports folder to the CoverletOutput folder reports: $(Build.SourcesDirectory)\TestResult\**\coverage.cobertura.xml as sepecified in above dotnet test task /p:CoverletOutput=$(Build.SourcesDirectory)\TestResult\.
See below full example in yaml format:
steps:
- task: UseDotNet#2
inputs:
version: 2.2.x
- task: DotNetCoreCLI#2
inputs:
command: restore
projects: '**\*.csproj'
- task: DotNetCoreCLI#2
inputs:
command: custom
custom: tool
arguments: install --tool-path . dotnet-reportgenerator-globaltool
displayName: Install ReportGenerator tool
- task: DotNetCoreCLI#2
displayName: Test .NET
inputs:
command: test
projects: '**\*Test*.csproj'
publishTestResults: false
arguments: '--configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutput=$(Build.SourcesDirectory)\TestResult\ /p:CoverletOutputFormat=cobertura'
condition: succeededOrFailed()
- task: reportgenerator#4
inputs:
reports: '$(Build.SourcesDirectory)\TestResult\**\coverage.cobertura.xml'
targetdir: '$(Build.SourcesDirectory)\coverlet\reports'
verbosity: 'Verbose'
condition: succeededOrFailed()
- task: PublishCodeCoverageResults#1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(Build.SourcesDirectory)\coverlet\reports\Cobertura.xml
failIfCoverageEmpty: false
reportDirectory: $(Build.SourcesDirectory)\coverlet\reports\
condition: succeededOrFailed()
You need have this settings:
--configuration $(BuildConfiguration) --collect:"XPlat Code Coverage" You need to install coverlet.collector nuget package.
Then install report generator: install --tool-path . dotnet-reportgenerator-globaltool
Run report generator: ./reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"
And publish code coverage to get this:

Setup of azure-pipelines.yml "No web project was found in the repository" with ASP.NET Core

I need help to setup my azure-pipelines.yml build file because I cannot find any good tutorial, sample or other kind of help anywhere.
I follow this tutorial by Microsoft https://learn.microsoft.com/en-us/azure/devops/pipelines/languages/dotnet-core?view=azure-devops but despite all information they give I still get errors.
azure-pipelines.yml
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- master
pool:
vmImage: 'Ubuntu-16.04'
variables:
buildConfiguration: 'Release'
steps:
# - script: dotnet build --configuration $(buildConfiguration)
# displayName: 'dotnet build $(buildConfiguration)'
- task: DotNetCoreInstaller#0
inputs:
version: '2.2.202' # replace this value with the version that you need for your project
- script: dotnet restore
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration Release' # Update this to match your need
# do this after you've built your app, near the end of your pipeline in most cases
# for example, you do this before you deploy to an Azure web app on Windows
- task: DotNetCoreCLI#2
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
- task: PublishBuildArtifacts#1
inputs:
ArtifactName: 'drop'
Log
##[section]Starting: DotNetCoreCLI
==============================================================================
Task : .NET Core
Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command. For package commands, supports NuGet.org and authenticated feeds like Package Management and MyGet.
Version : 2.150.1
Author : Microsoft Corporation
Help : [More Information](https://go.microsoft.com/fwlink/?linkid=832194)
==============================================================================
##[error]No web project was found in the repository. Web projects are identified by presence of either a web.config file or wwwroot folder in the directory.
##[error]Project file(s) matching the specified pattern were not found.
##[section]Finishing: DotNetCoreCLI
Why these error and what am I doing wrong?
Additional information
- I can build and publish from Visual Studio to my Azure web app. My API is working. I just cannot do it as part of CI / CD.
- I don't understand this error because I don't understand why the DotNetCore are requesting a web.config.
It only worked for me when I gave it the path to my Web API (set **/*WebApi.csproj to your own location).
But just as importantly, I had to assure the build agent it was NOT a web project by setting publishWebProjects to false.
- task: DotNetCoreCLI#2
displayName: '📗📗📗 Publish 📗📗📗'
inputs:
command: publish
projects: '**/*WebApi.csproj'
publishWebProjects: False
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
Q: Are emojis necessary in the publish definition?
A: Yes, emojis are necessary. (Actually, no. But it would be less silly than having to set publishWebProjects to false.)