使用 Dockerfile/go.mod 文件的私有(private)存储库的 Go 构建失败

标签 go github ssh dockerfile go-modules

尝试使用 Dockerfile 构建镜像,但看到以下错误:

[6/7] 运行 go mod 下载 && go mod 验证:

#10 4.073 go: github.com/private-repo/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="225047524d0f4c434f476254120c120c120f101210131214121a1011111013110f131046444415161a12121346" rel="noreferrer noopener nofollow">[email protected]</a>: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /app/pkg/mod/cache/vcs/40ae0075df7a81e12f09aaa30354204db332938b8767b01daf7fd56ca3ad7956: exit status 128:

#10 4.073 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096e607d496e607d617c6b276a6664" rel="noreferrer noopener nofollow">[email protected]</a>: Permission denied (publickey).  
#10 4.073 fatal: Could not read from remote repository.  
#10 4.073 Please make sure you have the correct access rights
#10 4.073 and the repository exists.
------
executor failed running [/bin/sh -c go mod download && go mod verify]: exit code: 1

这是我的 Dockerfile:

#Start from base image 1.16.5:
FROM golang:1.16.5

ARG SSH_PRIVATE_KEY

ENV ELASTIC_HOSTS=localhost:9200
ENV LOG_LEVEL=info

#Configure the repo url so we can configure our work directory:
ENV REPO_URL=github.com/private-repo/repo-name

#Setup out $GOPATH
ENV GOPATH=/app

ENV APP_PATH=$GOPATH/src/$REPO_URL

#/app/src/github.com/private-repo/repo-name/src

#Copy the entire source code from the current directory to $WORKPATH
ENV WORKPATH=$APP_PATH/src
COPY src $WORKPATH
WORKDIR $WORKPATH

RUN mkdir -p ~/.ssh && umask 0077 && echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa \
&& git config --global url."ssh://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d4dac7f3d4dac7dbc6d19dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a>/private-repo".insteadOf 
https://github.com \
&& ssh-keyscan github.com >> ~/.ssh/known_hosts

#prevent the reinstallation of vendors at every change in the source code
COPY go.mod go.sum $WORKDIR
RUN go mod download && go mod verify

RUN go build -x -o image-name .

#Expose port 8081 to the world:
EXPOSE 8081

CMD ["./image-name"]

在我的 GO 环境中,我确实有 GO111MODULE=on & GOPRIVATE=github.com/private-repo/*

此外,我可以在我的终端上进行身份验证:
ssh -T [email protected]
你好,私有(private)仓库!您已成功通过身份验证,但 GitHub 不提供 shell 访问权限。

“获取私有(private)仓库名称”成功。

我通过 Dockerfile 构建:

docker build --build-arg SSH_PRIVATE_KEY -t image-name .  

其中有命令:

RUN go build -x -o image-name .

我尝试过的:

GO111MODULE="on"  
GONOPROXY="github.com/user-id/*"  
GONOSUMDB="github.com/user-id/*"  
GOPRIVATE="github.com/user-id/*"  
GOPROXY="https://proxy.golang.org,direct"

[url "ssh://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe99978abe99978a968b9cd09d9193" rel="noreferrer noopener nofollow">[email protected]</a>/"]   
    insteadOf = https://github.com/

最佳答案

Basically, there are multiple repos under github.com/user-name/ and all of them are private which I want to use.

我宁愿使用更具体的指令:

 git config --global \
 url."ssh://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cebe5f8ccebe5f8e4f9eea2efe3e1" rel="noreferrer noopener nofollow">[email protected]</a>/user-name/*".insteadOf https://github.com/user-name/*

这样一来,代替将不会应用于所有 https://github.com URL,仅适用于与您需要的私有(private)存储库匹配的 URL使用 SSH。
OP ios-mxethe comments 中确认它确实按预期工作。

关于使用 Dockerfile/go.mod 文件的私有(private)存储库的 Go 构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68012130/

相关文章:

google-app-engine - 如何判断是否存在具有此名称的文件?

go - 无法使用客户端提供的 id_token 在服务器中使用 Oauth2 获取用户个人资料信息

git cherry-pick 不获取远程分支

windows - 如何在 Windows 上为在 Linux 上创建的 git 使用 ssh key ?

git - 如何指定 `go get` 将使用哪个 ssh key

go - 如何使用 client_golang 将指标推送到普罗米修斯?

python - 从源代码运行 Python 模块(通过 GitHub 获得)

git rebase 在长期(远程)功能分支上

git - 从 jenkins 服务器连接到 bitbucket 存储库

ssh - autossh 失败,退出状态为 1,并且没有错误消息