Heroku Repo Size 和 Slug Size 随每次部署而增加.为什么? [英] Heroku Repo Size and Slug Size increase with each deployment. Why?

查看:176
本文介绍了Heroku Repo Size 和 Slug Size 随每次部署而增加.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更改了应用程序中的一些文本并使用

I change some text in my application and deploy new version with

git push heroku master

但是每次我推,它都会增加很多.我在这里写下每次部署后的增长.

But everytime I push, it increase a lot. I write here the increasement after each deploy.

168.2 KB
37.9 MB

178.6 KB
38.7 MB

187 KB
39.4 MB

194 KB
40.2 MB

205.3 KB
40.9 MB

232.8 KB
41.8 MB

277.9 KB
42.4 MB

286.5 KB
43 MB

如果我选择我的应用项目中的所有文件夹(包括我使用 slugignore e gitignore 忽略的 public/、tmp/和 log/),总大小仅为 198 KB.

If i select all my folders in my app project (including public/, tmp/ and log/ that I ignore with slugignore e gitignore) total size is only 198 KB.

为什么会这样?

推荐答案

可以遵循几个步骤来减小 slug 大小.

There are a few steps that can be followed to reduce the slug size.

清除构建缓存

Buildpacks 缓存一些内容以加速未来的构建.但有时,当您的依赖项发生变化时(尤其是当您删除一个依赖项时),它可能不会从缓存中删除.因此,如果您遇到意外的大 slug 大小并且您已从项目中删除了一些依赖项,请先尝试此操作(如果您可以在下一次部署时忍受缓慢的构建).

Buildpacks cache some content to speed up future builds. But sometimes, when your dependencies change (especially when you remove a dependency), it might not be removed from the cache. So if you are facing unexpectedly high slug sizes and you have removed some dependencies from your project, try this first (if you can live with a slow build on the next deploy).

$ heroku plugins:install heroku-repo 
$ heroku repo:purge_cache -a appname

忽略带有 .slugignore 的文件

您的 .sligignore 应该列出存储库中的目录和文件(不会被 .gitignore 忽略),但应用程序在 prod 上运行不需要这些目录和文件.这些文件的一些示例是测试、文档和设计文件.要从 slug 中删除这些,请在项目的根目录创建一个 .slugignore.

Your .sligignore should list directories and files that are in the repo (not ignored by .gitignore) but not needed for the app to run on prod. Some examples of files like these are the tests, documentation and design files. To remove these from the slug, create a .slugignore at the root of the project.

*.psd
/doc
/test

要查看哪些文件被放入了 slug,你也可以输入 herokubash 并检查文件系统.

To check out what files are being put into the slug, you can also enter the heroku bash and check the filesystem.

heroku run bash -a appname  
du -sh .[^.]* * | sort -hr 

节点模块

这部分特定于包含将所有 JavaScript 代码捆绑到单个脚本的资产管道的应用.如果您使用的是节点服务器,这可能不适用于您.

This part is specific to apps that include an asset pipeline that bundles all JavaScript code to single script. If you are using a node server, this might not apply to you.

如果你在 webpacker 中使用 Rails,你可能在 node_modules 中有很多依赖项.但是在资产管道编译资源之后,您可能不需要 node_modules 目录(除非您在运行时运行使用这些模块的自定义节点脚本).在这种情况下,要删除 node_modules 目录,我们可以使用带有 .slug-post-clean 文件的 heroku-buildpack-post-build-clean buildpack.此文件的格式与 .gitignore 或 .sligignore 相同,因此只需从最终 slug 中列出您要忽略的目录,它们将被删除.使用 buildpack 需要确保的一件事是,这应该是顺序中的最后一个,否则它会在您的实际 buildpack 运行之前删除文件.

If you are using Rails with webpacker, you might have a lot of dependencies in node_modules. But after the asset pipeline compiles the resources, you probably don't need the node_modules directory (unless you run custom node scripts at runtime that use those modules). To remove the node_modules directory in this case, we can use the heroku-buildpack-post-build-clean buildpack with a .slug-post-clean file. The format of this file is the same as .gitignore or .sligignore, so just list out directories that you want to ignore from the final slug and they will be removed. One thing to make sure with the buildpack is that this should be the last one in the order otherwise it will remove the files before your actual buildpack runs.

但是如果您使用在运行时访问 node_modules 的自定义节点脚本怎么办?在这种情况下,我们要做的是将这些脚本移动到一个单独的目录中,并使用它自己的 package.json 文件.要将依赖项与根 package.json 的安装一起安装到该目录中,请将安装后脚本添加到您的根 package.json.这将指示 yarn 将目录更改为您的自定义脚本并在安装根项目的依赖项后在那里安装依赖项.

But what if you are using custom node scripts that access the node_modules at runtime? In that case, what we do is move those scripts to a separate directory with it's own package.json file. To install the dependencies into that directory along with the installation of root package.json, add a postinstall script to your root package.json. This will instruct yarn to change directory to your custom script and install dependencies there after the dependencies of your root project are installed.

"scripts": {
  "postinstall": "yarn --cwd lib/custom-node-script"
}

有了这个,即使在生产 Rails 应用程序中使用自定义节点脚本时,您也可以安全地删除根 node_modules 目录.

With this, you can then safely remove your root node_modules directory even when using custom node scripts in a production Rails app.

这是解决同一问题的详细帖子.

这篇关于Heroku Repo Size 和 Slug Size 随每次部署而增加.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