Azure - Publish Web API - Compile - azure

I'm trying to use a DevOps pipeline to deploy a Web API. I've got it working, looks great.
However, when the files are copied for the artifact, all of the source files are there too. When I use Visual Studio to "Publish" my Web API, there are minimal files, single Web.config (not Web.Release.config, Web.Debug.config)
Is there a way I can achieve this or, do I run a clean-up script to bin off the files I don't want there.
Example - published with Visual Studio:
Example - published with Azure:
Edit:
Created task group which goes through and deletes the unnecessary files and directories - not sure this is the correct approach but it does work.

As you know, First of all you should publish your source on Azure
e.g.
- task: UseDotNet#2
inputs:
packageType: 'sdk'
version: '3.1.x'
- script: dotnet publish --self-contained -r win-x64
displayName: 'dotnet build $(buildConfiguration)'
Then use your published files for your artifact.
e.g.
bin/Debug/netcoreapp3.1/win-x64/publish
You don't need to remove anything for this purpose.

Related

.Net Core Seleniun Tests in Azure DevOps- No Files matched the search pattern error for .Net restore task

Created a .Net core Selenium tests in Azure Repos.
I have the .csproj as well in the Repos.
Added a ASP.Net core task, in the restore task i have given Path to Project as "**/*.csproj".
Got the below error while running the build pipeline.
SYSTEMVSSCONNECTION exists true
SYSTEMVSSCONNECTION exists true
##[error]No files matched the search pattern
Not sure if the Agent didnt find my .csproj file. Any help is deeply appreciated.
I tried to reproduce your issue, but the task ran successfully.
My environment:
Agent: ubuntu-latest
.Net SDK Version: 3.1.403
My script:
- task: DotNetCoreCLI#2
inputs:
command: 'restore'
projects: '**/*.csproj'
feedsToUse: 'select'
You can do the following things to locate your issue:
Rerun the pipeline using my environment configuration.
Write the full and detailed path name. For example, $(System.defaultWorkingDirectory)/WindowsApp.csproj.
Add a command line task to run dotnet restore directly.
Set the system.debug variable to true. Rerun your pipeline and you will see a more detailed run log.

Azure pipeline task DotNetCoreCLI#2 - output folder of artifact

