我有一个monoprepo,其中一个文件夹有Typescript云函数,我想使用GCP云构建进行部署。然而,云构建似乎无法理解package.json在哪里。我想它是在根文件夹中查找它的。部署此功能的最佳方式是什么?
结构:
repo
- app2
- functions
- cloudbuild.yaml
- src
- index.ts
- onNewPostComment
- index.ts
- function2...
- package.json
- package-lock.json
- tsconfig.json
我的cloudbuild.yaml:
steps:
- name: 'gcr.io/cloud-builders/npm'
args: [ 'install']
- name: 'gcr.io/cloud-builders/npm'
args: [ 'run','build']
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
id: 'deploy-onNewPostComment'
args:
- gcloud
- functions
- deploy
- onNewPostComment
- --region=europe-west2
- --source=.
- --entry-point=onNewPostComment
- --trigger-http
- --runtime=nodejs16
package.json:
{
"name": "functions",
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "npm run build && firebase deploy --only functions",
...,
"engines": {
"node": "16"
},
"main": "lib/index.js",
...
"private": true
}
用于npm安装的GCP云构建的输出:
Already have image (with digest): gcr.io/cloud-builders/npm
npm WARN saveError ENOENT: no such file or directory, open '/workspace/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/workspace/package.json'
npm WARN workspace No description
npm WARN workspace No repository field.
npm WARN workspace No README data
npm WARN workspace No license field.
up to date in 0.293s
found 0 vulnerabilities
npm构建的输出:
Already have image (with digest): gcr.io/cloud-builders/npm
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /workspace/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/workspace/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /builder/home/.npm/_logs/2022-07-27T09_05_46_164Z-debug.log
如链接中所述:
初始回购结构:
首先,我们需要设置我们的初始回购结构,如下所示:
docker/
functions/
Dockerfile
start.sh
src/
functions/
http/
helloWorld/
index.ts
index.test.ts
package.json
packages/
lib/
index.ts
package.json
.eslintrc.json
.gitignore
docker-compose.yml
package.json
tsconfig.json
如线程中所述:
当您运行云构建作业时,它会从当前目录。如果您在子目录中,则所有同级或父目录没有上传,因此在Cloud Build上是已知的。
对于这样,您必须在父目录中运行云构建作业级别,并引用命令中的CloudBuild.yaml文件(以及指向正确的子目录(。
考虑也要改变你的cloudbuild.yaml步骤定义,因为根文件夹不再是子目录,但是根目录。您可以使用目录参数指定每个步骤的工作目录。
如果你不想在每个组件构建上上传整个monorepo触发gcloud builds summit时可以忽略文件手动命令。
然而,使用触发器无法做到这一点。你可以忽略文件,但这一次,意味着必须在中忽略文件生成作业触发条件(例如,自述文件,当你推它,触发一个新的构建是没有用的(。但是,如果由其他文件触发,则这些"被忽略的文件"仍将存在于运行作业中。
有关依赖关系的更多信息,请参阅文档。尝试升级节点版本,并使用npm最新版本部署云功能。