首页 > 解决方案 > 如何使用 yaml 配置从 azure 管道生成特定版本的 nuget 包

问题描述

我在这里定义了一个构建配置:

https://github.com/cpoDesign/APIFramework/blob/master/azure-pipelines.yml

我设法使用以下命令生成了一个 nuget 包

- task: DotNetCoreCLI@2
  inputs:
    command: pack
    projects: '**/*ApiFramework.csproj'

脚本任务输出的一个子集是

  Task "PackTask"
2018-11-27T23:02:32.4459067Z          Successfully created package '/home/vsts/work/1/a/CPODesign.ApiFramework.1.0.0.nupkg'.

下一步解决:

我不想创建一个发布到 nuget 的构建,因为这些步骤必须在逻辑上分开。因此,我创建了一个新步骤 Create a drop。

配置:

我的放置任务定义:

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)'
    artifactName: drop
    contents: '**/$(BuildConfiguration)/**/?(*.nupkg)'

构建输出:

2018-11-27T23:04:24.6351310Z ##[section]Starting: PublishBuildArtifacts
2018-11-27T23:04:24.6353582Z ==============================================================================
2018-11-27T23:04:24.6353896Z Task         : Publish Build Artifacts
2018-11-27T23:04:24.6353944Z Description  : Publish build artifacts to Azure Pipelines/TFS or a file share
2018-11-27T23:04:24.6354007Z Version      : 1.142.2
2018-11-27T23:04:24.6354046Z Author       : Microsoft Corporation
2018-11-27T23:04:24.6354091Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=708390)
2018-11-27T23:04:24.6354156Z ==============================================================================
2018-11-27T23:04:26.1357631Z ##[section]Async Command Start: Upload Artifact
2018-11-27T23:04:26.1357755Z Uploading 1 files
2018-11-27T23:04:26.6373194Z File upload succeed.
2018-11-27T23:04:26.6373313Z Upload '/home/vsts/work/1/a' to file container: '#/1558454/drop'
2018-11-27T23:04:27.9231805Z Associated artifact 91 with build 806
2018-11-27T23:04:27.9231947Z ##[section]Async Command End: Upload Artifact
2018-11-27T23:04:27.9232436Z ##[section]Finishing: PublishBuildArtifacts

注意: azure-devops 的 UI 已更改,工件(工件)不再创建为新选项卡,而是严重添加到报告摘要中

问题:

如何生成特定版本的 nuget 包 IE: 1.0.%(Build.BuildId)?

我的最后一次尝试是

- task: DotNetCoreCLI@2
  inputs:
    command: pack
    projects: '**/*ApiFramework.csproj'
   # packageVersion:'1.0.$(Build.BuildId)'

在哪里

   packageVersion:'1.0.$(Build.BuildId)'

会导致构建失败(当前分支发布在这里:https ://github.com/cpoDesign/APIFramework/blob/cpoDesign-build-mods-1/azure-pipelines.yml )

标签: azure-devopsnuget-packageazure-pipelines

解决方案


在令人沮丧的几个小时之后,我找到了答案

  1. 将生成的 nuget 包发布到构建工件中
  2. 生成发布以发布 nuget 包

构建配置

pool:
  vmImage: 'Ubuntu 16.04'

variables:
  buildConfiguration: 'Release'

steps:
- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
    command: restore
    projects: '**/*.csproj'

- script: dotnet test **/*.Tests.Unit.csproj --logger trx
- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'

- script: dotnet pack /p:PackageVersion='1.0.$(Build.BuildId)' --configuration $(buildConfiguration)  -o $(Build.ArtifactStagingDirectory)

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)'
    artifactName: drop
    contents: '**/$(BuildConfiguration)/**/?(*.nupkg)'

发布部分

我已更新版本以在每次成功构建后触发

dotnet nuget push artefactName.APIFramework/drop/CPODesign.ApiFramework.1.0.$(Build.BuildId).nupkg -k $(myapiKey) -s https://api.nuget.org/v3/index.json

推荐阅读