NuGet 跨平台身份验证
- 在开发过程中使用 Visual Studio 客户端还原 nuget 包,对于私有源,它会弹出一个交互窗口以供用户填写用户名密码。但是在打造 CI/CD 流水线时,使用命令还原包,并且无人值守,这个情况下,怎么做身份证验证,且听我慢慢道来
寻寻觅觅
撸起袖子干
- 参照文档,下载插件进行测试,或者执行脚本安装。插件放置位置如下
- 通过环境变量指定:NUGET_PLUGIN_PATHS="~/.nuget/plugins"
- 或者放在用户文件中指定位置:
.nuget
plugins
netfx
myFeedCredentialProvider
myFeedCredentialProvider.exe
nuget.protocol.dll
netcore
myFeedCredentialProvider
myFeedCredentialProvider.dll
nuget.protocol.dll
- 设置账号密码:通过设置环境变量 VSS_NUGET_EXTERNAL_FEED_ENDPOINTS,包含服务端点、用户名和访问令牌数组的 JSON,用于在 nuget.config 中对端点进行身份验证。示例:
{
"endpointCredentials": [
{
"endpoint": "http://example.index.json",
"username": "optional",
"password": "accesstoken"
}
]
}
- 为了方便,可以做成镜像,集成这个插件和认证信息,但我比较懒,就先用命令完成安装,并每次启动构建都安装,linux 下命令如下
mkdir ~/.nuget && cd ~/.nuget && \
wget -O tar https://github.com/microsoft/artifacts-credprovider/releases/download/0.1.18/Microsoft.NuGet.CredentialProvider.tar.gz && \
tar -xvf tar
后知后觉
收获
FROM aspnetcore:2.2 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM dotnetcoresdk:2.2 AS build
ARG VSS_NUGET_EXTERNAL_FEED_ENDPOINTS
RUN mkdir ~/.nuget && cd ~/.nuget && wget -O tar https://github.com/microsoft/artifacts-credprovider/releases/download/0.1.18/Microsoft.NuGet.CredentialProvider.tar.gz && tar -xvf tar
WORKDIR /src
COPY ["IdentityServer4/IdentityServer4.csproj", "IdentityServer4/"]
COPY ["IdentityServer4/Nuget.config", "IdentityServer4/"]
COPY ["IdentityServer4.XCode/IdentityServer4.XCode.csproj", "IdentityServer4.XCode/"]
RUN dotnet restore "IdentityServer4/IdentityServer4.csproj" --configfile "IdentityServer4/Nuget.config"
COPY . .
WORKDIR /src/IdentityServer4
RUN dotnet build "IdentityServer4.csproj" -c Release -o /app --no-restore
FROM build AS publish
RUN dotnet publish "IdentityServer4.csproj" -c Release -o /app --no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "IdentityServer4.dll"]
总结
打开App,阅读手记