登录
首页 >  Golang >  Go问答

构建 Go 文件时,出现私有存储库 Dockerfile/go.mod 的错误提示

来源:stackoverflow

时间:2024-02-15 09:36:25 417浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《构建 Go 文件时,出现私有存储库 Dockerfile/go.mod 的错误提示》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

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

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

#10 4.073 go: github.com/private-repo/[email protected]: 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 [email protected]: 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://[email protected]/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 [电子邮件受保护]

你好,私人仓库!您已成功通过身份验证,但 github 不提供 shell 访问权限。

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

我通过 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://[email protected]/"]   
    insteadOf = https://github.com/

正确答案


基本上,github.com/user-name/ 下有多个存储库,并且所有这些存储库都是我想使用的私有存储库。

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

git config --global \
 url."ssh://[email protected]/user-name/*".insteadOf https://github.com/user-name/*

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

以上就是《构建 Go 文件时,出现私有存储库 Dockerfile/go.mod 的错误提示》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>