Azure CI Pipeline - DotNetCoreCLI#2 Task error - azure-devops

I developed WebAPI project using .NET Core 3.1.0 and integration tests using XUnit.
I added the below task in Azure DevOps CI Pipeline (azure-pipelines.yaml) to run the integration tests project.
- task: DotNetCoreCLI#2
displayName: 'Run API integration tests - $(buildConfiguration)'
inputs:
command: 'test'
arguments: '--configuration $(buildConfiguration)'
publishTestResults: true
projects: '**/IntegrationTests/IntegrationTests.csproj'
I got the below error during pipeline execution. How to resolve this error?
##[error]Error: The process '/usr/bin/dotnet' failed with exit code 1
##[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build
Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://learn.microsoft.com/en-us/dotnet/core/tools/ and https://learn.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
##[error]Dotnet command failed with non-zero exit code on the following projects : /home/vsts/work/1/s/src/IntegrationTests/IntegrationTests.csproj

I've had a smiller issue but with .netcore 2.2. The problem was that the test tries to build before the test starts restoring the packages for the test, thus fails before the test runs or builds. One thinge that help me overcome this problem was this FAQ:
Most dotnet commands, including build, publish, and test include an implicit restore step. This will fail against authenticated feeds, even if you ran a successful dotnet restore in an earlier step, because the earlier step will have cleaned up the credentials it used.
To fix this issue, add the --no-restore flag to the Arguments textbox.
I've also read that the DotNetCLI had some issues when it came to tests like this one here
So I ended up using a script to solve this and other issues related to package restore.
- script: dotnet test '**/IntegrationTests/IntegrationTests.csproj' --configuration $(buildConfiguration) --logger trx;LogFileName=C:\temp\results
displayName: 'Run API integration tests - $(buildConfiguration)'
I hope that will help you or anyone who has similar issues.

I've had exactly the same problem, with the difference that my solution consisted of .net5 apps as well as .netcore3.1 apps.
I was able to solve this problem by specifying the newer dotnet runtime in the azure pipeline:
- task: UseDotNet#2
inputs:
version: '5.0.x'
packageType: runtime

Related

Azure DevOps Pipeline VsBuild dont work with Azure Function v2

I'm trying to publish my app with Azure DevOps. I have on the same repo 2 Azure functions (v2 and 3), 1 web application and 2 "libraries projects" (.net Framework 4). So it's my first time with DevOps and it's really hard...
I have a problem when building. I use a VSBuild task to build all the projects at the same time. Here my yaml :
- task: UseDotNet#2
displayName: 'Use .NET SDK 2.1'
inputs:
packageType: 'sdk'
version: '2.1.202' #I tried some others versions too
- task: VSBuild#1
inputs:
solution: 'project.sln'
Here the error :
##[error]C:\Users\VssAdministrator\.nuget\packages\microsoft.net.sdk.functions\1.0.29\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): Error : It was not possible to find any compatible framework version
And in the csproj of the project (Azure Function)
<TargetFramework>netstandard2.0</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
Has someone an idea to make this build work ?
To resolve this ##[error]C:\Users\VssAdministrator\.nuget\packages\microsoft.net.sdk.functions\1.0.29\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): Error : It was not possible to find any compatible framework version error:
Make sure your 1 web application and 2 libraries projects both have the same version of Microsoft.NET.Sdk.Functions 1.0.29.
References: https://github.com/Azure/azure-functions-vs-build-sdk/issues/199#issuecomment-665776934 , It was not possible to find any compatible framework version. The specified framework 'Microsoft.NETCore.App', version '2.2.0' was not found and https://github.com/dotnet/core/issues/3487

Inconsistent output in YAML pipeline vs default Azure Pipeline

I'm trying to run a "dotnet tool install" to install cli tool on the build agent in my pipeline and the behavior is inconsistent. Sometimes it executes the command successfully but it also fails sometimes with below error and I cannot figure out why however the same step executes successfully when i use azure default pipeline instead of YAML based.
error NU1212: Invalid project-package combination for project 1.0.0. DotnetToolReference project style can only contain references of the DotnetTool type The tool package could not be restored. Tool 'project' failed to install. This failure may have been caused by:
* You are attempting to install a preview release and did not use the --version option to specify the version.
* A package by this name was found, but it was not a .NET Core tool.
* The required NuGet feed cannot be accessed, perhaps because of an Internet connection problem.
* You mistyped the name of the tool.
Here is the task:
- task: DotNetCoreCLI#2
displayName: 'Install project'
inputs:
command: custom
custom: tool
arguments: 'install -g --version 1.0.0 --add-source https://sample.jfrog.io/artifactory/project project'
Command:
dotnet tool install -g --version 1.0.0 --add-source https://sample.jfrog.io/artifactory/project project
These tasks are running on Windows-latest image on Microsoft Hosted agents

