Skip to content

Commit

Permalink
Development: use --build-arg to invalidate cache (#244)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
humitos authored Dec 4, 2024
1 parent 1695f38 commit 1e5d46c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions dockerfiles/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
build:
context: ${PWD}
dockerfile: ${PWD}/dockerfiles/Dockerfile
args:
PRUNE_PYTHON_PACKAGE_CACHE: ${PRUNE_PYTHON_PACKAGE_CACHE:-0}

nginx:
stop_grace_period: 1s
Expand Down
27 changes: 25 additions & 2 deletions dockerfiles/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import hashlib

from invoke import task

Expand All @@ -14,8 +15,30 @@
})
def build(c, cache=False):
"""Build docker image for servers."""
cache_opt = '' if cache else '--no-cache'
c.run(f'{DOCKER_COMPOSE_COMMAND} build {cache_opt}', pty=True)
cache_opt = '--no-cache' if not cache else ""
cache_env_var = ""
if cache and not os.environ.get("PRUNE_PYTHON_PACKAGE_CACHE"):
files_to_cache = [
# Community
"requirements/docker.txt",

# Corporate
"../readthedocs.org/requirements/docker.txt",
"setup.cfg",

# Both
"../ext-theme/setup.cfg",
"../readthedocs-ext/setup.cfg",
]

cache_hash = hashlib.md5()
for f in files_to_cache:
if os.path.exists(f):
cache_hash.update(open(f, mode="rb").read())
cache_hash = cache_hash.hexdigest()
cache_env_var = f"PRUNE_PYTHON_PACKAGE_CACHE={cache_hash}"

c.run(f'{cache_env_var} {DOCKER_COMPOSE_COMMAND} build {cache_opt}', echo=True, pty=True)

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

0 comments on commit 1e5d46c

Please sign in to comment.