构建私有仓库的 Dockerfile/go.mod 文件时出现构建失败的情况。

huangapple go评论91阅读模式
英文:

Go build is failing for private repo with Dockerfile/go.mod files

问题

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

[6/7] RUN go mod download && go mod verify:

#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:

#从基础镜像1.16.5开始:
FROM golang:1.16.5

ARG SSH_PRIVATE_KEY

ENV ELASTIC_HOSTS=localhost:9200
ENV LOG_LEVEL=info

#配置仓库URL以便配置工作目录:
ENV REPO_URL=github.com/private-repo/repo-name

#设置$GOPATH
ENV GOPATH=/app

ENV APP_PATH=$GOPATH/src/$REPO_URL

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

#将整个源代码从当前目录复制到$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

#防止在源代码更改时重新安装依赖项
COPY go.mod go.sum $WORKDIR
RUN go mod download && go mod verify

RUN go build -x -o image-name .

#将端口8081暴露给外部:
EXPOSE 8081

CMD ["./image-name"]

在我的GO环境中,确实设置了GO111MODULE=on和GOPRIVATE=github.com/private-repo/*

此外,我能够在终端上进行身份验证:
ssh -T git@github.com
Hi private-repo! You've successfully authenticated, but GitHub does not provide shell access.

'go get private-repo-name' 是成功的。

我通过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"

insteadOf = https://github.com/
英文:

Trying to build image using Dockerfile, but seeing following error:

[6/7] RUN go mod download && go mod verify:

#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

Here is my 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://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"]

In my GO env, I do have GO111MODULE=on & GOPRIVATE=github.com/private-repo/*

Also, I'm able to authenticate at my terminal:
ssh -T git@github.com
Hi private-repo! You've successfully authenticated, but GitHub does not provide shell access.

'go get private-repo-name' is successful.

I build through Dockerfile:

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

which has command:

RUN go build -x -o image-name .

What I tried:

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

insteadOf = https://github.com/

答案1

得分: 4

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

我更愿意使用一个更具体的指令:

git config --global \
url."ssh://git@github.com/user-name/*".insteadOf https://github.com/user-name/*

这样,insteadOf 不会应用于所有的 https://github.com URL,只会应用于需要使用 SSH 的匹配私有仓库。OP ios-mxe评论中确认它确实按预期工作。

英文:

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

I would rather use a more specific directive:

 git config --global \
 url."ssh://git@github.com/user-name/*".insteadOf https://github.com/user-name/*

That way, the instead of won't apply to all https://github.com URLs, only to the one matching the private repository for which you need to use SSH.
The OP ios-mxe confirms in the comments it is indeed working as expected.

huangapple
  • 本文由 发表于 2021年6月17日 10:31:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/68012130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定