Skip to content

Commit

Permalink
[CI] Run Docker builds in the right directory.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
al45tair committed Sep 18, 2024
1 parent 1072ff1 commit 29a51a0
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions ci_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@
import urllib.request
import json
import subprocess
import shlex
import sys
import os


def run_command(cmd, log_file=None):
print("Running: {}".format(cmd))
if isinstance(cmd, str):
cmd = shlex.split(cmd)
print("Running: {}".format(shlex.join(cmd)))
sys.stdout.flush()
if log_file:
file = open(log_file, "w")
p = subprocess.Popen(cmd, shell=True, stdout=file, stderr=file)
else:
p = subprocess.Popen(cmd, shell=True)

file = None
p = subprocess.Popen(cmd, stdout=file, stderr=file)

(output, err) = p.communicate()
return p.wait()

Expand Down Expand Up @@ -62,30 +65,47 @@ def main():
results = {}
suite_status = True
dockerfiles = get_dockerfiles()
root_dir = os.path.dirname(os.path.realpath(__file__))
for dockerfile in dockerfiles:
docker_dir = os.path.dirname(os.path.realpath(__file__))
# Make sure everything is relative
dockerfile = os.path.relpath(os.path.realpath(dockerfile), root_dir)

docker_dir, docker_name = os.path.split(dockerfile)

print("Testing {}".format(dockerfile))
sys.stdout.flush()
log_file = dockerfile.replace(docker_dir,"").replace("/", "_")
log_file = dockerfile.replace("/", "_")
log_file = "{}.log".format(log_file)
cmd = "docker build --no-cache=true -f {dockerfile} .".format(dockerfile=dockerfile)
cmd = [
'docker', 'build', '--no-cache=true',
'-f', dockerfile,
docker_dir
]
if "buildx" in dockerfile:
# if "buildx" is part of the path, we want to use the new buildx build system and build
# for both amd64 and arm64.
cmd = "docker buildx create --use"
run_command(cmd, log_file)
cmd = "docker buildx inspect --bootstrap"
run_command(cmd, log_file)
cmd = "docker buildx build --platform linux/arm64,linux/amd64 --no-cache=true -f {dockerfile} .".format(dockerfile=dockerfile)
status = run_command(cmd, log_file)
run_command("docker buildx create --use", log_file=log_file)
run_command("docker buildx inspect --bootstrap", log_file=log_file)

cmd = [
'docker', 'buildx', 'build',
'--platform', 'linux/arm64,linux/amd64',
'--no-cache=true',
'-f', dockerfile,
docker_dir
]

status = run_command(cmd, log_file=log_file)
results[dockerfile] = status
if status != 0:
suite_status = False
results[dockerfile] = "FAILED"
else:
results[dockerfile] = "PASSED"

cmd = "mv {log} {results}{log}".format(log=log_file, results=results[dockerfile])
cmd = [
'mv', log_file, results[dockerfile] + log_file
]
run_command(cmd)
print("[{}] - {}".format(results[dockerfile], dockerfile))
sys.stdout.flush()
Expand Down

0 comments on commit 29a51a0

Please sign in to comment.