user4412054
user4412054

Reputation:

Heroku Repo Size and Slug Size increase with each deployment. Why?

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

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.

Why all this?

Upvotes: 5

Views: 4327

Answers (2)

Sapan Diwakar
Sapan Diwakar

Reputation: 10966

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

Purge build-cache

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

Ignore files with .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

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 

Node modules

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.

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.

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"
}

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

Here is the detailed post addressing the same issue.

Upvotes: 9

yangtheman
yangtheman

Reputation: 529

It's most likely due to build cache. To speed up build process, Heroku downloads dependencies, and over time it gets built up.

Use their repo plugin to purge build cache, and re-deploy. You will see the slug size being lowered.

https://github.com/heroku/heroku-repo

Here is more about their build cache: https://devcenter.heroku.com/articles/git#build-cache

Upvotes: 2

Related Questions