在我的项目中,我们使用nodejs和typescript进行google云应用程序引擎应用程序开发。我们有自己的构建机制来将ts文件编译成javascript,然后将它们收集到一个完整的可运行包中,所以我们不想依赖google cloud来安装依赖项,而是希望将node_modules中的所有节点包上传到google cloud。
但谷歌云似乎总是会忽略node_modules文件夹,并在部署期间运行npm install。即使我试着从app.yaml中删除'skip_files:- ^node_modules$‘,它也不起作用,谷歌云总是会自己安装软件包。
有没有人有把节点应用和node_modules一起部署的想法?谢谢。
发布于 2016-08-19 06:03:01
我观察到了同样的问题。
我的解决方法是在部署之前将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"
},您可以通过查看AppEngine生成的Docker镜像来确认它剥离了您的node_modules/。你可以在Images page上找到它。他们给你一个命令行,你可以在云控制台上运行来获取它。然后您可以运行docker run <image_name> ls来查看您的目录结构。该图像是在npm install之后创建的,所以一旦您使用上面的变通方法,您将在那里看到您的node_modules/。
发布于 2020-10-26 21:14:12
最新的解决方案是在.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发布于 2021-02-05 11:10:00
在.gcloudignore中允许node_modules不再起作用。
App Engine部署自2020年10月/11月起采用switched to buildpacks。由它触发的云构建步骤总是会删除上传的node_modules文件夹,并使用yarn或npm重新安装依赖项。
这是一个理想的行为,因为上传的node_modules可能来自不同的平台,可能会破坏与用于在app Engine环境中运行应用程序的Linux runner的兼容性。
因此,为了跳过Cloud Build中npm/yarn依赖项的安装,我建议:
node_modules创建tar归档,以避免在每个tar上载大量文件在.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的事情。这将确保预安装脚本无论如何都返回零退出代码,并且yarn/npm安装将继续。
Github为App Engine部署打包和上传依赖项的操作工作流可能如下所示:
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脚本,您将需要创建两个归档文件(一个用于生产依赖项,一个用于开发人员),并修改预安装脚本以解压当前所需的一个(取决于buildpack设置的NODE_ENV )。
https://stackoverflow.com/questions/37310253
复制相似问题