英文:
How can I deploy a Cloud Function Gen1 and Gen2 function from the same codebase?
问题
我正在尝试从同一代码库部署 Gen1 和 Gen2 云函数(因为 Firebase Auth 事件仅支持 Gen1)。
如果我只部署其中一个,部署工作正常,但如果我将两者一起添加,就会抛出错误:Function failed to start: no matching function found with name: "MyGen1Func"
。
这是我的 functions.go
根文件:
package functions
import (
"context"
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
type Event struct {
// ...
}
func MyGen1Func(ctx context.Context, data Event) error {
// ...
}
func init() {
functions.HTTP("MyGen2Func", func (w http.ResponseWriter, req *http.Request) {
// ...
})
}
以下是它们在 Cloud Build 中部署的方式(大部分内容):
gcloud functions deploy MyGen1Func \
--runtime go120 \
--source ./ \
--trigger-event providers/firebase.auth/eventTypes/user.create \
--entry-point MyGen1Func
gcloud functions deploy MyGen2Func \
--gen2 \
--runtime go120 \
--source ./ \
--trigger-http \
--allow-unauthenticated
谢谢!
英文:
I'm trying to deploy a Gen1 and Gen2 Cloud Function from the same codebase. (Because firebase auth events are only supported in Gen1).
The deploy works fine if I deploy 1 or the other, but if I add the 2 together, an error is throw: Function failed to start: no matching function found with name: "MyGen1Func"
This is my functions.go
root file:
package functions
import (
"context"
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
type Event struct {
// ...
}
func MyGen1Func(ctx context.Context, data Event) error {
// ...
}
func init() {
functions.HTTP("MyGen2Func", func (w http.ResponseWriter, req *http.Request) {
// ...
})
}
And this is (mostly) how they're deployed in cloudbuild:
gcloud functions deploy MyGen1Func \
--runtime go120 \
--source ./ \
--trigger-event providers/firebase.auth/eventTypes/user.create \
--entry-point MyGen1Func
gcloud functions deploy MyGen2Func \
--gen2 \
--runtime go120 \
--source ./ \
--trigger-http \
--allow-unauthenticated
Thanks!
答案1
得分: 1
原来你可以从根包中导出gen1和gen2函数,并在部署gen2函数时添加一个环境变量来标记入口点:
--set-env-vars FUNCTION_TARGET=MyGen2Func
package functions
import (
"context"
"net/http"
)
func MyGen1Func(ctx context.Context, data Event) error {
// ...
}
func MyGen2Func(w http.ResponseWriter, req *http.Request) {
// ...
}
英文:
Turns out you can just export both gen1 and gen2 functions from the root package and add an environment variable when deploying the gen2 function to mark the entry point:
--set-env-vars FUNCTION_TARGET=MyGen2Func
package functions
import (
"context"
"net/http"
)
func MyGen1Func(ctx context.Context, data Event) error {
// ...
}
func MyGen2Func(w http.ResponseWriter, req *http.Request) {
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论