Skip to content

Commit 1e5d46c

Browse files
authored
Development: use --build-arg to invalidate cache (#244)
* Development: use `--build-arg` to invalidate cache Create a hash of all the dependencies files and use it as a `--build-arg` to invalidate the cache when the hash changes. Related #241 * Update the cache hash instead of appending each of them * Accept `PRUNE_PYTHON_PACKAGE_CACHE` as an environment variable
1 parent 1695f38 commit 1e5d46c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

dockerfiles/docker-compose.yml

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ services:
1111
build:
1212
context: ${PWD}
1313
dockerfile: ${PWD}/dockerfiles/Dockerfile
14+
args:
15+
PRUNE_PYTHON_PACKAGE_CACHE: ${PRUNE_PYTHON_PACKAGE_CACHE:-0}
1416

1517
nginx:
1618
stop_grace_period: 1s

dockerfiles/tasks.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import hashlib
34

45
from invoke import task
56

@@ -14,8 +15,30 @@
1415
})
1516
def build(c, cache=False):
1617
"""Build docker image for servers."""
17-
cache_opt = '' if cache else '--no-cache'
18-
c.run(f'{DOCKER_COMPOSE_COMMAND} build {cache_opt}', pty=True)
18+
cache_opt = '--no-cache' if not cache else ""
19+
cache_env_var = ""
20+
if cache and not os.environ.get("PRUNE_PYTHON_PACKAGE_CACHE"):
21+
files_to_cache = [
22+
# Community
23+
"requirements/docker.txt",
24+
25+
# Corporate
26+
"../readthedocs.org/requirements/docker.txt",
27+
"setup.cfg",
28+
29+
# Both
30+
"../ext-theme/setup.cfg",
31+
"../readthedocs-ext/setup.cfg",
32+
]
33+
34+
cache_hash = hashlib.md5()
35+
for f in files_to_cache:
36+
if os.path.exists(f):
37+
cache_hash.update(open(f, mode="rb").read())
38+
cache_hash = cache_hash.hexdigest()
39+
cache_env_var = f"PRUNE_PYTHON_PACKAGE_CACHE={cache_hash}"
40+
41+
c.run(f'{cache_env_var} {DOCKER_COMPOSE_COMMAND} build {cache_opt}', echo=True, pty=True)
1942

2043
@task(help={
2144
'command': 'Command to pass directly to "docker compose"',

0 commit comments

Comments
 (0)