如何使用带有私有存储库依赖项的 Go 运行 Google 云函数?

How to run a Google Cloud Function using Go with dependencies in private repositories?

我正在尝试将此 tutorial 中的 Go 示例改编为在私有存储库中使用依赖项的内容。这是云函数示例代码:

package helloworld

import (
    "context"
    "log"

    "github.com/kurtpeek/my-private-repo/mypackage"
)

// PubSubMessage is the payload of a Pub/Sub event.
type PubSubMessage struct {
    Data []byte `json:"data"`
}

// HelloPubSub2 consumes a Pub/Sub message.
func HelloPubSub2(ctx context.Context, m PubSubMessage) error {
    name := string(m.Data)
    if name == "" {
        name = "World"
    }
    log.Printf("Hello, %s!", name)
    log.Println(mypackage.SayHello())
    return nil
}

其中 SayHello() 在私人仓库 github.com/kurtpeek/my-private-repo 中定义为

package mypackage

// SayHello says hello
func SayHello() string {
    return "Hello, world!"
}

在不调用 mypackage.SayHello() 的情况下,云函数会按预期进行部署和 运行s。我也可以 运行 HelloPubSub2main.go after 运行ning

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

并将我的 SSH 密钥添加到存储库中(参见 Medium article)。

但是,如果我尝试使用 mypackage.SayHello(),我会收到以下错误:

could not read Username for 'https://github.com'

这是完整的终端输出:

> 
gcloud functions deploy HelloPubSub2 --runtime go113 --trigger-topic mytopic
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
WARNING: Function created with limited-access IAM policy. To enable unauthorized access consider "gcloud alpha functions add-iam-policy-binding HelloPubSub2 --member=allUsers --role=roles/cloudfunctions.invoker"
Deploying function (may take a while - up to 2 minutes)...failed.                                              
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: {"error":{"buildpackId":"google.go.functions-framework","buildpackVersion":"0.9.0","errorType":13,"canonicalCode":13,"errorId":"03a1e2f7","errorMessage":"go: github.com/kurtpeek/my-private-repo@v0.0.0-20200508055124-8eb94cb388b2: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /layers/google.go.functions-framework/functions-framework/pkg/mod/cache/vcs/93c0b37d34e5cf0f9b9778b99e5968bf272c89cabd21f8c0d683212c8dd2ef89: exit status 128:\n\tfatal: could not read Username for 'https://github.com': terminal prompts disabled"},"stats":null}

部署此 Cloud Function 的最佳方式是什么?我应该 运行 go mod vendor 然后更改依赖项以引用 vendor/ 目录吗? (虽然这似乎每次都很麻烦)。

更新

本文档 https://cloud.google.com/functions/docs/writing/specifying-dependencies-go#using_private_dependencies 恰好解决了这个用例。不过,我还不能让它正常工作。我添加了

go.mod
go.sum

到我的 .gcloudignore 和 运行 go mod vendor,从而创建一个 vendor/ 目录,但是如果我再次尝试部署它,我现在得到

cannot find package \"github.com/kurtpeek/my-private-repo/mypackage\" in any of:\n\t/usr/local/go/src/github.com/kurtpeek/my-private-repo/mypackage (from $GOROOT)\n\t/workspace/src/github.com/kurtpeek/my-private-repo/mypackage (from $GOPATH)"

~/g/s/g/k/m/helloworld> 
gcloud functions deploy HelloPubSub2 --runtime go113 --trigger-topic mytopic
Deploying function (may take a while - up to 2 minutes)...failed.                                            
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: {"error":{"buildpackId":"google.go.build","buildpackVersion":"0.9.0","errorType":2,"canonicalCode":2,"errorId":"6191efcd","errorMessage":"src/helloworld/helloworld.go:7:2: cannot find package \"github.com/kurtpeek/my-private-repo/mypackage\" in any of:\n\t/usr/local/go/src/github.com/kurtpeek/my-private-repo/mypackage (from $GOROOT)\n\t/workspace/src/github.com/kurtpeek/my-private-repo/mypackage (from $GOPATH)"},"stats":null}

我运行在我的 GOPATH:

中使用它
kurt@Kurts-MacBook-Pro-13 ~/g/s/g/k/my-cloud-function> go env GOPATH
/Users/kurt/go
kurt@Kurts-MacBook-Pro-13 ~/g/s/g/k/my-cloud-function> pwd
/Users/kurt/go/src/github.com/kurtpeek/my-cloud-function

有什么解决办法吗?

我找到了答案(来自一名从事该产品的 Google 工程师)here as well as in 。关键是gcloud functions deploy只是将运行所在目录的内容复制到云函数的'context'中,所以vendor/目录必须在那个目录下.同时,我注意到 package 不能是 main,所以我采取了一些不寻常的步骤,从存储库的根目录中删除 go.mod 并 运行ning go mod initgo mod vendorhelloworld 目录中。现在有效了:

> 
gcloud functions deploy HelloPubSub2 --runtime go113 --trigger-topic mytopic
Deploying function (may take a while - up to 2 minutes)...done.                                                         
availableMemoryMb: 256
entryPoint: HelloPubSub2
eventTrigger:
...