我们如何使用Cake build对安全的NuGet服务器进行身份验证? [英] How do we authenticate against a secured NuGet server with Cake build?

查看:96
本文介绍了我们如何使用Cake build对安全的NuGet服务器进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Cake Build自动化构建,我们使用nuget.org的NuGet软件包,但我们也有自己的NuGet Feed服务器,该服务器具有用户名/密码身份验证.我们如何将Cake Build与具有身份验证的自定义NuGet提要服务器一起使用?

We are working on automating our builds using Cake Build and we use NuGet packages from nuget.org but we also have our own NuGet Feed server which has a username/password authentication to access. How do we utilize Cake Build with a custom NuGet feed server with authentication?

推荐答案

Cake利用NuGet.exe来安装工具,插件和NuGet别名.

Cake utilizes the NuGet.exe for installing tools, addins and the NuGet aliases.

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

Unless you have a source specified in the #tool/#addin directives or provided to the NuGet aliases, then NuGet.exe will look for nuget.config in current path and eventually end up at current users global settings (%AppData%\NuGet\NuGet.config).

您有两种选择,如果您不想更改Cake文件或存储库中的任何内容,则可以为您的用户全局存储凭据,并且NuGet.exe将选择以下示例:

You have a couple of options, if you don't want to change anything in Cake files or your repository, then you can store your credentials for your user globally and NuGet.exe will pick these up example:

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

免责声明NuGet.exe和dotnet CLI的某些版本在加密密码方面存在问题,一种解决方法是添加-StorePasswordInClearText这样的:

disclaimer some versions of NuGet.exe and dotnet CLI have issues with encrypted passwords, an workaround for this is adding the -StorePasswordInClearText like this:

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

然后,您的凭据以纯文本格式保存,这与您的凭据以纯文本格式保存的缺点相同.

Then your credentials are saved in plain text, which works with the drawback that your credentials are saved in plain text.

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

You can also override nuget.config settings by specifying a specific source for #tool/#addin directives and NuGet aliases.

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

Below is an example to illustrate providing a source for the #tool directive

#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 feed

and i.e. for the official V2 nuget feed

#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 directive

Below is an example to illustrate providing a source for the #addin directive

#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 feed

and i.e. for the official V2 nuget feed

#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 别名具有类似

NuGet aliases

The NuGet aliases have commands like NuGetAddSource and NuGetHasSource for working directly with sources, these are great if you for example wish to add sources to a CI before a NuGet restore step like below:

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中,但是您也可以覆盖 NuGetRestore 别名.

The above will just add sources to your existing nuget.config, but you can also override the NuGet source for the NuGetInstall & NuGetRestore aliases.

NuGetInstall别名的重载为 NuGetInstallSettings 工具设置类,它具有一个属性,您可以用于覆盖使用哪些供稿,例如:

The NuGetInstall alias has overloads that take an NuGetInstallSettings tool settings class which has an Source property which you can use to override which feeds are used, example:

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

NuGetRestore

类似地,NuGetRestore别名具有重载,可让您指定 NuGetRestoreSettings 具有属性,您可以用于覆盖使用哪些供稿,例如:

NuGetRestore

Similarly the NuGetRestore alias has overloads that lets you specify an NuGetRestoreSettings which has an Source property which you can use to override which feeds are used, example:

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" }
        }
    );
}

结论

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

Conclusion

There's a few ways to address your issue.

通过在计算机上配置了多个源时指定一个源,您还可以获得改进的NuGet还原/安装性能,但是当前项目仅使用官方源,因此跳过查看所有已配置的源进食并走到.

Also you can get improved NuGet restore/install performance by specifying a source when you have multiple sources configured on your machine, but the current project only uses official ones as it then skips looking thru all configured feeds and goes streight to the source.

但是,如果您的供稿具有身份验证,那么您将需要为使用nuget.exe NuGetAddSource 别名.

But if your feed has authentication then you will need to add credentials for those using nuget.exe or NuGetAddSource alias.

提示,它具有.

Tip for those using MyGet, it has pre-authenticated url:s you could use without adding a source, but just specifying the Source property for Restore/Install, this is sensitive information so don't store them in your build scripts but rather as i.e. environment variables.

这篇关于我们如何使用Cake build对安全的NuGet服务器进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