Setup of azure-pipelines.yml "No web project was found in the repository" with ASP.NET Core - 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.)

Related

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)

DevOps IIS Release Pipeline Not Extracting Files To Correct Location

I am trying to setup a Release Pipeline for an ASPNET Core application to an on-premises IIS Virtual Machine (VM).
Everything is working as expected except when it deploys the files to the IIS server, but it's not extracting the files and it's dropping the zip file in the wrong folder location. For example, the application files are supposed to be deployed to %SystemDrive%\sites\{mysite} - This is setup in the Release Pipeline under the IIS Web App Manage section (shown below).
On the server, this is deploying to the correct location, but instead of all the app files being deployed to the \sites\{mysite} it's creating two additional folders and then dropping a zip file called "WebApp.zip" like this, (%SystemDrive%\sites\{mysite}\{artifactName}\{artifactName}\WebApp.zip)
How do I get this to actually only deploy the website app files to the \sites\{mysite} directory?
I do have a yaml file, shown below. Any help would be greatly appreciated.
trigger:
- development
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:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest#2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: DotNetCoreCLI#2
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifactName: '{artifactName}' #removed the name for this post
- task: IISWebAppDeploymentOnMachineGroup#0
displayName: 'IIS Web App Deploy'
inputs:
WebSiteName: '{removed}' #removed the name for this post
Package: '$(System.DefaultWorkingDirectory)'
RemoveAdditionalFilesFlag: true
XmlVariableSubstitution: True
EDIT:
I've updated my yaml to look like what's shown below. If I do not include the PublishPipelineArtifact#1 task then no files ever get copied to the IIS server. This is also deploying now to a folder structure like this: {artifact}\{artifact}\(all of the files) which is an improvement, but still I don't need the two {artifact} folders, I just want the files deployed in the website's root directory.
Any help is greatly appreciated as I've spent a pretty ridiculous amount of time on this thus far.
Updated Yaml:
trigger:
- development
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: PublishPipelineArtifact#1
inputs:
targetPath: $(System.DefaultWorkingDirectory)/bin/Release/net5.0
artifact: {artifactName}
Specify the path to the *.zip package file for your IISWebAppDeploymentOnMachineGroup#0 task's Package input instead of just the default working directory.
Edit:
You have a lot going on that's confusing me and I believe it's probably because you're not sure what you're doing - forgive me if incorrect. First, you look like you're building and packaging the build results twice - with VSBuild#1 the first time, then using dotnet build and dotnet publish the second time via two DotNetCoreCLI#2 tasks. You then have a PublishPipelineArtifact#1 referencing the target of both packaging/publishing tasks.
You then attempt to deploy these packages but reference a completely different path/file. Typically a build and deployment would be split into two separate jobs and the deployment job would download the published pipeline artifact. That downloaded artifact is the path/file you want to reference in your deployment task's Package input.

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:

Azure pipeline error: Directory '/home/vsts/work/1/a' is empty. Nothing will be added to build artifact 'drop'

This is my azure-pipelines.yaml
trigger:
- dev
pool:
vmImage: 'ubuntu-latest'
variables:
solution: '**/*.sln'
steps:
- task: NuGetToolInstaller#1
- task: UseDotNet#2
displayName: 'Use dotnet sdk 3.1'
inputs:
version: 3.1.x
includePreviewVersions: false
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: DotNetCoreCLI#2
inputs:
command: 'build'
projects: '$(solution)'
outputDir: '$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts#1
inputs:
pathToPublish: '$(Build.ArtifactStagingDirectory)/**/*'
At the PublishBuildArtifacts tesk it shows the error:
[error]Publishing build artifacts failed with an error: Not found PathtoPublish: /home/vsts/work/1/a/**/*
Azure pipeline error: Directory '/home/vsts/work/1/a' is empty. Nothing will be added to build artifact 'drop'
That because there is a slight syntax error in your yaml file. You could use following dotnet build task:
- task: DotNetCoreCLI#2
inputs:
command: 'build'
projects: '$(solution)'
arguments: '--output $(Build.ArtifactStagingDirectory)'
You could check the correct syntax by classic editor:
Or you could check the document Build your project and dotnet build for some more details.
Hope this helps.
From the help for the PublishBuildArtifact step:
The folder or file path to publish. This can be a fully-qualified path or a path relative to the root of the repository. Wildcards are not supported. Variables are supported. Example: $(Build.ArtifactStagingDirectory)
You're trying to wildcard the pathToPushlish. Setting that to $(Build.ArtifactStagingDirectory) will grab the directory and all contents, preserving structure, which it looks like you want.
James is correct, if you want to publish your artifacts to the build machine just use the built-in directory, you will use that path later for your deployment, by that time if you are not sure about the artifact name you can just use wildcard to retrieve the package, if there is only 1 package.

Azure DevOps ignores the Publish of one project

On Azure DevOps Pipelines I have task to Build an Asp.Net Core application:
- task: DotNetCoreCLI#2
displayName: 'Build'
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'
With Output:
Restoring packages for /home/vsts/work/1/s/src/Api/Api.csproj...
Restoring packages for /home/vsts/work/1/s/src/Core/Core.csproj...
Restore completed in 672.27 ms for /home/vsts/work/1/s/src/Core/Core.csproj.
Restore completed in 676.4 ms for /home/vsts/work/1/s/src/Api/Api.csproj.
Core -> /home/vsts/work/1/s/src/Core/bin/Release/netcoreapp2.2/Core.dll
Api -> /home/vsts/work/1/s/src/Api/bin/Release/netcoreapp2.2/Api.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Then I added a Publish task:
- task: DotNetCoreCLI#2
displayName: 'Publish'
inputs:
command: publish
publishWebProjects: false
arguments: '--configuration $(buildConfiguration) --output $(build.artifactstagingdirectory)'
zipAfterPublish: true
With output:
Restore completed in 54.68 ms for /home/vsts/work/1/s/src/Core/Core.csproj.
Core -> /home/vsts/work/1/s/src/Core/bin/Release/netcoreapp2.2/Core.dll
Core -> /home/vsts/work/1/a/.
Both tasks run fine but task Publish does not Publish the Api project.
Any idea why?