使用 Cloud Build 部署到 App Engine 时如何使用相对依赖项

How to use relative dependencies when deploying to App Engine with Cloud Build

我有一个全栈项目,其中有一些通用模块,主要是类型定义。我想将它们保存在同一个存储库中,以保持我的应用程序状态一致。但是,部署并不像我预期的那样工作。在我的前两个构建步骤 yarn install 和 yarn build 中,我的相对依赖性没有明显的问题。

不过Gcloud好像没有找到。在第 6 个构建步骤中,它显示以下错误消息:

Gcloud building steps

Already have image (with digest): eu.gcr.io/gae-runtimes/buildpacks/nodejs12/builder:nodejs12_20210308_12_21_0_RC00
=== Node.js - Yarn (google.nodejs.yarn@0.9.0) ===
--------------------------------------------------------------------------------
Running "bash -c command -v yarn || true"
/usr/bin/yarn
Done "bash -c command -v yarn || true" (53.639626ms)
DEBUG: Yarn is already installed, skipping installation.
--------------------------------------------------------------------------------
Running "node -v"
v12.21.0
Done "node -v" (35.463448ms)
DEBUG: Current dependency hash: "8d626f89a4d8d27dba2aab32f998ebdf2b45531f6abae7f55eabb00d85cff016"
DEBUG:   Cache dependency hash: "e6c8baadd1eb6d1fd69fc3255ba5936ea43d709f6502d54719fdb17c9d9865af"
Installing application dependencies.
DEBUG: ***** CACHE MISS: "prod dependencies"
--------------------------------------------------------------------------------
Running "node -v"
v12.21.0
Done "node -v" (5.746529ms)
--------------------------------------------------------------------------------
Running "yarn install --non-interactive --frozen-lockfile (NODE_ENV=production)"
yarn install v1.22.4
[1/4] Resolving packages...
error Package "common" refers to a non-existing file '"/common"'.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
Done "yarn install --non-interactive --frozen-lockfile (NODE_ENV=p..." (528.261919ms)
Failure: (ID: 98e389b6) yarn install v1.22.4
[1/4] Resolving packages...
error Package "common" refers to a non-existing file '"/common"'.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
--------------------------------------------------------------------------------
Running "mv -f /builder/outputs/output-5577006791947779410 /builder/outputs/output"
Done "mv -f /builder/outputs/output-5577006791947779410 /builder/o..." (15.651484ms)
ERROR: failed to build: exit status 1

文件夹结构:

client/
    package.json
    cloudbuild.json
common/
    package.json
server/
    package.json
    cloudbuild.json

client/package.json

{
    "name": "client",
    "dependencies": {
        "common": "file:../common"
    }
}

client/cloudbuild.json

{
    "steps": [
        {
            "name": "node",
            "entrypoint": "yarn",
            "args": ["--cwd", "client", "install"]
        },
        {
            "name": "node",
            "dir": "client",
            "entrypoint": "yarn",
            "args": ["run", "build"]
        },
        {
            "name": "gcr.io/google.com/cloudsdktool/cloud-sdk",
            "entrypoint": "bash",
            "dir": "client",
            "args": ["gcloud", "app", "deploy"]
        }
    ],
    "timeout": "1600s"
}

我需要如何调整我的 cloudbuild 文件以便构建器找到我的相对依赖项?

您应该将所有内容(clientservercommon)组合到一个 Cloud Build 配置中。

当您 gcloud build submit 时,源目录被转移到 Cloud Build 服务 (VM) 并放置在名为 /workspace 的目录中。如果您希望能够从 clientserver 引用 common,则必须在构建过程中复制此目录。为了节省能源,构建common一次然后构建clientserver引用它很方便。

然后您可以通过引用 common 来构建 clientserver,因为它也被转移了。

您可以从一个 Cloud Build 作业中生成多个图像,因此您应该为 client 生成一个,为 server 生成一个。

文件夹结构:

cloudbuild.json
client/
    package.json

common/
    package.json
server/
    package.json

或者,我不太熟悉 Node.JS 包管理,您可以先 (Cloud) Build common 然后让 client 的 (Cloud) Builds 可以访问它和 server。通常,这将通过一些(网络可访问的)私人包管理解决方案,clientserver 将作为其构建的一部分引用。