In our .NET Web API project, we tried to build API project in Azure DevOps and publish the artifact to a folder with the pipeline task below:
- task: DotNetCoreCLI#2
displayName: Publish web API artifact
inputs:
command: publish
publishWebProjects: false
arguments: '$(Build.SourcesDirectory)\XYZ.Research.API\XYZ.Research.API.csproj --configuration $(BuildConfiguration) --output testpath'
zipAfterPublish: true
modifyOutputPath: true
But I am not sure which folder the artifact is kept. Below is the log from this step:
2020-07-31T12:04:23.6282186Z ##[section]Starting: Publish web API artifact
2020-07-31T12:04:23.6590490Z ==============================================================================
2020-07-31T12:04:23.6591051Z Task : .NET Core
2020-07-31T12:04:23.6591393Z Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command
2020-07-31T12:04:23.6591740Z Version : 2.172.2
2020-07-31T12:04:23.6591974Z Author : Microsoft Corporation
2020-07-31T12:04:23.6592357Z Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
2020-07-31T12:04:23.6592942Z ==============================================================================
2020-07-31T12:04:25.5581194Z [command]C:\windows\system32\chcp.com 65001
2020-07-31T12:04:25.5581889Z Active code page: 65001
2020-07-31T12:04:25.5583746Z Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
2020-07-31T12:04:25.5588792Z [command]C:\hostedtoolcache\windows\dotnet\dotnet.exe publish d:\a\1\s\XYZ.Research.API\XYZ.Research.API.csproj --configuration Release --output testpath
.....
some warning message ignored
.....
2020-07-31T12:04:38.0843543Z XYZ.Research.API -> d:\a\1\s\XYZ.Research.API\bin\Release\netcoreapp3.0\XYZ.Research.API.dll
2020-07-31T12:04:38.9127845Z XYZ.Research.API -> d:\a\1\s\testpath\
2020-07-31T12:04:46.0295716Z Info: Azure Pipelines hosted agents have been updated to contain .Net Core 3.x (3.1) SDK/Runtime along with 2.1. Unless you have locked down a SDK version for your project(s), 3.x SDK might be picked up which might have breaking behavior as compared to previous versions.
2020-07-31T12:04:46.0296632Z Some commonly encountered changes are:
2020-07-31T12:04:46.0297619Z If you're using `Publish` command with -o or --Output argument, you will see that the output folder is now being created at root directory rather than Project File's directory. 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
2020-07-31T12:04:46.0442329Z ##[section]Finishing: Publish web API artifact
Because we will need the file location in next step (deployment), I tried
d:\a\1\s\testpath\XYZ.Reserch.API.zip
d:\a\1\s\testpath\XYZ.Reserch.API\XYZ.Reserch.API.zip
but none of these location has the artifact file.
Did anyone see this issue before? Any help would be appreciated.
------------------- update -------------------------------
As #Source Code suggested, I used task "PowerShell#2" and find that the artifact file are actually in "D:\a\1\s\testpath\testpath.zip". That means the 'testpath' sub-folder are created in $(Build.SourceDirectory) and the artifact file are also renamed to 'test.zip'.
I would recommend that you add a PowerShell/Bash/Cmd task after your DotNetCoreCLI#2 task and run a inline script with the 'ls' command that should list all the items to the results for you. This will allow you to see what is actually there after the task.
If on a Windows agent:
- task: PowerShell#2
displayName: List Files Post Publish
inputs:
targetType: inline
script: Get-ChildItem
If on Linux or Mac
- task: Bash#3
displayName: List Files Post Publish
inputs:
targetType: inline
script: ls
Additionally I noticed you're providing your csproj file via the arguments parameter. There is a parameter named projects which should be used for this. Also you may consider using the the artifacts staging directory as your output directory. The task would look like this:
- task: DotNetCoreCLI#2
displayName: Publish web API artifact
inputs:
command: publish
projects: '$(Build.SourcesDirectory)\XYZ.Research.API\XYZ.Research.API.csproj'
publishWebProjects: false
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: true
modifyOutputPath: true
One important thing to note is if you do change the output directory ensure that you change the working directory for the PowerShell or Bash tasks so you output the contents of the correct directory. It defaults to the $(Build.SourcesDirectory) so ensure that you change this if needed.

Azure DevOps: How to copy VS Profile settings into a yml script

I'm setting up a pipeline on Azure DevOps. Previously, I used to build and publish in VS.
I created a yml file using the Azure Functions template and added lines to deploy to a slot. It looks like the release configuration is also set up. How would I be able to declare my target framework, deployment mode, and target runtime in the yml? I also need to enable ReadyToRun and removing additional files at destination.
We can use the field Arguments to configure the VS profile setting in the yaml build.
Sample:
- task: DotNetCoreCLI#2
displayName: 'Build project'
inputs:
projects: '**/*.csproj'
arguments: '--configuration Release --framework <framework> --runtime <runtime> '
Enable ReadyToRun.
Add task .NET Core->change the command to publish and add the Arguments --PublishReadyToRun true
Removing additional files at destination.
When using the Azure App Service Deploy task, and you are using the Publish using Web Deploy option, there is an additional option to Remove Additional Files at Destination.
Steps:
Additional Deployment Options->enable select deployment method->enable the option Remove additional files at destination.
Update1

Fail to execute "dotnet tool restore --interactive" in Build Pipeline when using Azure DevOps Artifact

