Skip to content

Commit 7877836

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 7877836

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

ci_test.py

Lines changed: 35 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,48 @@ 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+
72+
# Make everything relative to the root
73+
docker_dir = os.path.relpath(docker_dir, root_dir)
74+
dockerfile = os.path.relpath(dockerfile, root_dir)
75+
6776
print("Testing {}".format(dockerfile))
6877
sys.stdout.flush()
69-
log_file = dockerfile.replace(docker_dir,"").replace("/", "_")
78+
log_file = dockerfile.replace("/", "_")
7079
log_file = "{}.log".format(log_file)
71-
cmd = "docker build --no-cache=true -f {dockerfile} .".format(dockerfile=dockerfile)
80+
cmd = [
81+
'docker', 'build', '--no-cache=true',
82+
'-f', dockerfile,
83+
docker_dir
84+
]
7285
if "buildx" in dockerfile:
7386
# if "buildx" is part of the path, we want to use the new buildx build system and build
7487
# 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)
88+
run_command("docker buildx create --use", log_file=log_file)
89+
run_command("docker buildx inspect --bootstrap", log_file=log_file)
90+
91+
cmd = [
92+
'docker', 'buildx', 'build',
93+
'--platform', 'linux/arm64,linux/amd64',
94+
'--no-cache=true',
95+
'-f', dockerfile,
96+
docker_dir
97+
]
98+
99+
status = run_command(cmd, log_file=log_file)
81100
results[dockerfile] = status
82101
if status != 0:
83102
suite_status = False
84103
results[dockerfile] = "FAILED"
85104
else:
86105
results[dockerfile] = "PASSED"
87106

88-
cmd = "mv {log} {results}{log}".format(log=log_file, results=results[dockerfile])
107+
cmd = [
108+
'mv', log_file, results[dockerfile] + log_file
109+
]
89110
run_command(cmd)
90111
print("[{}] - {}".format(results[dockerfile], dockerfile))
91112
sys.stdout.flush()

0 commit comments

Comments
 (0)