Skip to content

Commit eb4d396

Browse files
committed
feat(run-task): automatically interpolate $TASK_WORKDIR in environment
1 parent 5680f74 commit eb4d396

File tree

6 files changed

+44
-29
lines changed

6 files changed

+44
-29
lines changed

src/taskgraph/run-task/run-task

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,10 +1091,10 @@ def _display_python_version():
10911091

10921092

10931093
def main(args):
1094-
os.environ["TASK_WORKDIR"] = os.getcwd()
1094+
task_workdir = os.environ["TASK_WORKDIR"] = os.getcwd()
10951095
print_line(
10961096
b"setup",
1097-
b"run-task started in %s\n" % os.environ["TASK_WORKDIR"].encode("utf-8"),
1097+
b"run-task started in %s\n" % task_workdir.encode("utf-8"),
10981098
)
10991099
print_line(
11001100
b"setup",
@@ -1313,20 +1313,27 @@ def main(args):
13131313
for repo in repositories:
13141314
vcs_checkout_from_args(repo)
13151315

1316-
resource_process = None
1317-
1318-
try:
1319-
for k in ["MOZ_FETCHES_DIR", "UPLOAD_DIR"] + [
1320-
"{}_PATH".format(repository["project"].upper())
1321-
for repository in repositories
1322-
]:
1323-
if k in os.environ:
1324-
os.environ[k] = os.path.abspath(os.environ[k])
1316+
# Interpolate environment variables with defined substitution patterns. For
1317+
# example, the variable `CACHE_DIR={task_workdir}/.cache` will be
1318+
# interpolated with the task's working directory.
1319+
env_subs = {
1320+
"task_workdir": task_workdir,
1321+
}
1322+
for k, v in os.environ.items():
1323+
for subname, subval in env_subs.items():
1324+
# We check for an exact match rather than using a format string to
1325+
# avoid accidentally trying to interpolate environment variables
1326+
# that happen to contain brackets for some other reason.
1327+
pattern = f"{{{subname}}}"
1328+
if pattern in v:
1329+
os.environ[k] = v.replace(pattern, subval)
13251330
print_line(
13261331
b"setup",
13271332
b"%s is %s\n" % (k.encode("utf-8"), os.environ[k].encode("utf-8")),
13281333
)
13291334

1335+
resource_process = None
1336+
try:
13301337
if "MOZ_FETCHES" in os.environ:
13311338
fetch_artifacts()
13321339

src/taskgraph/transforms/run/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def cmp_artifacts(a):
367367
"task-reference": json.dumps(task_fetches, sort_keys=True)
368368
}
369369

370-
env.setdefault("MOZ_FETCHES_DIR", "fetches")
370+
env.setdefault("MOZ_FETCHES_DIR", "{task_workdir}/fetches")
371371

372372
yield task
373373

src/taskgraph/transforms/run/common.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import hashlib
1111
import json
1212

13+
from taskgraph.util import path
1314
from taskgraph.util.taskcluster import get_artifact_prefix
1415

1516

@@ -98,16 +99,16 @@ def support_vcs_checkout(config, task, taskdesc, repo_configs, sparse=False):
9899
assert is_mac or is_win or is_linux
99100

100101
if is_win:
101-
checkoutdir = "./build"
102+
checkoutdir = "build"
102103
hgstore = "y:/hg-shared"
103104
elif is_docker:
104105
checkoutdir = "{workdir}/checkouts".format(**task["run"])
105106
hgstore = f"{checkoutdir}/hg-store"
106107
else:
107-
checkoutdir = "./checkouts"
108+
checkoutdir = "checkouts"
108109
hgstore = f"{checkoutdir}/hg-shared"
109110

110-
vcsdir = checkoutdir + "/" + get_vcsdir_name(worker["os"])
111+
vcsdir = f"{checkoutdir}/{get_vcsdir_name(worker['os'])}"
111112
cache_name = "checkouts"
112113

113114
# Robust checkout does not clean up subrepositories, so ensure that tasks
@@ -138,7 +139,8 @@ def support_vcs_checkout(config, task, taskdesc, repo_configs, sparse=False):
138139
"REPOSITORIES": json.dumps(
139140
{repo.prefix: repo.name for repo in repo_configs.values()}
140141
),
141-
"VCS_PATH": vcsdir,
142+
# If vcsdir is already absolute this will return it unmodified.
143+
"VCS_PATH": path.join("{task_workdir}", vcsdir),
142144
}
143145
)
144146
for repo_config in repo_configs.values():
@@ -162,3 +164,5 @@ def support_vcs_checkout(config, task, taskdesc, repo_configs, sparse=False):
162164
# only some worker platforms have taskcluster-proxy enabled
163165
if task["worker"]["implementation"] in ("docker-worker",):
164166
taskdesc["worker"]["taskcluster-proxy"] = True
167+
168+
return vcsdir

