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

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

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) {
  // ...
}

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:

确定