You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

私有GitHub仓库构建Go项目报错:无法获取内部模块

问题

我是大学作业小组成员,我们从公共仓库github.com/ucy-coast/cs499-fa22克隆创建了私有仓库github.com/ucy-coast/username/cs499-fa22,使用Go、Docker和docker-compose开发,所有Go依赖均来自公共仓库。仓库创建者构建无报错,但我通过ssh-agent转发密钥克隆仓库后,执行构建时出现错误:无法找到github.com/ucy-coast/hotel-app/internal/rate/proto、profile/proto、geo/proto模块,报错提示无法读取GitHub用户名,终端提示禁用。具体错误日志如下:

go: finding module for package github.com/sirupsen/logrus
go: finding module for package gopkg.in/mgo.v2
go: finding module for package google.golang.org/grpc/reflection
go: finding module for package golang.org/x/net/context
go: finding module for package github.com/ucy-coast/hotel-app/internal/rate/proto
go: finding module for package github.com/golang/protobuf/proto
go: finding module for package google.golang.org/grpc
go: finding module for package google.golang.org/grpc/credentials/insecure
go: finding module for package github.com/ucy-coast/hotel-app/internal/profile/proto
go: finding module for package github.com/ucy-coast/hotel-app/internal/geo/proto
go: finding module for package github.com/hailocab/go-geoindex
go: finding module for package github.com/uber/jaeger-client-go/config
go: finding module for package github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc
go: finding module for package github.com/opentracing/opentracing-go
go: finding module for package google.golang.org/grpc/keepalive
go: downloading github.com/golang/protobuf v1.5.2
go: downloading github.com/opentracing/opentracing-go v1.2.0
go: downloading google.golang.org/grpc v1.50.1
go: downloading github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
go: downloading github.com/sirupsen/logrus v1.9.0
go: downloading github.com/hailocab/go-geoindex v0.0.0-20160127134810-64631bfe9711
go: downloading golang.org/x/net v0.2.0
go: downloading gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
go: downloading github.com/uber/jaeger-client-go v2.30.0+incompatible
go: found github.com/sirupsen/logrus in github.com/sirupsen/logrus v1.9.0
go: found github.com/opentracing/opentracing-go in github.com/opentracing/opentracing-go v1.2.0
go: found github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc in github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
go: found github.com/hailocab/go-geoindex in github.com/hailocab/go-geoindex v0.0.0-20160127134810-64631bfe9711
go: found google.golang.org/grpc in google.golang.org/grpc v1.50.1
go: found google.golang.org/grpc/keepalive in google.golang.org/grpc v1.50.1
go: found google.golang.org/grpc/reflection in google.golang.org/grpc v1.50.1
go: found github.com/golang/protobuf/proto in github.com/golang/protobuf v1.5.2
go: found golang.org/x/net/context in golang.org/x/net v0.2.0
go: found google.golang.org/grpc/credentials/insecure in google.golang.org/grpc v1.50.1
go: found github.com/uber/jaeger-client-go/config in github.com/uber/jaeger-client-go v2.30.0+incompatible
go: found gopkg.in/mgo.v2 in gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
go: downloading github.com/stretchr/testify v1.7.0
go: downloading google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go: downloading github.com/google/go-cmp v0.5.6
go: downloading google.golang.org/protobuf v1.27.1
go: downloading golang.org/x/sys v0.2.0
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading golang.org/x/text v0.4.0
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading github.com/stretchr/objx v0.1.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
go: finding module for package github.com/ucy-coast/hotel-app/internal/profile/proto
go: finding module for package github.com/uber/jaeger-lib/metrics
go: finding module for package gopkg.in/yaml.v2
go: finding module for package go.uber.org/atomic
go: finding module for package github.com/ucy-coast/hotel-app/internal/geo/proto
go: finding module for package github.com/pkg/errors
go: finding module for package github.com/ucy-coast/hotel-app/internal/rate/proto
go: finding module for package github.com/uber/jaeger-lib/metrics/metricstest
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/uber/jaeger-lib v2.4.1+incompatible
go: downloading go.uber.org/atomic v1.10.0
go: downloading gopkg.in/yaml.v2 v2.4.0
go: found github.com/pkg/errors in github.com/pkg/errors v0.9.1
go: found github.com/uber/jaeger-lib/metrics in github.com/uber/jaeger-lib v2.4.1+incompatible
go: found go.uber.org/atomic in go.uber.org/atomic v1.10.0
go: found github.com/uber/jaeger-lib/metrics/metricstest in github.com/uber/jaeger-lib v2.4.1+incompatible
go: found gopkg.in/yaml.v2 in gopkg.in/yaml.v2 v2.4.0
go: finding module for package github.com/HdrHistogram/hdrhistogram-go
go: finding module for package github.com/ucy-coast/hotel-app/internal/geo/proto
go: finding module for package github.com/ucy-coast/hotel-app/internal/rate/proto
go: finding module for package github.com/ucy-coast/hotel-app/internal/profile/proto
go: downloading github.com/HdrHistogram/hdrhistogram-go v1.1.2
go: found github.com/HdrHistogram/hdrhistogram-go in github.com/HdrHistogram/hdrhistogram-go v1.1.2
go: downloading gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f
go: downloading github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e
go: downloading github.com/kr/text v0.2.0
go: finding module for package github.com/ucy-coast/hotel-app/internal/rate/proto
go: finding module for package github.com/ucy-coast/hotel-app/internal/profile/proto
go: finding module for package github.com/ucy-coast/hotel-app/internal/geo/proto
github.com/ucy-coast/hotel-app/internal/frontend imports
        github.com/ucy-coast/hotel-app/internal/profile/proto: cannot find module providing package github.com/ucy-coast/hotel-app/internal/profile/proto: module github.com/ucy-coast/hotel-app/internal/profile/proto: git ls-remote -q origin in /go/pkg/mod/cache/vcs/25d7ecce62d5217dbfc309e95fa0935ac3442cda3276f618cce8defe7a0cec32: exit status 128:
        fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
