gpt4 book ai didi

node.js - 在 Firebase Function 和 Cloud Run 之间共享代码

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:15 25 4
gpt4 key购买 nike

到目前为止,我一直在使用 Firebase 函数。这次,我想在同一个项目中使用 Firebase Functions 和 Cloud Run。有没有在 Firebase Functions 和 Cloud Run 之间共享代码的好方法?仅通过创建符号链接(symbolic link)就可以工作吗?或者,我需要创建私有(private) npm 包吗?

我的开发环境是 Windows 10, Node 8。我正在使用 Firebase-tools 部署 Firebase Functions,并打算使用 gcloud 命令部署 Cloud Run。

---编辑

我研究了如何将私有(private) npm 包与 Google Cloud Source Repositories 结合使用。对于这个小项目来说,看起来有点麻烦。然后我找到了有用的信息。

Local npm dependency “does not a contain a package.json file” in docker build, but runs fine with npm start https://stackoverflow.com/a/58368933/7908771

我尝试了下面相同的方法,看起来效果很好。

---编辑2

项目文件夹结构:

project/ ................... <- specify project root when `docker build`
├─ .dockerignore ...........
├─ containers/ .............
│ ├─ package.json ......... <- gather docker commands here
│ └─ hello-world/ .........
│ ├─ Dockerfile ........ <- COPY modules from functions in this Dockerfile
│ ├─ index.js .......... <- require modules from here
│ ├─ package-lock.json .
│ └─ package.json ......
├─ functions/ ..............
│ ├─ index.js .............
│ ├─ package-lock.json ....
│ ├─ package.json .........
│ ├─ modules/ .............
│ │ ├─ cloudStorage/ ..... <- wanted module
│ │ │ ├─ index.js .......
│ │ │ ├─ package.json ...
│ │ │ └─ test.js ........

在 Dockerfile 中:

WORKDIR /usr/src/app
COPY functions/modules ../../functions/modules

项目根目录上的.dockerignore:

**/.git
./node_modules # <- ignore only root node_modules to COPY node_modules inside wanted modules

需要在容器内复制js中的函数模块:

const cloudStorage = require('../../functions/modules/cloudStorage')

构建脚本:

{
"scripts": {
"build:hello-world": "docker build ../ --tag gcr.io/project/hello-world -f hello-world/Dockerfile",
}
}

这样在容器外测试js时就可以加载模块,甚至在容器内也可以加载模块。

这会导致任何问题吗?如果有什么问题请告诉我。

谢谢。

最佳答案

符号链接(symbolic link)根本不起作用,因为 Cloud Functions 或 Cloud Run 的每个部署都是完全相互独立的。他们不共享任何文件系统。您必须将共享代码部署到每个产品。私有(private) npm 包可能会起作用。最重要的是,您的 package.json 必须描述从哪里获取代码,否则代码必须成为部署的一部分。

关于node.js - 在 Firebase Function 和 Cloud Run 之间共享代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59394148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com