Skip to content

Commit d6ce8ac

Browse files
committed
load-task: allow specifying caches on the command line
When debugging a task it can be useful to re-use e.g. the checkout cache between load-task invocations, to not have to clone from scratch each time.
1 parent ee9db51 commit d6ce8ac

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/taskgraph/docker.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,18 @@ def load_task(
523523
env.update(task_def["payload"].get("env", {})) # type: ignore
524524

525525
# run-task expects the worker to mount a volume for each path defined in
526-
# TASKCLUSTER_CACHES, delete them to avoid needing to do the same.
526+
# TASKCLUSTER_CACHES; delete them to avoid needing to do the same, unless
527+
# they're passed in as volumes.
527528
if "TASKCLUSTER_CACHES" in env:
528-
del env["TASKCLUSTER_CACHES"]
529+
if volumes:
530+
caches = env["TASKCLUSTER_CACHES"].split(";")
531+
caches = [cache for cache in caches if cache in volumes.values()]
532+
else:
533+
caches = []
534+
if caches:
535+
env["TASKCLUSTER_CACHES"] = ";".join(caches)
536+
else:
537+
del env["TASKCLUSTER_CACHES"]
529538

530539
envfile = None
531540
initfile = None

src/taskgraph/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,14 @@ def image_digest(args):
792792
default="taskcluster",
793793
help="Relative path to the root of the Taskgraph definition.",
794794
)
795+
@argument(
796+
"--volume",
797+
"-v",
798+
metavar="HOST_DIR:CONTAINER_DIR",
799+
default=[],
800+
action="append",
801+
help="Mount local path into the container.",
802+
)
795803
def load_task(args):
796804
from taskgraph.config import load_graph_config # noqa: PLC0415
797805
from taskgraph.docker import load_task # noqa: PLC0415
@@ -806,6 +814,11 @@ def load_task(args):
806814
except ValueError:
807815
args["task"] = data # assume it is a taskId
808816

817+
volumes = {}
818+
for vol in args["volume"]:
819+
k, v = vol.split(":", 1)
820+
volumes[k] = v
821+
809822
root = args["root"]
810823
graph_config = load_graph_config(root)
811824
return load_task(
@@ -815,6 +828,7 @@ def load_task(args):
815828
remove=args["remove"],
816829
user=args["user"],
817830
custom_image=args["image"],
831+
volumes=volumes,
818832
)
819833

820834

test/test_docker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ def test_load_task_env_init_and_remove(mocker, run_load_task):
216216
"--",
217217
"echo foo",
218218
],
219-
"env": {"FOO": "BAR", "BAZ": "1", "TASKCLUSTER_CACHES": "path"},
219+
"env": {"FOO": "BAR", "BAZ": "1", "TASKCLUSTER_CACHES": "/path;/cache"},
220220
"image": {"taskId": image_task_id, "type": "task-image"},
221221
},
222222
}
223-
ret, mocks = run_load_task(task, remove=True)
223+
ret, mocks = run_load_task(task, remove=True, volumes={"/host/path": "/cache"})
224224
assert ret == 0
225225

226226
# NamedTemporaryFile was called twice (once for env, once for init)
@@ -231,7 +231,7 @@ def test_load_task_env_init_and_remove(mocker, run_load_task):
231231
env_lines = written_env_content[0].split("\n")
232232

233233
# Verify written env is expected
234-
assert "TASKCLUSTER_CACHES=path" not in env_lines
234+
assert "TASKCLUSTER_CACHES=/cache" in env_lines
235235
assert "FOO=BAR" in env_lines
236236
assert "BAZ=1" in env_lines
237237

test/test_main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ def test_load_task_command(run_load_task):
466466
remove=True,
467467
user=None,
468468
custom_image=None,
469+
volumes={},
469470
)
470471

471472
# Test with interactive flag
@@ -479,6 +480,7 @@ def test_load_task_command(run_load_task):
479480
remove=True,
480481
user=None,
481482
custom_image=None,
483+
volumes={},
482484
)
483485

484486

@@ -503,6 +505,7 @@ def test_load_task_command_with_stdin(run_load_task):
503505
remove=True,
504506
user=None,
505507
custom_image=None,
508+
volumes={},
506509
)
507510

508511

@@ -520,6 +523,7 @@ def test_load_task_command_with_task_id(run_load_task):
520523
remove=True,
521524
user=None,
522525
custom_image=None,
526+
volumes={},
523527
)
524528

525529

0 commit comments

Comments
 (0)