首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Go build对于Dockerfile/go.mod文件的私有回购失败

Go build对于Dockerfile/go.mod文件的私有回购失败
EN

Stack Overflow用户
提问于 2021-06-17 02:31:55
回答 1查看 3.6K关注 0票数 2

试图使用Dockerfile构建映像,但发现以下错误:

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

代码语言:javascript
复制
#10 4.073 go: github.com/private-repo/repo-name@v0.0.0-20210608233213-12dff748001d: 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 git@github.com: 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:

代码语言:javascript
复制
#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://git@github.com/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"]

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

,我也可以在我的终端上进行身份验证:

ssh -T git@github.com

嗨,私人-回购!您已经成功地通过了身份验证,但是GitHub不提供shell访问。

“'go get私人回购-名称”是成功的。

我通过Dockerfile构建:

代码语言:javascript
复制
docker build --build-arg SSH_PRIVATE_KEY -t image-name .  

它有命令:

代码语言:javascript
复制
RUN go build -x -o image-name .

我尝试了什么:

代码语言:javascript
复制
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://git@github.com/"]   
    insteadOf = https://github.com/
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-17 09:03:52

基本上,在github.com/user-name/下有多个repos,它们都是私有的,我想使用它们。

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

代码语言:javascript
复制
 git config --global \
 url."ssh://git@github.com/user-name/*".insteadOf https://github.com/user-name/*

这样,代替将不适用于所有https://github.com URL,只适用于与您需要对其使用SSH的私有存储库匹配的URL。

OP ios-mxethe comments中证实了它确实如预期的那样工作。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68012130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档