有啥方法可以在 Firebase 云功能中执行回滚?
Posted
技术标签:
【中文标题】有啥方法可以在 Firebase 云功能中执行回滚?【英文标题】:Is there any way to perform rollback in firebase cloud functions?有什么方法可以在 Firebase 云功能中执行回滚? 【发布时间】:2022-01-09 05:56:36 【问题描述】:我们正在 Firebase 上的 NodeJS/TS 中运行一个更大的后端应用程序,其中包含大约 180 个云函数和 Firestore 作为数据库。到目前为止,Firebase 一直很好地满足了我们的需求,但我们正在达到一个使用水平,即使是少量的停机时间也会造成很大的损害。由于云功能的数量,完整部署可能需要长达 30 分钟,因此我们通常只对更改的功能进行部分部署,这仍然需要大约 10 分钟。我正在尝试找到一种能够快速回滚到给定函数的先前版本的方法,以防在生产部署后发现错误。 Firebase 似乎不提供回滚功能,因此唯一的选择是使用以前的版本重新部署代码。一个问题是部署时间(单个函数最多 10 分钟),另一个是部分部署时的 git 版本控制。通常会有一个分支准确反映 prod 中可以使用的内容,但对于部分部署,情况不再如此。使用 prod 维护具有一对一分支的良好 git 版本控制的唯一选择是每次都进行完整部署,但这需要大量时间(30 多分钟不包括重试)。 firebase 部署通常也会失败或超过部署配额,这使得 CI 管道之类的事情变得非常困难(它必须自动重试失败的功能,而且时间仍然是一个问题,因为在停机时间)。有没有人找到一个很好的回滚(版本控制)解决方案和一个可以与 firebase 大规模配合使用的 git 结构?
【问题讨论】:
【参考方案1】:看起来你不是唯一一个。前面的问题回答了。我建议设置一些版本控制。我会首先解决失败的部署问题,这应该会减少 deploy time
和重新部署时间,特别是如果它的多个 .您也可以使用不同的部署分支或设置暂存环境。我会花时间获得 GIT 控制设置/交钥匙。
每位用户爱丽儿:
Each time you make a deploy to a cloud function you get an output line like this:
sourceArchiveUrl: gs://my-store-bucket/us-central1-function_name-xxoxtdxvxaxx.zip
I entered my Google Cloud Platform Developer Console -> Cloud Functions -> function_name -> Source tab
and there almost at the bottom it says: Source location
my-store-bucket/us-central1-function_name-xxoxtdxvxaxx.zip
the same as it was shown in the CLI, but without gs:// that link lead me to the following: https://storage.cloud.google.com/my-store-bucket/us-central1-function_name-........
I removed from the link everything that came after
https://storage.cloud.google.com/my-store-bucket
and that lead me to a huge list of files that each one of them represented a an image of all my cloud functions at the time point of each time i have made a deploy, exactly what i needed!
The only thing left to do was to locate the file with the last date before my mistaken deploy
来源:Retrieving an old version of a Google Cloud function source
截至 2019 年 Rolling back to an older version of a firebase function (google cloud function)
2021: Roll back Firebase hosting and functions deploy jointly?
您可以回滚 Firebase 托管部署,但如果不使用 GIT 版本控制等,则不能回滚功能。使用部分功能,您可以部署多个功能/组。您可以签出远程配置模板以进行回滚,并将其保留长达 90 天。
https://firebase.google.com/docs/remote-config/templates
Firebase partial deploy multiple grouped functions
https://firebase.google.com/docs/cli#roll_back_deploys
【讨论】:
【参考方案2】:Cloud Functions for Firebase 基于 Cloud Functions,它们的行为是相同的。而今天,不可能将流量路由到以前的版本(并执行回滚)。 (我还可以告诉你,NodeJS16 现在是 GA,而不是 Cloud Functions for Firebase documentation 中仍然提到的 Beta)
下一个 Cloud Functions 运行时正在烹饪(并提供预览版)。该运行时在底层基于 Cloud Run,允许流量拆分/路由,因此接受回滚。
因此,目前您还没有解决方案来使用 Firebase 函数执行简单的回滚。一个很大的变化可能是直接使用 Cloud Functions V2 运行时,或事件 Cloud Run,但它是您的代码库的一个重大变化。
另一种解决方案是在所有函数前使用负载平衡器,然后:
以新名称部署新功能(当前部署不更新,每次部署新版本时创建新服务) 使用新功能创建新的无服务器后端 更新 URL 映射以考虑新的后端。一段时间后,删除旧的函数版本。
这也需要大量工作才能将其付诸实施。而且更新 URL 映射时的广告延迟应该在 3 到 5 分钟之间,与您当前的解决方案相比,这并不是一个很大的优势。
【讨论】:
以上是关于有啥方法可以在 Firebase 云功能中执行回滚?的主要内容,如果未能解决你的问题,请参考以下文章