【问题标题】:How to force app engine upload node_modules如何强制应用引擎上传node_modules
【发布时间】:2016-09-15 13:38:18
【问题描述】:

在我的项目中,我们使用 nodejs 和 typescript 进行谷歌云应用引擎应用程序开发。我们有自己的构建机制,将 ts 文件编译成 javascript,然后将它们收集到一个完整的可运行包中,这样我们就不想在 google cloud 上中继安装依赖项,而是希望将 node_modules 内的所有节点包上传到谷歌云。

但似乎谷歌云总是会忽略 node_modules 文件夹并在部署期间运行 npm install 。即使我试图从 app.yaml 中删除 'skip_files: - ^node_modules$',它也不起作用,谷歌云总是会自行安装包。

有没有人有关于将节点应用程序与 node_modules 一起部署的想法?谢谢。

【问题讨论】:

    标签: node.js google-app-engine


    【解决方案1】:

    我观察到了同样的问题。

    我的解决方法是在部署之前将 node_modules/ 重命名为 node_modules_hack/。这可以防止 AppEngine 将其删除。

    我在安装时将其恢复为原始名称,使用以下(部分)package.json 文件:

      "__comments": [
        "TODO: Remove node_modules_hack once AppEngine stops stripping node_modules/"
      ],
      "scripts": {
        "install": "mv -fn node_modules_hack node_modules",
        "start": "node server.js"
      },
    

    您可以通过查看它生成的 Docker 映像来确认 AppEngine 删除了您的 node_modules/。您可以在Images page 上找到它。他们为您提供了一个命令行,您可以在云控制台上运行该命令行来获取它。然后你可以运行docker run <image_name> ls 来查看你的目录结构。该图像是在npm install 之后创建的,因此一旦您使用上述解决方法,您就会在那里看到您的node_modules/

    【讨论】:

    • mv -fn node_modules_hack node_modules
    • 我不确定这是否是一个好主意,因为有很多特定于平台 (os) 的 npm 包和预安装的 node_modules 可能会在您不知道发生了什么的情况下失败跨度>
    【解决方案2】:

    最新的解决方案是在.gcloudignore 中允许node_modules

    下面是默认的.gcloudignore(如果您还没有gcloud app deploy,则初始执行会生成一个)以及您需要的更改:

    # This file specifies files that are *not* uploaded to Google Cloud Platform
    # using gcloud. It follows the same syntax as .gitignore, with the addition of
    # "#!include" directives (which insert the entries of the given .gitignore-style
    # file at that point).
    #
    # For more information, run:
    #   $ gcloud topic gcloudignore
    #
    .gcloudignore
    # If you would like to upload your .git directory, .gitignore file or files
    # from your .gitignore file, remove the corresponding line
    # below:
    .git
    .gitignore
    
    # Node.js dependencies:
    # node_modules/ # COMMENT OR REMOVE THIS LINE
    

    【讨论】:

      【解决方案3】:

      .gcloudignore 中允许node_modules 不再有效。

      自 2020 年 10 月/11 月起,App Engine 部署为 switched to buildpacks。由此触发的 Cloud Build 步骤将始终删除上传的 node_modules 文件夹并使用 yarn 或 npm 重新安装依赖项。

      以下是相关的 buildpack 代码: https://github.com/GoogleCloudPlatform/buildpacks/blob/89f4a6ba669437a47b482f4928f974d8b3ee666d/cmd/nodejs/yarn/main.go#L60

      这是一种理想的行为,因为上传的 node_modules 可能来自不同的平台,并且可能会破坏与用于在 App Engine 环境中运行您的应用的 Linux 运行程序的兼容性。

      因此,为了在 Cloud Build 中跳过 npm/yarn 依赖项安装,我建议:

      • 将 Linux runner CI 与您在 App Engine 环境中使用的相同 Node 版本使用。

      • 使用您的 node_modules 创建 tar 存档,不要在每个 gcloud app deploy 上上传大量文件。

      • .gcloudignore 中忽略node_modules 目录。

      • preinstall 脚本中解压缩node_modules.tar.gz 存档。不要忘记保持向后兼容性以防 tar 存档丢失(本地开发等):

      {
        "scripts": {
          "preinstall": "test -f node_modules.tar.gz && tar -xzf node_modules.tar.gz && rm -f node_modules.tar.gz || true"
        }
      }
      

      注意... || true 的事情。这将确保 preinstall 脚本无论如何返回零退出代码,并且 yarn/npm install 将继续。

      为 App Engine 部署打包和上传依赖项的 Github Actions 工作流程可能如下所示:

        deploy-gae:
          name: App Engine Deployment
          runs-on: ubuntu-latest
          steps:
            - name: Checkout
              uses: actions/checkout@v2
      
            # Preferable to use the same version as in GAE environment
            - name: Set Node.js version
              uses: actions/setup-node@v2
              with:
                node-version: '14.15.4'    
      
            - name: Save prod dependencies for GAE upload
              run: |
                yarn install --production=true --frozen-lockfile --non-interactive
                tar -czf node_modules.tar.gz node_modules
                ls -lah node_modules.tar.gz | awk '{print $5,$9}'
      
            - name: Deploy
              run: |
                gcloud --quiet app deploy app.yaml --no-promote --version "${GITHUB_ACTOR//[\[\]]/}-${GITHUB_SHA:0:7}"
      

      这只是initially suggested hack 的扩展版本。

      注意:如果您的 package.json 中有 gcp-build 脚本,您将需要创建两个存档(一个用于生产依赖项,一个用于开发)并修改预安装脚本以解压缩当前需要的一个(取决于NODE_ENV 由 buildpack 设置)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 2011-03-24
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2013-05-19
        相关资源
        最近更新 更多