Go Runtime部署GCloud Functions:私有依赖及Vendor同步报错问题
Go 1.22 Google Cloud Functions部署时vendor目录"不一致"报错的解决方案
问题描述
部署基于Go的Google Cloud Function时:
- 因代码引入私有GitHub包,
gcloud functions deploy报错无法读取GitHub用户名: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. - 采用
go mod vendor打包依赖后,切换到Go 1.22运行时(添加--runtime=go122参数)部署,又触发依赖不一致错误:
但实际failed to build: extracting module and package names: (error ID: 03a1e2f7): go: inconsistent vendoring in /workspace/serverless_function_source_code: github.com/GoogleCloudPlatform/functions-framework-go@v1.9.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt ...vendor/modules.txt已正确标记依赖为explicit:
所有依赖均出现该错误。# github.com/GoogleCloudPlatform/functions-framework-go v1.9.1 ## explicit; go 1.21
解决方案
方案1:用Go 1.22重新生成vendor目录
- 本地切换到Go 1.22版本(确保已安装)
- 删除现有vendor目录:
rm -rf vendor - 更新
go.mod中的Go版本为1.22:go edit -go=1.22 - 重新生成vendor目录:
go mod vendor - 重新执行部署命令:
gcloud functions deploy YOUR_FUNCTION_NAME --runtime=go122 [其他必要参数]
方案2:部署时强制启用vendor模式
若方案1无效,可在部署时通过环境变量强制Go使用vendor目录:
gcloud functions deploy YOUR_FUNCTION_NAME --runtime=go122 --set-build-env-vars GOFLAGS="-mod=vendor" [其他必要参数]
原因分析
Go 1.22对vendor/modules.txt的格式做了细节调整,同时Google Cloud Functions的Go 1.22运行时构建环境会严格校验依赖的版本匹配性。如果本地用Go 1.21生成vendor目录,文件中标记的go 1.21会与部署环境的Go 1.22不兼容,导致构建工具误判为依赖不一致。
内容的提问来源于stack exchange,提问作者Vicente Gavara
相关产品推荐
相关产品推荐

