8

I am having issues trying to deploy a Google cloud function in Go 1.11 using Go modules. I have have the following code structure in my GOPATH:

└── example
    ├── models
    │   ├── go.mod
    │   └── models.go
    └── load
        ├── fn.go
        ├── go.mod
        ├── go.sum
        └── vendor
            └── ....

the load/go.mod looks like the following:

module github.com/example/load

require (
    github.com/example/models v0.0.0
)

replace github.com/example/models => ../models

When I try to deploy the function using the command

gcloud functions deploy load-data --entry-point GCSNewFileTrigger --runtime go111 --trigger-resource new_data --trigger-event google.storage.object.finalize

I get the following error:

Deploying function (may take a while - up to 2 minutes)...failed.                                                                                                                                                                     
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: go: parsing /models/go.mod: open /models/go.mod: no such file or directory
go: error loading module requirements

The commands go mod vendor and go mod verify run successfully locally and I can see my local package models in the vendor folder of load

4

1 回答 1

6

与供应商相比,建造者更喜欢模块。如果有go.mod,将使用模块。当你上传你的函数时,它只包括你的函数在根目录,而不是任何上一级的目录。因此,当有一个go.mod并且您有一个指向上一级的替换指令时,它将不起作用。

解决方案是供应商而不是上传go.mod/go.sum文件。使用时gcloud,您可以创建一个.gcloudignore文件来为您执行此操作。有关更多详细信息,请参阅https://cloud.google.com/functions/docs/concepts/go-runtime#specifying_dependencies

免责声明:我在 Google 工作,负责这个产品。

于 2019-01-29T18:55:47.680 回答