I'm using Azure DevOps and would like to run dotnet tool restore --interactive in my Build Pipeline. In my code repo, there is nuget.config as follows. The file has a private feed hosted in Azure Artifact in addition to nuget.org.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="MyTools" value="https://mytools.pkgs.visualstudio.com/MyBot/_packaging/MyTools/nuget/v3/index.json" />
</packageSources>
</configuration>
In the private feed in Azure Artifact, my private dotnet tools exist. To retrieve the tool I created following yml file.
pool:
vmImage: "windows-latest"
demands:
- msbuild
- visualstudio
- vstest
variables:
buildPlatform: "Any CPU"
buildConfiguration: "Release"
steps:
- task: PowerShell#2
inputs:
targetType: inline
script: "Invoke-WebRequest -Uri https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1 -OutFile ./installcredprovider.ps1 ;./installcredprovider.ps1;"
- task: DotNetCoreCLI#2
displayName: "Restore tools"
inputs:
command: custom
custom: tool
arguments: restore --interactive
However, as I run the yml file in the build process, following result appears. As the message shows, it requires sign-in, but I do not want to do device sign-in every time. So Is there any good way so that I do not have to do device log-in every time?
##[section]Starting: Restore tools
==============================================================================
Task : .NET Core
Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command
Version : 2.162.0
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
[command]C:\windows\system32\chcp.com 65001
Active code page: 65001
[command]"C:\Program Files\dotnet\dotnet.exe" tool restore --interactive
Welcome to .NET Core 3.1!
---------------------
SDK Version: 3.1.100
Telemetry
---------
The .NET Core tools collect usage data in order to help us improve your experience. The data is anonymous. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.
Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry
----------------
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Find out what's new: https://aka.ms/dotnet-whats-new
Learn about the installed HTTPS developer cert: https://aka.ms/aspnet-core-https
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs
Write your first app: https://aka.ms/first-net-core-app
--------------------------------------------------------------------------------------
[CredentialProvider]DeviceFlow: https://mytools.pkgs.visualstudio.com/MyBot/_packaging/MyTools/nuget/v3/index.json
[CredentialProvider]ATTENTION: User interaction required.
**********************************************************************
To sign in, use a web browser to open the page https://microsoft.com/devicelogin to authenticate.
**********************************************************************
C:\Program Files\dotnet\sdk\3.1.100\NuGet.targets(123,5): error : [CredentialProvider]Device flow authentication failed. User was presented with device flow, but didn't react within 90 seconds. [C:\Users\VssAdministrator\AppData\Local\Temp\wk4fkuq1.hqu\restore.csproj]
C:\Program Files\dotnet\sdk\3.1.100\NuGet.targets(123,5): error : Unable to load the service index for source https://mytools.pkgs.visualstudio.com/MyBot/_packaging/MyTools/nuget/v3/index.json. [C:\Users\VssAdministrator\AppData\Local\Temp\wk4fkuq1.hqu\restore.csproj]
C:\Program Files\dotnet\sdk\3.1.100\NuGet.targets(123,5): error : Response status code does not indicate success: 401 (Unauthorized). [C:\Users\VssAdministrator\AppData\Local\Temp\wk4fkuq1.hqu\restore.csproj]
Restore completed in 775.06 ms.
Tool 'dotnet-ef' (version '3.1.1') was restored. Available commands: dotnet-ef
Package "localenv" failed to restore, due to Microsoft.DotNet.ToolPackage.ToolPackageException: The tool package could not be restored.
at Microsoft.DotNet.Tools.Tool.Install.ProjectRestorer.Restore(FilePath project, PackageLocation packageLocation, String verbosity)
at Microsoft.DotNet.ToolPackage.ToolPackageInstaller.InstallPackageToExternalManagedLocation(PackageLocation packageLocation, PackageId packageId, VersionRange versionRange, String targetFramework, String verbosity)
at Microsoft.DotNet.Tools.Tool.Restore.ToolRestoreCommand.InstallPackages(ToolManifestPackage package, Nullable`1 configFile)
Restore partially failed.
##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
Info: Azure Pipelines hosted agents have been updated to contain .Net Core 3.x SDK/Runtime along with 2.2 & 2.1. Unless you have locked down a SDK version for your project(s), 3.x SDK might be picked up which might have breaking behavior as compared to previous versions.
Some commonly encountered changes are:
If you're using `Publish` command with -o or --Output argument, you will see that the output folder is now being created at root directory rather than Project File's directory. 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 :
##[section]Finishing: Restore tools! (Including efcore tools)
Fail to execute “dotnet tool restore --interactive” in Build Pipeline when using Azure DevOps Artifact
You could not use the command line dotnet tool restore --interactive on the hosted agent, that because:
Microsoft decided not to put the plugin artifacts-credprovider in dotnet.exe because there
were concerns that shipping auth integration to Azure DevOps within
the sdk would've been unfair to other feed providers. To resolve your
401 issues you need to install the auth plugin manually.
That the reason why you get the 401 (Unauthorized) error.
In addition, you mentioned that you do not want to commit the token info in the nuget.config file, as a solution for this issue, you could add a nuget Service connections with PAT:
Then use this externalFeedCredentials: nugettest in the dotnet restore task instead of powershell task (there is no option to accept the certification Information):
- task: DotNetCoreCLI#2
displayName: Restore
inputs:
command: restore
projects: path/to/xxx.csproj
feedsToUse: config
nugetConfigPath: path/to/NuGet.Config
externalFeedCredentials: nugettest
Then we could resoter the package from our private feed.
If you get the 403 403 (Forbidden) error with above method, you can check the thread here.
Hope this helps.
You can't use the flag --interactive in a build pipeline, because this flag except to get an output from the user during the execution.
You can use the default restore option in the .Net core task:
- task: DotNetCoreCLI#2
inputs:
command: 'restore'
projects: '$(Build.SourcesDirecotry)/path/to/csproj'
feedToUse: 'config'
nugetConfigPath: '$(Build.SourcesDirectory)/path/to/nuget.config'
This one is because you should let agent know the nuget sources with pre-authenticated feed url prior to dotnet restore:
- task: NuGetCommand#2
displayName: 'Add Nuget Feed'
inputs:
command: custom
arguments: 'sources add -name <name_of_source> -source <feed_url>'
I hope this would be useful.

Azure Functions: how to deploy a project downloaded from the Azure portal?

I have implemented a C# function in the Azure Portal.
I downloaded the content of the Function App as a Visual Studio project.
I now want to deploy it with Azure DevOps release pipeline.
The deployment works fine but my function is not deployed. Sounds like the .csx file is not taken into account.
I tried to change the Build Action of the csx file to C# Compiler but it failed during the compilation with missing dependencies. Not sure it is the right way to proceed.
What are the steps to deploy the Azure functions?
It seems a little bit overkill to deploy csx functions with AzureDevOps. We could simply zip those files in the same folder structure as they were downloaded, go to https://<functionappname>.scm.azurewebsites.net/ZipDeployUI and drag the zip file to deploy. If you prefer command line instead of manual deployment, check Azure CLI command.
If you have to work with AzureDevOps, have a look at Build pipeline YAML below(Release pipeline is the default template Deploy a function app to Azure Functions).
resources:
- repo: self
queue:
name: Hosted VS2017
steps:
- task: ArchiveFiles#2
displayName: 'Archive $(system.defaultworkingdirectory)'
inputs:
rootFolderOrFile: '$(system.defaultworkingdirectory)'
includeRootFolder: false
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: drop'
Note that the <functionName>.csproj file downloaded is useless and shouldn't be commited to repo. But if we have installed some extensions and see extensions.csproj file, add Net Core build task before ArchiveFiles#2.
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '$(Parameters.RestoreBuildProjects)'
arguments: '--configuration release -o bin --no-incremental'

Resources