src/taskgraph/transforms/run/run_task.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@ def common_setup(config, task, taskdesc, command):
7070
for (repo, config) in run["checkout"].items()
7171
}
7272

73-
support_vcs_checkout(
73+
vcs_path = support_vcs_checkout(
7474
config,
7575
task,
7676
taskdesc,
7777
repo_configs=repo_configs,
7878
sparse=bool(run["sparse-profile"]),
7979
)
8080

81-
vcs_path = taskdesc["worker"]["env"]["VCS_PATH"]
8281
for repo_config in repo_configs.values():
8382
checkout_path = path.join(vcs_path, repo_config.path)
8483
command.append(f"--{repo_config.prefix}-checkout={checkout_path}")

test/test_scripts_run_task.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def test_display_python_version_should_output_python_versions(run_task_mod, caps
438438

439439

440440
@pytest.fixture
441-
def run_main(tmp_path, capsys, mocker, mock_stdin, run_task_mod):
441+
def run_main(tmp_path, mocker, mock_stdin, run_task_mod):
442442
base_args = [
443443
f"--task-cwd={str(tmp_path)}",
444444
]
@@ -463,21 +463,26 @@ def inner(extra_args=None, env=None):
463463
args.extend(base_command_args)
464464

465465
result = run_task_mod.main(args)
466-
out, err = capsys.readouterr()
467-
return result, out, err, env
466+
return result, env
468467

469468
return inner
470469

471470

472471
def test_main_interpolate_environment(run_main):
473-
result, out, err, env = run_main(
474-
env={"MOZ_FETCHES_DIR": "file", "UPLOAD_DIR": "file", "FOO": "file"}
472+
result, env = run_main(
473+
env={
474+
"MOZ_FETCHES_DIR": "{task_workdir}/file",
475+
"UPLOAD_DIR": "$TASK_WORKDIR/file",
476+
"FOO": "{foo}/file",
477+
"BAR": "file",
478+
}
475479
)
476480
assert result == 0
477481

478482
assert env == {
479483
"MOZ_FETCHES_DIR": "/builds/worker/file",
480-
"UPLOAD_DIR": "/builds/worker/file",
481-
"FOO": "file",
484+
"UPLOAD_DIR": "$TASK_WORKDIR/file",
485+
"FOO": "{foo}/file",
486+
"BAR": "file",
482487
"TASK_WORKDIR": "/builds/worker",
483488
}

test/test_transforms_run_run_task.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def assert_generic_worker(task):
9999
"worker": {
100100
"command": [
101101
"C:/mozilla-build/python3/python3.exe run-task "
102-
'--ci-checkout=./build/src/ -- bash -cx "echo hello '
102+
'--ci-checkout=build/src/ -- bash -cx "echo hello '
103103
'world"'
104104
],
105105
"env": {
@@ -111,11 +111,11 @@ def assert_generic_worker(task):
111111
"HG_STORE_PATH": "y:/hg-shared",
112112
"MOZ_SCM_LEVEL": "1",
113113
"REPOSITORIES": '{"ci": "Taskgraph"}',
114-
"VCS_PATH": "./build/src",
114+
"VCS_PATH": "{task_workdir}/build/src",
115115
},
116116
"implementation": "generic-worker",
117117
"mounts": [
118-
{"cache-name": "checkouts", "directory": "./build"},
118+
{"cache-name": "checkouts", "directory": "build"},
119119
{
120120
"content": {
121121
"url": "https://tc-tests.localhost/api/queue/v1/task/<TASK_ID>/artifacts/public/run-task" # noqa
@@ -159,7 +159,7 @@ def assert_run_task_command_generic_worker(task):
159159
[
160160
"/foo/bar/python3",
161161
"run-task",
162-
"--ci-checkout=./checkouts/vcs/",
162+
"--ci-checkout=checkouts/vcs/",
163163
"--",
164164
"bash",
165165
"-cx",

0 commit comments

Comments
 (0)