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

标签 node.js firebase docker google-cloud-functions google-cloud-run

到目前为止,我一直在使用 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/

相关文章:

javascript - 即使正确渲染,EJS 变量也会引发错误

node.js - Bitbucket Pipeline + Firebase 托管

android - RecyclerView 不从 Firebase Datasnapshot 检索数据

docker - 如何使用带 docker-compose 的官方 MySQL 镜像为 Docker 容器使用临时卷?

docker - 每次重新启动机器时都会出现 Docker 错误 : "Cannot connect to the Docker daemon at unix:///var/run/docker.sock."

javascript - 将文件中的对象属性随机分配给 inline-css

javascript - NodeJS/ExpressJS : proxy an HTTP video stream (from VLC)

docker - Openshift OC补丁未从/docker-entrypoint-initdb.d执行initdb.sql

node.js - 领事:SD架构。从前端访问微服务的正确方法是什么?

javascript - Fire Base 云功能在 Node.js 文件中不起作用