【问题标题】:Deploying Nx workspace to Google App Engine将 Nx 工作区部署到 Google App Engine
【发布时间】:2022-04-12 00:08:34
【问题描述】:

我的 Nx 应用程序在本地运行良好,但部署到 GAE 时失败并出现以下错误:

sh: 1: exec: nx: not found

它无法找到dependencies(不是devDependencies)下列出的nx,因为GAE 没有安装它们。

我的package.json 里面有这个:

  "scripts": {
    "start": "nx serve api",

所以当 GAE 运行 npm run start 时它会失败。我尝试将路径直接指定为nx,但尝试使用node_modules/nx/bin/nx 引用它,但这也失败了。

我想知道如何让 GAE 使用 nx 为应用程序提供服务。

这是一个如此简单和基本的用例,我很困惑,它没有一个简单的解决方案。我一定遗漏了一些非常简单的东西。

【问题讨论】:

  • 您是否尝试过执行npx nx serve api 而不是只使用nx serve

标签: node.js google-app-engine nrwl-nx nrwl


【解决方案1】:

我使用的是 Nx@13,下面是部署到 GAE 的步骤:

  1. "generatePackageJson": true,添加到production执行器,它会在dist/apps/serviceA/package.json中生成一个package.json

  2. GAE will look for scripts:{"start:"your way to start nodejs app "}server.js 如果找不到 start 脚本。您必须编写一个脚本来将scripts:{"start:"node main.js"} 添加到#1 中生成的package.json

  3. cloudbuild中,我们将不得不在dist/apps/serviceA文件夹中执行GAE部署命令,幸运的是cloudbuildstep supportsdir

cloudbuild.yaml

steps:
  - name: node:16-bullseye
    entrypoint: npm
    args: ['install']

  - name: node:16-bullseye
    entrypoint: npm
    args: ['run', 'lint', '${_SERVICE_NAME}']

  - name: node:16-bullseye
    entrypoint: npm
    args: ['run', 'test', '${_SERVICE_NAME}']

  - name: node:16-bullseye
    entrypoint: npm
    args:
      ['run', 'build', '${_SERVICE_NAME}', '--', '--configuration=production']

  - name: node:16-bullseye
    entrypoint: bash
    args:
      [
        '-c',
        'node tools/prepare-gcp-app-engine.js --serviceName=${_SERVICE_NAME}',
      ]

  - name: gcr.io/google.com/cloudsdktool/cloud-sdk:376.0.0-slim
    dir: dist/apps/${_SERVICE_NAME}
    entrypoint: gcloud
    args:
      - app
      - deploy
      - app-engine.yaml
      - --version=$SHORT_SHA
      - --promote
      - --stop-previous-version
      - --quiet

substitutions:
  _SERVICE_NAME: game-api

Source code demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-19
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2020-05-06
    • 2017-07-27
    • 2017-09-04
    相关资源
    最近更新 更多