Random failures in build pipelines when running dotnet-ef

As part of build I need to generate db migration script. I'm using Microsoft provided build agent
(only interesting part below)
pool:
vmImage: 'windows-2019'
- task: DotNetCoreCLI#2
displayName: Install dotnet-ef
inputs:
command: 'custom'
custom: 'tool'
arguments: 'install dotnet-ef -g --version 5.0.0-preview.8.20407.4'
- task: DotNetCoreCLI#2
displayName: Generate migrations sql script
inputs:
command: 'custom'
custom: 'ef'
arguments: 'migrations script --project Web/Dal --startup-project Web/WebApi --configuration $(buildConfiguration) --context EmailContext --no-build --output $(Build.ArtifactStagingDirectory)/emailcontext-migrations.sql --idempotent --verbose'
dotnet-ef installation seems to work fine:
Tool 'dotnet-ef' (version '5.0.0-preview.8.20407.4') was successfully installed.
but it still fails from time to time with (more often recently) :
"C:\Program Files\dotnet\dotnet.exe" ef migrations script --project Web/Dal --startup-project Web/WebApi --configuration Release --context EmailContext --no-build --output D:\a\1\a/emailcontext-migrations.sql --idempotent --verbose
Could not execute because the specified command or file was not found.
Is there a problem with my build pipeline configuration?
If it fails from time to time I would rather say that this can be an issue with preview version.
Please add an next step after installing to list all globally installed tools:
dotnet tool list -g
You may also show us a log of installing tool for case when your pipeline doesn't work. To verify if you have this:
(We simply don't know it, since we can't check your logs).
And if it still happens I would encourage you to create an issue on GitHub.
From your description, this is an intermittent issue. So your pipeline configuration could be correct.
Could not execute because the specified command or file was not found.
This issue seems to be related to the dotnet-ef package installed.
As Krzysztof Madej's suggestion, this package version could cause this issue.
You could try to use the latest version: 5.0.0-rc.1.20451.13 or latest stable version: 3.1.8.
Here is a GitHub ticket with the same issue( Can't find the file after global installing dotnet-ef). You could follow it and check the update.
On the other hand, you could try to use the Command Line Task to install the dotnet-ef.
For example:
- task: CmdLine#2
inputs:
script: 'dotnet tool install --global dotnet-ef --version xxxx'

Azure DevOps Pipeline PostSharp failure on linux VM

I had planned to move my DevOps Pipeline to linux vm. Unfortunately, I faced with this problem. I am using Postsharp 6 on my .Net Core 2.2 project.
Build FAILED.
/home/vsts/.nuget/packages/postsharp/6.1.18/build/PostSharp.targets(148,5): error MSB4062: The "PostSharp.MSBuild.PostSharpValidateLanguageVersion" task could not be loaded from the assembly /home/vsts/.nuget/packages/postsharp/6.1.18/build//net471/PostSharp.MSBuild.v6.1.18.Release.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. [/home/vsts/work/1/s/Core.csproj]
/home/vsts/.nuget/packages/postsharp/6.1.18/build/PostSharp.targets(148,5): error MSB4062: Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [/home/vsts/work/1/s/Core.csproj]
0 Warning(s)
1 Error(s)
PostSharp doesn't work on Linux VM.
The only solution is using Windows Server on yaml. Even Selenium removed Postharp support.
trigger:
- master
pool:
vmImage: 'windows-2019'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
You can look at the pictures to understand the steps better.
Update
PostSharp 6.3.5 preview version supports Linux and macOS builds.
Also, you can create a docker image too. I am using these images:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build

Code Coverage Screen hangs in Azure Pipeline Build

Im currently setting up an azure pipeline for my repository. Currently it builds correctly and runs the unit tests. However the code coverage tab just spins infinitely. Any idea on what would cause this?
Details:
The artifact directory looks like this:
The console shows this error:
Error: Could not find route for route id
ms.vss-tfs-web.project-overview-route. Ensure that the requested route
is added to routes shared data.
This is how the test results are ran and generated:
dotnet tool install dotnet-reportgenerator-globaltool --tool-path .
dotnet test $(Build.SourcesDirectory)\RulesMadeEasy.Tests -c debug --logger trx --no-restore /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --results-directory $(Build.SourcesDirectory)\TestResults\ /p:CoverletOutput=$(Build.SourcesDirectory)\TestResults\
.\reportgenerator -reports:$(Build.SourcesDirectory)\TestResults\coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)\TestResults\ -reporttypes:"HTMLInline_AzurePipelines;Badges" --version 4.0.0-rc4
The code coverage results are published using the PublishCodeCoverageResults#1 task with the following inputs
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(Build.SourcesDirectory)\TestResults\coverage.cobertura.xml'
reportDirectory: '$(Build.SourcesDirectory)\TestResults'
You have to enable the Boards service in you Azure DevOps project to let the error disappear.