How can I deploy a Cloud Function Gen1 and Gen2 function from the same codebase?

huangapple go评论137阅读模式
英文:

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 根文件:

  1. package functions
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/GoogleCloudPlatform/functions-framework-go/functions"
  6. )
  7. type Event struct {
  8. // ...
  9. }
  10. func MyGen1Func(ctx context.Context, data Event) error {
  11. // ...
  12. }
  13. func init() {
  14. functions.HTTP("MyGen2Func", func (w http.ResponseWriter, req *http.Request) {
  15. // ...
  16. })
  17. }

以下是它们在 Cloud Build 中部署的方式(大部分内容):

  1. gcloud functions deploy MyGen1Func \
  2. --runtime go120 \
  3. --source ./ \
  4. --trigger-event providers/firebase.auth/eventTypes/user.create \
  5. --entry-point MyGen1Func
  6. gcloud functions deploy MyGen2Func \
  7. --gen2 \
  8. --runtime go120 \
  9. --source ./ \
  10. --trigger-http \
  11. --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:

  1. package functions
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/GoogleCloudPlatform/functions-framework-go/functions"
  6. )
  7. type Event struct {
  8. // ...
  9. }
  10. func MyGen1Func(ctx context.Context, data Event) error {
  11. // ...
  12. }
  13. func init() {
  14. functions.HTTP("MyGen2Func", func (w http.ResponseWriter, req *http.Request) {
  15. // ...
  16. })
  17. }

And this is (mostly) how they're deployed in cloudbuild:

  1. gcloud functions deploy MyGen1Func \
  2. --runtime go120 \
  3. --source ./ \
  4. --trigger-event providers/firebase.auth/eventTypes/user.create \
  5. --entry-point MyGen1Func
  6. gcloud functions deploy MyGen2Func \
  7. --gen2 \
  8. --runtime go120 \
  9. --source ./ \
  10. --trigger-http \
  11. --allow-unauthenticated

Thanks!

答案1

得分: 1

原来你可以从根包中导出gen1和gen2函数,并在部署gen2函数时添加一个环境变量来标记入口点:

--set-env-vars FUNCTION_TARGET=MyGen2Func

  1. package functions
  2. import (
  3. "context"
  4. "net/http"
  5. )
  6. func MyGen1Func(ctx context.Context, data Event) error {
  7. // ...
  8. }
  9. func MyGen2Func(w http.ResponseWriter, req *http.Request) {
  10. // ...
  11. }
英文:

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

  1. package functions
  2. import (
  3. "context"
  4. "net/http"
  5. )
  6. func MyGen1Func(ctx context.Context, data Event) error {
  7. // ...
  8. }
  9. func MyGen2Func(w http.ResponseWriter, req *http.Request) {
  10. // ...
  11. }

huangapple
  • 本文由 发表于 2023年4月18日 01:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76037752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定