Skip to content

Commit 29a51a0

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 29a51a0

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

ci_test.py

Lines changed: 34 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,47 @@ 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+
# Make sure everything is relative
71+
dockerfile = os.path.relpath(os.path.realpath(dockerfile), root_dir)
72+
73+
docker_dir, docker_name = os.path.split(dockerfile)
74+
6775
print("Testing {}".format(dockerfile))
6876
sys.stdout.flush()
69-
log_file = dockerfile.replace(docker_dir,"").replace("/", "_")
77+
log_file = dockerfile.replace("/", "_")
7078
log_file = "{}.log".format(log_file)
71-
cmd = "docker build --no-cache=true -f {dockerfile} .".format(dockerfile=dockerfile)
79+
cmd = [
80+
'docker', 'build', '--no-cache=true',
81+
'-f', dockerfile,
82+
docker_dir
83+
]
7284
if "buildx" in dockerfile:
7385
# if "buildx" is part of the path, we want to use the new buildx build system and build
7486
# 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)
87+
run_command("docker buildx create --use", log_file=log_file)
88+
run_command("docker buildx inspect --bootstrap", log_file=log_file)
89+
90+
cmd = [
91+
'docker', 'buildx', 'build',
92+
'--platform', 'linux/arm64,linux/amd64',
93+
'--no-cache=true',
94+
'-f', dockerfile,
95+
docker_dir
96+
]
97+
98+
status = run_command(cmd, log_file=log_file)
8199
results[dockerfile] = status
82100
if status != 0:
83101
suite_status = False
84102
results[dockerfile] = "FAILED"
85103
else:
86104
results[dockerfile] = "PASSED"
87105

88-
cmd = "mv {log} {results}{log}".format(log=log_file, results=results[dockerfile])
106+
cmd = [
107+
'mv', log_file, results[dockerfile] + log_file
108+
]
89109
run_command(cmd)
90110
print("[{}] - {}".format(results[dockerfile], dockerfile))
91111
sys.stdout.flush()

0 commit comments

Comments
 (0)