我的 .net 项目是我在开发过程中在本地构建的,并由我的 ci/cd 服务器远程构建的。
该项目在 myproject/nuget.config
中有
项目级配置。它被我的 ci/cd 服务器使用,它提供了一些环境变量:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="custom" value="https://example.com/api/nuget/index.json" />
</packageSources>
<packageSourceCredentials>
<custom>
<!-- env vars provided by pipeline -->
<add key="Username" value="%USERNAME%" />
<add key="ClearTextPassword" value="%API_KEY%" />
</custom>
</packageSourceCredentials>
<packageSourceMapping>
<!-- get other packages from nuget server -->
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
<!-- get own packages from private server -->
<packageSource key="custom">
<package pattern="MyOrg.*" />
</packageSource>
</packageSourceMapping>
</configuration>
我的本地计算机在 ~/.nuget/Nuget/Nuget.Config
中有
用户级配置,除了不同的凭据之外,这是相同的:
<packageSourceCredentials>
<custom>
<add key="Username" value="my_username" />
<add key="ClearTextPassword" value="my_personal_api_key" />
</custom>
</packageSourceCredentials>
这适用于 ci/cd 服务器,即生产环境。
但是在本地(即在开发中),运行时出现 401/未经授权的错误
dotnet restore
(等) - 因为 项目级配置(需要额外的环境变量)会覆盖用户级配置。 (我有许多其他存储库也有同样的问题,所以我不想导出环境变量或向我的 .bashrc 添加内容:这很混乱,我不想这样做。)
这似乎与通常的做法相反,其中有一些“默认”签入源代码管理,并且可以在开发(使用覆盖配置文件)和生产(使用配置文件或环境变量)期间进行自定义。
我该怎么做?
(交叉发布到repo。)
“dotnet Restore”命令的“--configfile”开关似乎会有帮助。请参阅https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-restore#options。你可以调用然后
dotnet restore --configfile ~/.nuget/Nuget/Nuget.Config