在 Firebase 函数和云之间共享代码 运行

Sharing code between Firebase Functions and Cloud Run

到目前为止,我一直在使用 Firebase Functions。这一次,我想在同一个项目中使用 Firebase Functions 和 Cloud 运行。有没有在 Firebase Functions 和 Cloud 运行 之间共享一些代码的好方法?它会通过创建一个符号 link 来工作吗?或者,我需要创建私有 npm 包吗?

我的开发环境是Windows10,节点8。我正在使用Firebase-tools来部署Firebase Functions,并打算使用gcloud命令来部署Cloud 运行。

--- 编辑

我检查了如何将私有 npm 包与 Google 云源存储库一起使用。这个小项目看起来有点麻烦。然后我找到了有用的信息。

Local npm dependency “does not a contain a package.json file” in docker build, but runs fine with npm start

我在下面尝试了同样的方法,它似乎工作正常。

--- 编辑 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时可以加载模块,在容器内也可以加载。

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

谢谢。

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