0
votes

Im trying to pack a web api i made in .net core 3.1 with Azure Pipeline.

  - task: DotNetCoreCLI@2
displayName: Package NuGet
inputs: 
  command: 'pack'
  projects: '**/*.csproj'
  arguments: '--configuration $(BuildConfiguration)'
  outputDir: '$(Build.ArtifactStagingDirectory)/packages'

Thats the task i used, i found it from another post on stack overflow.

My only issue with that is that it gives a number of .nupkg files, instead of one, and that the web api packages doesn't have the dependencies dll.

Also i created a .nuspec file, but i don't seem to manage to use it correctly with Azure Pipeline I've tried what's explained here : https://docs.microsoft.com/fr-fr/nuget/reference/msbuild-targets#packing-using-a-nuspec

or just by targeting the .nuspec file like it is explained on the tooltipe of pipeline

enter image description here

If anybody could put me on the right path that would be greatly appreciated :)

EDIT: I want to pack everything in a nupkg to then deploy it for an IIS site.

This is my nuspec file :

   <?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>WebAPI</id>
        <version>1.0.0</version>
        <authors>Kevin Pinero</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>WebAPI</description>
    </metadata>
    <files>
        <file src=".\bin\Release\*\*.dll" target="lib" />
        <file src=".\bin\Release\*\*.exe" target="lib" />
        <file src=".\bin\Release\*\*.json" target="lib" />
       <!-- <file src=".\bin\Release\*\Properties\*.json" target="lib" /> -->
        <file src=".\bin\Release\*\*.pdb" target="lib" />
        <file src=".\bin\Release\*\*.config" target="lib" />
    </files>
</package>

And this the error i get on the pipeline :

error MSB4068: The element is unrecognized, or not supported in this context.

Using this task :

  - task: DotNetCoreCLI@2
    inputs:
      command: 'pack'
      packagesToPack: '**/*API.nuspec'
      nobuild: true
      versioningScheme: 'off'

I know on microsoft website I could potentially use this command too

dotnet pack ~/projects/app1/project.csproj -p:NuspecFile=~/projects/app1/project.nuspec -p:NuspecBasePath=~/projects/app1/nuget

But i'm not sure how to translate it in AZ pipeline ...

EDIT Solution adopted:

I've decide to resolve my issues this way :

  - task: DotNetCoreCLI@2
    displayName: Build at solution level
    inputs:
      command: 'build'
      projects: $(solution)
      arguments: '--no-restore --configuration $(buildConfiguration)'

  - task: DotNetCoreCLI@2
    displayName: Execute tests
    inputs:
      command: 'test'
      projects: $(testProjects)
      arguments: '--no-build --configuration $(buildConfiguration)'

  - task: DotNetCoreCLI@2
    displayName: Pack project
    inputs:
      command: publish
      projects: '**/projectName.csproj'
      publishWebProjects: False
      arguments: '--no-build --configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)
      zipAfterPublish: False

  - task: PublishBuildArtifacts@1
    displayName: Publish 
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)' 
      artifactName: 'artefact name'
3
What are you trying to accomplish by packing your API project in a .nupkg?Josh Gust
What is your issue when trying to use the .nuspec file? Please edit your question to provide details about what it means for you to "not manage to use it correctly" so that your issue in this area might be resolved.Josh Gust
You may need to make some edits in WebApi.csproj file as workarounds. So that dotnet pack will pick up the dependency assemblies and pack them together into package. You can try the two links in the end of my answer, if you're confused about how to do that and need more details, feel free to let me know .LoLance
Hi friend, is there any update for this issue? Just checking in to see if you need any further help:)LoLance

3 Answers

0
votes

I would recommend to use the Azure DevOps online pipeline editor. It is great to use and get up to speed (auto-completion, syntaxic review, direct commit/push).

What you try to achieve can be done with the steps:

  • dotnet build
  • dotnet pack -> specify the right projects to be packed here
- task: DotNetCoreCLI@2
displayName: 'Create packed NuGet files'
inputs:
  command: 'pack'
  packagesToPack: '**/*Api.csproj;!**/*Tests.csproj'
  versioningScheme: 'off'
  • nuget push

