【问题标题】:How to add private nuget source in dotnet dockerfile?如何在 dotnet dockerfile 中添加私有 nuget 源?
【发布时间】:2019-12-19 02:14:13
【问题描述】:

我尝试将私有 nuget 源添加(覆盖)到我的构建脚本中,以添加用户/密码 - 并将其排除在我的源代码控制之外。到目前为止我尝试了什么:

  • nuget 未被识别为图像内的命令
  • dotnet nuget 没有添加其他来源的命令
  • 安装nuget不影响dotnet restore
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 
RUN apt-get update && apt-get install -y nuget 
RUN nuget source Add -Name "Private Feed" -Source ".." -UserName "UserVAR" -Password "PassVAR"
RUN dotnet restore

【问题讨论】:

标签: .net docker .net-core nuget dockerfile


【解决方案1】:
  1. 如果是天蓝色工件,请在您的私人供稿中创建具有完全访问权限的个人访问令牌。

  2. 将 NuGet.Config 文件添加到项目的根目录

    <configuration>
      <packageSources>
        <add key="MyPrivateFeed" value="https://myfeed.url/index.json" />
      </packageSources>
      <packageSourceCredentials>
        <MyPrivateFeed>
          <add key="Username" value="myusername" />
          <add key="ClearTextPassword" value="xq6n4lvnayeo2f467osasdasdgj4nat7xw2mkm7qwowvqeutjdueq" />
          <add key="ValidAuthenticationTypes" value="basic" />
        </MyPrivateFeed>
      </packageSourceCredentials>
    </configuration>
    

    ClearTextPassword 密钥是您的 PAT

  3. 将这两行添加到 docker 文件的复制部分

    COPY "NuGet.Config" "/"
    RUN ["cp", "/NuGet.Config", "/root/.nuget/NuGet/NuGet.Config"]
    

    最后运行你的 docker build 它应该可以工作了。

【讨论】:

  • 这行得通,但是您将凭据提交到您的存储库。
  • 当你拥有私人仓库时,你别无选择。
  • 您至少可以将 PAT 限制为仅读取包。
  • 这不太准确。您可以通过参数 ARG 密码将凭据传递给 Dockerfile
【解决方案2】:

通过将 nuget.config 添加到解决方案/项目并将其复制到 Docker 项目中:

WORKDIR /src
COPY ["nuget.config", ""]

你可以添加源代码,然后你的 docker build 就成功了。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Nellson Nuget Repo" value="http://private.source.local:123/v3/index.json" />
  </packageSources>
  <activePackageSource>
    <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
    <add key="Nellson Nuget Repo" value="http://private.source.local:123/v3/index.json" />
  </activePackageSource>
</configuration>

【讨论】:

  • 这行得通,但我在构建管道中遇到未经授权的错误
【解决方案3】:

nuget source 命令是 nuget.exe 命令行界面的一部分,只能在 Linux 上与 Mono 结合使用。因为 mcr.microsoft.com/dotnet/core/sdk 映像是一个 Linux 容器(准确地说是 Debian)——您需要自己安装 Mono 和 Nuget 才能使用该命令。您的另一个选择是获取 official Mono docker image 并将 .NET Core 和 NuGet.exe 安装到其中。

【讨论】:

    【解决方案4】:

    我当前的解决方法是创建一个nuget.config 的副本,其中包含一个包含用户名和密码占位符的packageSourceCredentials 部分。然后我用这个文件替换我现有的nuget.config,并用环境变量替换用户名和密码。

    唯一的缺点是我需要保持两个配置文件同步。如果我在项目中修改我的nuget.config,我还需要记住更新副本。

    nuget.config.template

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="GitHub private registry" value="https://nuget.pkg.github.com/your_orga/index.json" />
      </packageSources>
      <packageSourceCredentials>
        <GitHub_x0020_private_x0020_registry>
            <add key="Username" value="USER" />
            <add key="ClearTextPassword" value="PW" />
        </GitHub_x0020_nuget_x0020_registry>
    </packageSourceCredentials>
    </configuration>
    

    Dockerfile

    FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-image
    ARG NUGET_AUTH_TOKEN
    ARG NUGET_USER_NAME
    
    WORKDIR /app
    COPY ./My.Project .
    
    # Replace nuget.config
    RUN rm nuget.config
    COPY ./gitlab-ci/nuget.config.template ./nuget.config
    RUN sed -i -e "s/USER/$NUGET_USER_NAME/g" -e "s/PW/$NUGET_AUTH_TOKEN/g" nuget.config
    
    RUN dotnet restore
    

    .gitlab-ci.yml

    docker build
          --build-arg NUGET_USER_NAME=$NUGET_USER_NAME
          --build-arg NUGET_AUTH_TOKEN=$NUGET_AUTH_TOKEN
          --tag $CI_REGISTRY/organization/application:$CI_COMMIT_SHA
          --file gitlab-ci/Dockerfile
          .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-17
      • 2020-01-11
      • 2014-09-03
      • 2021-03-04
      • 1970-01-01
      • 2021-08-16
      • 2021-05-25
      • 1970-01-01
      相关资源
      最近更新 更多