Skip to content

Commit fb94d05

Browse files
committed
[CI] Run Docker builds in the right directory.
Make sure we run the Docker builds in the directory containing the Dockerfile. Also, prefer lists of arguments rather than strings, especially when including filenames.
1 parent 1072ff1 commit fb94d05

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

ci_test.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616
import urllib.request
1717
import json
1818
import subprocess
19+
import shlex
1920
import sys
2021
import os
2122

2223

2324
def run_command(cmd, log_file=None):
24-
print("Running: {}".format(cmd))
25+
if isinstance(cmd, str):
26+
cmd = shlex.split(cmd)
27+
print("Running: {}".format(shlex.join(cmd)))
2528
sys.stdout.flush()
2629
if log_file:
2730
file = open(log_file, "w")
28-
p = subprocess.Popen(cmd, shell=True, stdout=file, stderr=file)
2931
else:
30-
p = subprocess.Popen(cmd, shell=True)
31-
32+
file = None
33+
p = subprocess.Popen(cmd, stdout=file, stderr=file)
34+
3235
(output, err) = p.communicate()
3336
return p.wait()
3437

@@ -62,30 +65,44 @@ def main():
6265
results = {}
6366
suite_status = True
6467
dockerfiles = get_dockerfiles()
68+
root_dir = os.path.dirname(os.path.realpath(__file__))
6569
for dockerfile in dockerfiles:
66-
docker_dir = os.path.dirname(os.path.realpath(__file__))
70+
docker_dir, docker_name = os.path.split(os.path.realpath(dockerfile))
71+
6772
print("Testing {}".format(dockerfile))
6873
sys.stdout.flush()
69-
log_file = dockerfile.replace(docker_dir,"").replace("/", "_")
74+
log_file = os.path.relpath(dockerfile, root_dir).replace("/", "_")
7075
log_file = "{}.log".format(log_file)
71-
cmd = "docker build --no-cache=true -f {dockerfile} .".format(dockerfile=dockerfile)
76+
cmd = [
77+
'docker', 'build', '--no-cache=true',
78+
'-f', dockerfile,
79+
docker_dir
80+
]
7281
if "buildx" in dockerfile:
7382
# if "buildx" is part of the path, we want to use the new buildx build system and build
7483
# for both amd64 and arm64.
75-
cmd = "docker buildx create --use"
76-
run_command(cmd, log_file)
77-
cmd = "docker buildx inspect --bootstrap"
78-
run_command(cmd, log_file)
79-
cmd = "docker buildx build --platform linux/arm64,linux/amd64 --no-cache=true -f {dockerfile} .".format(dockerfile=dockerfile)
80-
status = run_command(cmd, log_file)
84+
run_command("docker buildx create --use", log_file=log_file)
85+
run_command("docker buildx inspect --bootstrap", log_file=log_file)
86+
87+
cmd = [
88+
'docker', 'buildx', 'build',
89+
'--platform', 'linux/arm64,linux/amd64',
90+
'--no-cache=true',
91+
'-f', dockerfile,
92+
docker_dir
93+
]
94+
95+
status = run_command(cmd, log_file=log_file)
8196
results[dockerfile] = status
8297
if status != 0:
8398
suite_status = False
8499
results[dockerfile] = "FAILED"
85100
else:
86101
results[dockerfile] = "PASSED"
87102

88-
cmd = "mv {log} {results}{log}".format(log=log_file, results=results[dockerfile])
103+
cmd = [
104+
'mv', log_file, results[dockerfile] + log_file
105+
]
89106
run_command(cmd)
90107
print("[{}] - {}".format(results[dockerfile], dockerfile))
91108
sys.stdout.flush()

0 commit comments

Comments
 (0)