在Azure DevOps中,管道可以用来构建解决方案,O(∩_∩)O哈哈~快万能了,本章主要介绍如何创建Nuget包并且将其发布到Nuget服务器的过程。 file

前面我创建了一个非常简单的类库,这边我不做过多叙述,接下来我们需要进行编辑csproj文件,当我们创建Nuget包时,我们将使用dotnet pack命令。这于传统的Nuget cli稍微有点不同,在传统的Nuget CLI中,我们创建nuspec文件并针对nuspec运行nuget packdotnet pack命令将从csproj创建一个nuspec文件,然后将代码打包到一个nupkg文件中,需要将一些关键信息添加到csproj文件中,以确保它可以正确的创建Nuget包。首先我们需要一个PackageId,这将是Nuget包本身的名称,根据我们要发布的位置这个名称必须是唯一的,接下来是Version,它将是已发布的软件包的版本号,正如下所示是我们的csproj文件

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <PackageId>AzureDevOpsTest.Common</PackageId>
    <Version>1.0.0</Version>
    <Authors>HueiFeng</Authors>
    <Description>Test project of common utils</Description>
    <IsPackable>true</IsPackable>
  </PropertyGroup>

</Project>

我们需要在Azure DevOps中创建项目,并进行git仓库的绑定,当绑定完之后我们先来创建相关的服务账号信息,步骤如下所示:

file

file

file

构建项目(dotnet build)

- task: DotNetCoreCLI@2
  displayName: 'dotnet build'
  inputs:
    command: 'build'
    arguments: '--configuration $(buildConfiguration)'

打包项目(dotnet pack)

使用nobuild意味着在运行pack之前不会编译项目,因为它已经在上面的步骤中构建了

- task: DotNetCoreCLI@2
  displayName: "dotnet pack"
  inputs:
    command: 'pack'
    arguments: '--configuration $(buildConfiguration)'
    packagesToPack: '**/*.csproj'
    nobuild: true
    versioningScheme: 'off'

发布项目(nuget push)

如下所示这种方式适合在Azure DevOps组织内的制品库,当然在制品库中可以设置可见性,我们可以设置对该程序包的公开和私有。allowPackageConflicts:true代表在版本重复的情况下,可以进行跳过重复的版本,避免返回409。但是这种方式在Nuget.exe中无法进行使用,这将避免不了返回409影响我们流水线的正常性。我们只能进行选择通过dotnet nuget push的方式进行忽略并发布,下面第二个代码片段所写。

对于这个问题其实我寻求过答案,但是团队这边给出的是:

I have discussed this with the team and we have decided not to add this functionality to the task. Instead of using these "bulkier" tasks we now recommend using the NuGet Authenticate task to authenticate to Azure DevOps Artifacts feeds and to use a script task with nuget/dotnet to use the parameters you need.
The reason for this is that the NuGet/Dotnet clients are actively being developed and new parameters and functionality are being added often. To keep up with all of the updates causes a lot of extra support for these bulky tasks. Please use example above to set up your pipeline with the '--skip-duplicate' parameter. Thank you for understanding.

这是痛苦的这将代表我们无法通过如下这种很简单的方式不得不选择通过dotnet nuget push进行发布,不太清真了。。。

- task: NuGetCommand@2
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'external'
    publishFeedCredentials: 'hueifeng_nuget'
    allowPackageConflicts: true
- task: DotNetCoreCLI@2
  displayName: "dotnet push"
  inputs:
    command: 'custom'
    custom: 'dotnet nuget push $(Build.ArtifactStagingDirectory)/**/*.nupkg -k $(APIKey) -s https://api.nuget.org/v3/index.json  --skip-duplicate'

Reference

https://github.com/hueifeng/AzureDevOpsDemo/tree/demo03