github-action-setup-ddev
github-action-setup-ddev copied to clipboard
How to prepopulate the composer or npm cache within ddev using actions/cache?
I tried searching on how we could use actions/cache to help pre-populate the composer and npm caches inside the DDEV containers running in Github Actions. https://github.com/drud/ddev/pull/1248 added a persistent cache between DDEV containers, but that doesn't help when running in Github Actions which is a unique instance every time.
The ddev composer cache is stored in /mnt/ddev-global-cache/composer/, which is the "composer" directory in the ddev-global-cache docker volume. So you'd have to extract it from the docker volume and save it away as a tarball in a cache or something. Not hard to do.
The npm cache would be normal github actions cache behavior, you'd declare cache and use it. https://github.com/actions/cache
You could just get the composer cache with docker cp ddev-d9-web:/mnt/ddev-global-cache/composer /your/cache/dir
And of course you'd have to restore it with the reverse behavior on action start.
The path in the runner is probably /var/lib/docker/volumes/ddev-global-cache/. (docker volume inspect ddev-global-cache)
I don't recommend restoring directly to a volume in any docker environment, but it might work.
NPM's cache is still generally located in ~/.npm, which would be outside our project directory as well.
We should probably figure out how to get npm's cache somewhere else, as ~/.npm is lost on every restart. It could also be copied with the docker cp approach, but I'd certainly be open to a PR or issue moving it somewhere better (at least in ddev-global-cache or /mnt/ddev-config or something).
However, I don't know if the npm cache is OK across projects? composer cache is pretty resilient and seems to be fine across all php versions and multiple projects.
I was interested in figuring out how to cache npm and yarn cache, but there doesn't seem to be an obvious way in either case. There doesn't seem to be an environment variable or anything. If you know better I'd love to hear about it.
It doesn't directly help you with this specific problem but the caches will move and be persisted in v1.20.0:
- https://github.com/drud/ddev/pull/4051
I would still be curious how I could help add composer or npm cache data to those containers ahead of time, before I first do a ddev start (since we use a hook that runs composer install on start).
There are awkward ways to do it (running a container with volume mounted ahead of time and prepopulating) but the best way would be to just keep a running cache with the stuff in it. You could in fact cache all of ddev-global-cache and restore it.
v1.20.0 will be out in the next couple of weeks, with npm caching support
Hrm, can I use actions/cache to cache the docker container itself?
It's docker volume, not container, and I think for predictable, supported docker behavior you'd have to copy it out of volume before caching, and copy it in after cache restore.
Theoretically the layers of containers are also there. But here it is "easier" because ddev-global-cache is a volume already.
Randy is right and I have been too quick. To do it in a supported way I would rather follow the procedure for backupping and restoring.
Seems this is answered.