Azure DevOps 私有 nuget 存储库在使用 docker 构建时未经授权

tyc*_*czj 5 nuget docker .net-core azure-devops

我正在尝试为我的项目创建一个 docker 镜像,该镜像有一个私有的 Azure DevOps nuget 存储库。我可以使用我的凭据连接到 Visual Studio 中的 repo,但是当我尝试构建 docker 映像时出现错误。

无法加载源 https://{my_url}/nuget/v3/index.json 的服务索引响应状态代码不表示成功:401(未授权)

我将我Nuget.config file的复制到我的项目的根目录

配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <disabledPackageSources>
    <add key="Microsoft and .NET" value="true" />
  </disabledPackageSources>
  <activePackageSource>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </activePackageSource>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="libraries" value="https://{my_url}/nuget/v3/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <libraries>
      <add key="Username" value="{my_username}" />
      <add key="ClearTextPassword" value="{my_pass}" />
    </libraries>
  </packageSourceCredentials>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这是我的 dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0.0-preview8-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.0.100-preview8-nanoserver-1903 AS build
WORKDIR /src
COPY NuGet.config "MyProject/"
COPY ["MyProject/MyProject.csproj", "MyProject/"]
COPY . .
RUN dotnet restore "MyProject/MyProject.csproj"
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.dll"]
Run Code Online (Sandbox Code Playgroud)

我不知道还能做什么才能连接到我的仓库

4c7*_*b41 3

您需要强制dotnet restore使用您的配置文件:

RUN dotnet restore "MyProject/MyProject.csproj" --configfile Nuget.Config
Run Code Online (Sandbox Code Playgroud)

或者你可以在你的 dockerfile 中执行类似的操作:

# Add Environment Variables references for access private Azure Artifacts Repository
# Use --build-arg <ARG> to pass this in.
ARG TOKENPASS
ARG TOKENUSER
ARG NUGET_ENDPOINT

# Fetch Azure Artifacts "Magic" credentials providers
RUN wget -qO- https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh | bash

# Needs to be enabled to fetch the private repository credentials for NuGet restore.
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS="{\"endpointCredentials\": [{\"endpoint\":\"$NUGET_ENDPOINT\", \"username\":\"$TOKENUSER\", \"password\":\"$TOKENPASS\"}]}"

# Workaround of some kind to help Azure Artifact Private repository image collection.
ENV DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER 0
Run Code Online (Sandbox Code Playgroud)

并使用 docker build 与--build-arg TOKENPASS=$(TOKENPASS) --build-arg TOKENUSER=$(TOKENUSER) --build-arg NUGET_ENDPOINT=$(NUGET_ENDPOINT)