DevOps IIS Release Pipeline Not Extracting Files To Correct Location - asp.net-core

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.

Related

Azure Devops Pipeline Azure Repos Git .Net Error in Tests Failed to run as a self-contained app

I have a .Net solution with multiple projects, including a test project.
When I create a pipeline for my solution using Azure Devops templates, with the connect Azure Repos Git and configure ASP.Net Core (.Net Framework), I get the following yml file:
# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- main
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:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
When I run the pipline and reaches the VSTest Task, after running the tests, it gives me the following error:
##[error]Testhost process for source(s) 'D:\a\1\s\Fazer.UITests\bin\Release\net6.0\Microsoft.TestPlatform.PlatformAbstractions.dll' exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
##[error]Failed to run as a self-contained app.
##[error] - The application was run as a self-contained app because 'D:\a\1\s\Fazer.UITests\bin\Release\net6.0\testhost.runtimeconfig.json' was not found.
##[error] - If this should be a framework-dependent app, add the 'D:\a\1\s\Fazer.UITests\bin\Release\net6.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
##[error]. Please check the diagnostic logs for more information.
How can I fix this?
After a while searching for the solution, I found out that all I had to do was to change the line **\*test*.dll to **\*Tests.dll for the key testAssemblyVer2 in the task VSTest#2.
Before:
- task: VSTest#2
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
--> **\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
After:
- task: VSTest#2
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
--> **\*Tests.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
Then the pipeline runned sucesssfully!

DevOps pipeline CI error when I updated .net core 3.1 to .net core 6

So I updated all the projects of my solution to .net core 6, before every single one was .net core 3.1.
Then I started to get the following error in build step:
##[error]PjInspe.Tests\PjInspe.Tests.csproj(0,0): Error NU1201:
Project PjInspe.Aplicacao is not compatible with netcoreapp3.1
(.NETCoreApp,Version=v3.1). Project PjInspe.Aplicacao supports:
net6.0 (.NETCoreApp,Version=v6.0)
But all the projects has this in the .csproj
And its the only project that is failing
Any ideas how to solve this?
EDIT:
My pipeline YAML
Iam using the windows 2022 to run the CI and I've seen the sdk from the net6 on the build logs
pool:
name: Azure Pipelines
steps:
- task: DotNetCoreCLI#2
displayName: Restore
inputs:
command: restore
projects: '**/*Project.csproj'
vstsFeed: '***************'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '$(Parameters.RestoreBuildProjects)'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI#2
displayName: Test
inputs:
command: test
projects: '$(Parameters.TestProjects)'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI#2
displayName: Publish
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)'
zipAfterPublish: True
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
The right way is supposted to be to modify you dotnetcore cli task as follows
- task: DotNetCoreCLI#2
displayName: Test
inputs:
command: test
version: '6.0.x' //better to put this in a variable
projects: '$(Parameters.TestProjects)'
arguments: '--configuration $(BuildConfiguration)'
But I still had a similar build problem. I fixed it by using the use dotnet core task BEFORE all other tasks and specifying the version I need.
This is the yaml code that I used
- task: UseDotNet#2
displayName: 'Use .NET 6 Core sdk'
inputs:
packageType: 'sdk'
version: '6.0.x'

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)

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.

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.)