No need for a nuspec file as long as you add some fields in your csproj.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <AssemblyName>Withywoods.Selenium</AssemblyName>
    <RootNamespace>Withywoods.Selenium</RootNamespace>
    <ProjectGuid>{08D9DDB8-BF5B-4E45-8E0C-D9AC85ABF020}</ProjectGuid>
    <Authors>devprofr</Authors>
    <Description>Library to ease the use of Selenium web driver and provide testing best practices.</Description>
    <RepositoryUrl>https://github.com/devpro/withywoods</RepositoryUrl>
    <PackageProjectUrl>https://github.com/devpro/withywoods</PackageProjectUrl>
  </PropertyGroup>

I have a short example here if you want: pkg.yml

.NET Core and Azure DevOps is a great combination, feel free to comment if you have any issues!

(On my side, I pack library projects but not Api, I do dotnet publish on Api to use them as artifact in release pipelines.)

0
votes

My only issue with that is that it gives a number of .nupkg files, instead of one, and that the web api packages doesn't have the dependencies dll.

I assume you have more than one projects in same solution. Let's call the Web Api project A, other projects B, C and D...

1.So if you only want to pack A into nuget package, instead of using projects: '**/*.csproj', we can use something like projects: '**/A.csproj'. Then it won't pack a number of nuget packages any more.

2.For the dependencies dll, I think you mean the project references like Josh Gust said above. For this, it's still one open issue about dotnet pack command. You can track that issue there to get notifications if there's any update.

For now, we have to make changes to A.csproj to use msbuild magics as workaround. You can try Martin's or zvirja's workarounds there. In my opinion, they both helps for your issue.

Hope all above helps :)

Update1:

For your edit, if you want to translate it in AZ pipeline, you can use the custom command to call the pack. Something like this:

- task: DotNetCoreCLI@2
  displayName: 'dotnet custom'
  inputs:
    command: custom
    projects: '**/ProjectName.csproj'
    custom: pack
    arguments: '-p:NuspecFile=~/projects/app1/project.nuspec -p:NuspecBasePath=~/projects/app1/nuget'

You can check the log to confirm it actually ran the command: "C:\Program Files\dotnet\dotnet.exe" pack D:\a\1\s\xxx\xxx.csproj -p:NuspecFile=~/projects/app1/project.nuspec -p:NuspecBasePath=~/projects/app1/nuget. Hope that's what you need.

And I'm not certainly sure that we can use ~ in that command, but if it works locally, then you can translate it in Azure Devops Pipeline using my way...

0
votes

UPDATE



Because you are trying to package a Web API project for delivery to IIS, you should stop trying to use nuget as that mechanism, and instead use the dotnet publish command. If you want to continue to use the DotNetCoreCLI@2 task (as opposed to using the script shortcut to call the dotnet cli directly), then I would point you to the documentation to Build, Test and Deploy .Net Core Apps.

This documentation is not written specifically for Web API projects, but is a set of generic guidelines for operating with .Net Core Apps is Azure DevOps Pipelines. An example is quoted below (emphasis mine):

After you've built and tested your app (not web api specifically), you can upload the build output to Azure Pipelines or TFS, create and publish a NuGet package, or package the build output into a .zip file to be deployed to a web application.

When you read in the Package and Deliver your Code section that publish to a NuGet feed is a valid option for delivering code, it is. However this method of delivery should be used for library type deliverables. The dotnet publish command is what is designed to package a web api project and all its dependencies into a .zip (or folder if you specify that option) in preparation for Web Deploy commands against an IIS instance.



Original Answer



Without getting into the details of why you want to create a .nupkg from your API project.

You mention:

My only issue with that is that it gives a number of .nupkg files, instead of one, and that the web api packages doesn't have the dependencies dll

This has been the MO of dotnet pack for a while now as regards project-to-project (P2P) references.

The documentation for dotnet pack states this behavior.

NuGet dependencies of the packed project are added to the .nuspec file, so they're properly resolved when the package is installed. Project-to-project references aren't packaged inside the project. Currently, you must have a package per project if you have project-to-project dependencies.

If you want to have more control over the files that are included in your .nupkg then you will want to author a .nuspec file manually and provide it to the dotnet pack command as indicated in the last Example on the documentation page.

Using the Azure DevOps task for the dotnet core cli DotNetCoreCLI@2 should allow you to simply put the path to the .nuspec file in the input. More information will be necessary in your question if this isn't working for you.