使用Docker进行构建时未经授权的Azure DevOps私有nuget存储库

时间:2019-08-19 02:12:00

标签: docker .net-core azure-devops nuget

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

  

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

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

nuget.config

<?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>

这是我的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"]

我不确定该怎么做才能使其连接到我的仓库

2 个答案:

答案 0 :(得分:2)

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

RUN dotnet restore "MyProject/MyProject.csproj" --configfile Nuget.Config

或者,您可以在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

并通过--build-arg TOKENPASS=$(TOKENPASS) --build-arg TOKENUSER=$(TOKENUSER) --build-arg NUGET_ENDPOINT=$(NUGET_ENDPOINT)

使用docker build

答案 1 :(得分:0)

我偶然发现了此[post] [1]: “ https://developercommunity.visualstudio.com/content/problem/530691/response-status-code-does-not-indicate-success-401.html” (参考:解决方案@底部)

基本上,将PAT Organization更改为“所有可访问的组织”,然后滚动浏览权限\安全性部分并设置以下内容:

构建(工件,定义,请求,对构建进行排队以及更新的构建属性):阅读

已连接的服务器(访问端点):已连接的服务器

打包(创建,读取,更新和删除摘要和打包):已读

(不确定是否所有这些都是必需的。)

Docker文件:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src

# The Personal Access Token arg
ARG PAT

# Set environment variables
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS '{"endpointCredentials":[{"endpoint":"https://pkgs.dev.azure.com/ORG/_packaging/FEEDNAME/nuget/v3/index.json","username":"USERNAME","password":"'${PAT}'"}]}'

# Get and install the Artifact Credential provider
RUN wget -O - https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh  | bash

COPY ["PROJECTNAME.csproj", "./"]
RUN dotnet restore -s "https://pkgs.dev.azure.com/ ORG /_packaging/ FEEDNAME /nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json"

COPY . ./
WORKDIR /src
RUN dotnet build " PROJECTNAME.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish --no-restore " PROJECTNAME.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", " PROJECTNAME.dll"]

docker命令:

docker build -t BUILDNAME:local . --build-arg PAT=xxxxxxxxxxxxxxxxxxxxxxxxx

以前,我们使用的是带有凭据\ PAT的nuget.config文件,并且可以正常工作。我不知道是什么原因导致它停止。如果有人有想法请发帖。