github.com/ucy-coast/hotel-app/internal/geo imports
        github.com/ucy-coast/hotel-app/internal/geo/proto: cannot find module providing package github.com/ucy-coast/hotel-app/internal/geo/proto: module github.com/ucy-coast/hotel-app/internal/geo/proto: git ls-remote -q origin in /go/pkg/mod/cache/vcs/25d7ecce62d5217dbfc309e95fa0935ac3442cda3276f618cce8defe7a0cec32: exit status 128:
        fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
github.com/ucy-coast/hotel-app/internal/rate imports
        github.com/ucy-coast/hotel-app/internal/rate/proto: cannot find module providing package github.com/ucy-coast/hotel-app/internal/rate/proto: module github.com/ucy-coast/hotel-app/internal/rate/proto: git ls-remote -q origin in /go/pkg/mod/cache/vcs/25d7ecce62d5217dbfc309e95fa0935ac3442cda3276f618cce8defe7a0cec32: exit status 128:
        fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
ERROR: Service 'profile' failed to build: The command '/bin/sh -c go mod tidy' returned a non-zero code: 1

解决方案

核心原因

Go模块在Docker构建过程中默认使用HTTPS协议访问GitHub,而你的SSH密钥无法在容器内的HTTPS请求中生效,导致权限验证失败;同时容器处于非交互式环境,终端无法弹出用户名输入提示,最终触发报错。

具体修复步骤

  1. 配置Git自动转换GitHub协议为SSH
    在本地执行以下命令,让所有GitHub的HTTPS模块请求自动转为SSH:

    git config --global url."git@github.com:".insteadOf "https://github.com/"
    

    若需在Dockerfile中集成,添加以下命令:

    RUN git config --global url."git@github.com:".insteadOf "https://github.com/"
    
  2. Docker构建时转发SSH代理

    • 单独使用docker build时,添加SSH代理参数:
      docker build --ssh default .
      
      同时在Dockerfile中添加GitHub主机密钥验证,避免SSH连接警告:
      RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
      
    • 使用docker-compose时,在docker-compose.yml中配置SSH代理:
      services:
        profile:
          build:
            context: .
            ssh:
              - default  # 转发本地默认SSH代理到构建容器
      
  3. 验证仓库权限与SSH密钥

    • 确认你的GitHub账号已被添加到github.com/ucy-coast/hotel-app仓库的权限列表(若该仓库为私有)。
    • 本地测试SSH连接GitHub是否正常:
      ssh -T git@github.com
      
      返回Hi [你的用户名]! You've successfully authenticated...则说明密钥有效。
  4. 临时应急方案:本地预下载依赖
    先在本地执行依赖下载,再让Docker构建时复用本地缓存:

    go mod download
    docker build --mount=type=cache,target=/go/pkg/mod .
    

内容的提问来源于stack exchange,提问作者Βασίλης Χατζηπαντελή

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 08:05:25