如何在 docker 构建中 pip 安装具有私有子模块的私有存储库?

How to pip install private repository that has a private submodule in docker build?

我正在构建一个项目,它需要对私有 python 存储库有一些依赖。这个 repo 有一个子模块,它也指向一个私人 repo。我可以使用 ssh 密钥在我的机器上获取它,这在容器内不方便。

在我的 dockerfile 中,我阻止了这一步:

RUN pip3 install wheel \
  && pip3 wheel git+https://${GITHUB_TOKEN}@github.com/owner/repo.git@master \
--wheel-dir=/svc/wheels

它可以毫无问题地获取 repo,但无法处理子模块。我猜 pip 解析 URL 并且不传递最初传输的令牌。

我真的不知道如何解决这个问题。我可以在本地拥有该文件夹并复制它,或者设置一个我在构建期间访问的私人 pip 存储库(如果可能的话),或者在容器内使用 ssh 密钥(对我来说这听起来很糟糕)。

有什么“标准方法”吗?如果不是,你有什么建议?

EDIT1:这似乎是一个 git 问题,因为 git clone --recursive 也不起作用

与其在命令行中传递 ${GITHUB_TOKEN),不如将其配置到 .netrc 文件中,例如in this answer.

您的 Dockerfile 将包括如下内容:

RUN echo machine github.com login ${GITHUB_TOKEN} > ~/.netrc
RUN pip3 install wheel \
  && pip3 wheel git+https://github.com/owner/repo.git@master \
--wheel-dir=/svc/wheels

dockerfileRUN pip install ...

之前使用以下命令
RUN git config --global url."https://${GITHUB_TOKEN}@github".insteadOf https://github

这将用 https://${GITHUB_TOKEN}@github 替换出现的 https://github,即使在执行 git 命令时在子模块级别也是如此。

注意:在父存储库中,子模块 url(在 .gitmodules 中定义)应设置为 https://github 而不是 git@github