Azure Pipeline中NuGet pack任务未使用Azure Artifacts源的问题
解决Azure Pipeline中NuGet Pack无法使用Azure Artifacts源的问题
问题场景
执行NuGet pack命令时出现以下错误:
error NU1101: Unable to find package Repositories.Abstractions. No packages exist with this id in source(s): C:\Program Files\dotnet\library-packs, Microsoft Visual Studio Offline Packages, nuget.org
当前Pack任务配置:
- task: DotNetCoreCLI@2 displayName: Create NuGet Package inputs: command: 'pack' packagesToPack: '**/${{ parameters.nuGetPackageToBuild }}.csproj' versioningScheme: 'byBuildNumber' feedsToUse: 'select' vstsFeed: 'd4d1f2ae-4068-405e-93b1-etc. etc. etc. etc.'
经确认,该Pack任务未使用指定的Azure Artifacts源,仅调用了nuget.org和本地离线源;但同一管道中的Restore任务可正常使用Azure Artifacts源:
- task: NuGetCommand@2 name: 'NuGetRestore' displayName: 'NuGet Restore' inputs: command: 'restore' restoreSolution: '**/Osprey.NuGet.sln' feedsToUse: 'select' vstsFeed: 'd4d1f2ae-4068-405e-93b1-etc. etc. etc. etc.'
解决方案
由于feedsToUse和vstsFeed参数仅对Restore命令生效,Pack任务需通过以下方式指定Azure Artifacts源:
方案一:提前配置NuGet源
在Pack任务前添加步骤,将Azure Artifacts源加入NuGet配置:
- task: NuGetCommand@2 displayName: '添加Azure Artifacts源' inputs: command: 'custom' arguments: 'sources add -n "MyAzureFeed" -s "https://pkgs.dev.azure.com/[你的组织名]/[你的项目名]/_packaging/[你的Feed名]/nuget/v3/index.json" -u "任意值" -p "$(System.AccessToken)" -StorePasswordInClearText'
然后修改Pack任务,移除无效参数:
- task: DotNetCoreCLI@2 displayName: Create NuGet Package inputs: command: 'pack' packagesToPack: '**/${{ parameters.nuGetPackageToBuild }}.csproj' versioningScheme: 'byBuildNumber'
方案二:使用NuGetCommand@2执行Pack命令
改用NuGetCommand@2任务执行pack,并通过arguments直接指定源:
- task: NuGetCommand@2 displayName: Create NuGet Package inputs: command: 'pack' packagesToPack: '**/${{ parameters.nuGetPackageToBuild }}.csproj' versioningScheme: 'byBuildNumber' arguments: '-Source "https://pkgs.dev.azure.com/[你的组织名]/[你的项目名]/_packaging/[你的Feed名]/nuget/v3/index.json" -Source nuget.org'
注意事项
- 替换命令中的
[你的组织名]、[你的项目名]、[你的Feed名]为实际信息 $(System.AccessToken)是Pipeline内置变量,需确保当前管道拥有Azure Artifacts Feed的读取权限
内容的提问来源于stack exchange,提问作者GSFatPracctice
相关产品推荐
相关产品推荐

