【问题标题】:Go build is failing for private repo with Dockerfile/go.mod files带有 Dockerfile/go.mod 文件的私有仓库的 Go 构建失败
【发布时间】:2021-09-01 19:57:51
【问题描述】:

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

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

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

#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"]

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

另外,我可以在终端上进行身份验证:
ssh -T git@github.com
嗨私人回购!您已成功通过身份验证,但 GitHub 不提供 shell 访问权限。

'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"

[url "ssh://git@github.com/"]   
    insteadOf = https://github.com/

【问题讨论】:

    标签: go github ssh dockerfile go-modules


    【解决方案1】:

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

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

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

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

    【讨论】:

    • => 错误 [6/7] RUN go mod download && go mod verify 1.3s ------ > [6/7] RUN go mod download && go mod verify: #10 1.263去:github.com/user-name/repo-name@v0.0.0-20210608233213-12dff748001d:无效版本:git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags /* 在 /app/pkg/mod/cache/vcs/40ae0075df7a81e12f09aaa30354204db332938b8767b01daf7fd56ca3ad7956 中:退出状态 128:#10 1.263 致命:无法读取“github.com”的用户名:终端提示已禁用
    • @ios_mxe github.com/user-name/repo-name 与您的问题中的@github.com/private-repo 相同吗?是不是应该使用 SSH 而不是 HTTPS?
    • 是的。基本上,在 github.com/user-name/ 下有多个存储库,它们都是我想要使用的私有存储库。所以我尝试了 github.com/user-name/*。 Go env 设置为:GOPRIVATE=github.com/private-repo/*
    • @ios_mxe 好的。我已经相应地编辑了我的答案。
    • 感谢@VonC!以下工作: git config --global url."ssh://git@github.com/user-name/*".insteadOf "github.com/user-name*"
    猜你喜欢
    • 1970-01-01
    • 2020-09-07
    • 2019-04-30
    • 1970-01-01
    • 2021-11-01
    • 2023-03-20
    • 2016-06-29
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多