首页 > 解决方案 > 尝试使用 git 子模块在 Docker Hub 上自动构建时的身份验证问题

问题描述

我正在尝试Docker Hub的自动构建功能。我的情况是:

我按照使用链接的私有子模块构建存储库的说明进行操作,这似乎正是我的用例,但无法使其正常工作。

我已将我的 Docker Hub 存储库链接到我的 BitBucket 存储库,以便我的后端自动构建对特定分支的新提交。

Dockerfile:

FROM python:3.9.7-buster AS builder

RUN git clone \
    --branch master \
    --single-branch \
    git@bitbucket.org:myorganisationname/dependency.git

RUN pip wheel --no-cache-dir --wheel-dir /usr/src/app/wheels --use-feature=in-tree-build ./dependency

[...]

我使用Atlassian 规定的方法从我的 Windows 计算机创建了一对私钥和公钥,并将公钥添加到我的依赖项的 repo 授权密钥中。

1.现在,如果我不添加 SSH_PRIVATE环境变量

我在构建过程中收到以下错误:

Cloning into '.'...
Warning: Permanently added the RSA host key for IP address '18.205.93.0' to the list of known hosts.
Switched to a new branch 'deploy'
KernelVersion: 4.4.0-1060-aws
[...]
#15 [builder 6/7] RUN git clone --branch master --single-branch 
git@bitbucket.org:myorganisationname/dependency.git
#15 sha256:94b95bf83f7896175a6f81ce71694d3e98b14540dede62445c02e779def9c581
#15 0.646 Cloning into 'dependency'...
#15 0.751 Host key verification failed.
#15 0.752 fatal: Could not read from remote repository.
#15 0.752
#15 0.752 Please make sure you have the correct access rights
#15 0.752 and the repository exists.
#15 ERROR: executor failed running [/bin/sh -c git clone --branch master --single-branch 
git@bitbucket.org:myorganisationname/dependency.git]: exit code: 128
------
> [builder 6/7] RUN git clone --branch master --single-branch 
git@bitbucket.org:myorganisationname/dependency.git
------
error: failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c git clone --branch 
master --single-branch git@bitbucket.org:myorganisationname/dependency.git]: exit code: 128
Build failed using Buildkit

这是预期的。

2.添加用于构建的 SSH_PRIVATE环境变量时

在我复制我的私钥的地方,我有这个错误:

Cloning into '.'...
Warning: Permanently added the RSA host key for IP address '18.205.93.2' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
please ensure the correct public key is added to the list of trusted keys for this repository (128)

Docker Hub无法再访问我的 BitBucket 存储库,这对我来说没有多大意义,为构建添加环境变量应该不会影响这一点。

我已经检查了后端的 BitBucket 存储库是否包含Docker Hub在其授权密钥中自动添加的密钥(我还添加了我生成的密钥,以防万一)。


你能看出哪里出了问题吗?

谢谢你。

标签: gitdockerhub

解决方案


这是我错过的:为了使用Docker Hubbuild environment variable中定义的内容,必须创建一个来覆盖命令(相关文档部分)。hookbuild

所以解决方案是创建一个hooks目录和文件:

挂钩/构建

#!/bin/bash

docker build \
    --build-arg SSH_PRIVATE=$SSH_PRIVATE\
    -f $DOCKERFILE_PATH \
    -t $IMAGE_NAME .

推荐阅读