我们如何使用Cake构建对安全的NuGet服务器进行身份validation?

我们正在使用Cake Build自动化我们的构建,我们使用nuget.org的NuGet包,但我们也有自己的NuGet Feed服务器,它具有访问的用户名/密码认证。 我们如何利用带有身份validation的自定义NuGet提要服务器来使用Cake Build?

Cake利用NuGet.exe安装工具, NuGet.exe和NuGet别名。

除非您在#tool / #addin指令中指定了源或提供给NuGet别名,否则NuGet.exe将在当前路径中查找nuget.config并最终以当前用户的全局设置结束( %AppData%\NuGet\NuGet.config )。

您有几个选项,如果您不想更改Cake文件或存储库中的任何内容,那么您可以全局存储您的用户凭据, NuGet.exe将选择这些示例:

 nuget sources Update -Name [name of source] -Source [uri to your source] -UserName [your username] -Password [your password] 

免责声明某些版本的NuGet.exe和dotnet CLI存在加密密码问题,解决方法是添加-StorePasswordInClearText如下所示

 nuget sources Update -Name [name of source] -Source [uri to your source] -UserName [your username] -Password [your password] -StorePasswordInClearText 

然后,您的凭据将以纯文本格式保存,其缺点是您的凭据以纯文本格式保存。

您还可以通过为#tool / #addin指令和NuGet别名指定特定源来覆盖nuget.config设置。

#tool指令

下面是一个示例,用于说明为#tool指令提供源代码

 #tool "NUnit.ConsoleRunner" or #tool nuget:?package=NUnit.ConsoleRunner&version=3.4.0 

 #tool nuget:[source]?package=NUnit.ConsoleRunner or #tool nuget:[source]?package=NUnit.ConsoleRunner&version=3.4.0 

而对于官方V2 nuget饲料

 #tool nuget:https://www.nuget.org/api/v2?package=NUnit.ConsoleRunner or #tool nuget:https://www.nuget.org/api/v2?package=NUnit.ConsoleRunner&version=3.4.0 

#addin指令

下面是一个示例,用于说明为#addin指令提供源代码

 #addin "Cake.Slack" or #addin nuget:?package=Cake.Slack&version=0.4.0 

 #addin nuget:[source]?package=Cake.Slack or #addin nuget:[source]?package=Cake.Slack&version=0.4.0 

而对于官方V2 nuget饲料

 #addin nuget:https://www.nuget.org/api/v2?package=Cake.Slack or #addin nuget:https://www.nuget.org/api/v2?package=Cake.Slack&version=0.4.0 

NuGet别名

NuGet 别名有像NuGetAddSource和NuGetHasSource这样的命令直接使用源代码,如果您希望在NuGet恢复步骤之前向CI添加源代码,这些内容非常好:

 var source = new { Name = EnvironmentVariable("PRIVATE_FEED_NAME"), Source = EnvironmentVariable("PRIVATE_FEED_SOURCE"), ApiUserName = EnvironmentVariable("PRIVATE_FEED_USERNAME"), ApiKey = EnvironmentVariable("PRIVATE_FEED_PASSWORD") }; if (!NuGetHasSource(source.SourceUrl)) { NuGetAddSource( source.Name, source.SourceUrl, new NuGetSourcesSettings { UserName = source.ApiUserName, Password = source.ApiKey } ); } 

以上只会为您现有的nuget.config添加源nuget.config ,但您也可以覆盖NuGetInstall和NuGetRestore别名的NuGet源代码。

NuGetInstall

NuGetInstall别名具有带有NuGetInstallSettings工具设置类的重载,该类具有Source属性,您可以使用该属性覆盖使用哪些Feed,例如:

 NuGetInstall("MyNugetPackage", new NuGetInstallSettings { Source = new []{ "https://api.nuget.org/v3/index.json" } }); 

NuGetRestore

类似地,NuGetRestore别名具有重载,允许您指定具有Source属性的NuGetRestoreSettings ,您可以使用该属性覆盖使用哪些Feed,例如:

 var solutions = GetFiles("./**/*.sln"); // Restore all NuGet packages. foreach(var solution in solutions) { Information("Restoring {0}", solution); NuGetRestore( solution, new NuGetRestoreSettings { Source = new []{ "https://api.nuget.org/v3/index.json" } } ); } 

结论

有几种方法可以解决您的问题。

此外,您可以通过在计算机上配置多个源时指定源来改进NuGet还原/安装性能,但当前项目仅使用官方项目,然后通过所有已配置的源跳过查找并直接进入

但是,如果您的Feed具有身份validation,那么您将需要为使用nuget.exe或NuGetAddSource别名的人添加凭据。

对于那些使用MyGet的人来说 ,它有预先validation的 url:你可以在不添加源的情况下使用,但只是为Restore / Install指定Source属性,这是敏感信息,所以不要将它们存储在构建脚本中而是作为即环境变量