在 Firebase 函数和 Cloud Run 之间共享代码

ast*_*net 1 node.js firebase docker google-cloud-functions google-cloud-run

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

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

- - 编辑

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

本地 npm 依赖项在 docker build 中“不包含 package.json 文件”,但使用 npm start 运行良好/sf/answers/4085825341/

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

--- 编辑 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 ........ 
Run Code Online (Sandbox Code Playgroud)

在 Dockerfile 中:

WORKDIR /usr/src/app
COPY functions/modules ../../functions/modules
Run Code Online (Sandbox Code Playgroud)

.dockerignore 项目根目录:

**/.git
./node_modules # <- ignore only root node_modules to COPY node_modules inside wanted modules
Run Code Online (Sandbox Code Playgroud)

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

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 ........ 
Run Code Online (Sandbox Code Playgroud)

构建脚本:

{
  "scripts": {
    "build:hello-world": "docker build ../ --tag gcr.io/project/hello-world -f hello-world/Dockerfile",
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢你。

Dou*_*son 5

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