-
Notifications
You must be signed in to change notification settings - Fork 182
/
ci_test.py
executable file
·126 lines (104 loc) · 3.79 KB
/
ci_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ci_test - Build all Dockerfiles -*- python -*-
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2024 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
from __future__ import absolute_import, print_function, unicode_literals
import urllib.request
import json
import subprocess
import shlex
import sys
import os
def run_command(cmd, log_file=None):
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")
else:
file = None
p = subprocess.Popen(cmd, stdout=file, stderr=file)
(output, err) = p.communicate()
return p.wait()
def get_dockerfiles():
dockerfiles = []
GITHUB_API_URL = "https://api.github.com"
response = urllib.request.urlopen("{}/repos/{}/pulls/{}/files".format(GITHUB_API_URL,
os.environ['ghprbGhRepository'],
os.environ['ghprbPullId']))
data = json.load(response)
for file_info in data:
filename = file_info['filename']
print(filename)
if "Dockerfile" in filename and not "windows" in filename:
dockerfiles.append(filename)
return dockerfiles
def print_results(results):
sorted(results.items(), key=lambda x: x[1])
print("=======================")
for dockerfile in results:
print("{}: {}".format(dockerfile, results[dockerfile]))
print("=======================")
def main():
print("--- Running Docker Tests ---")
results = {}
suite_status = True
dockerfiles = get_dockerfiles()
root_dir = os.path.dirname(os.path.realpath(__file__))
for dockerfile in dockerfiles:
# Make sure everything is relative
dockerfile = os.path.relpath(os.path.realpath(dockerfile), root_dir)
docker_dir = "."
print("Testing {}".format(dockerfile))
sys.stdout.flush()
log_file = dockerfile.replace("/", "_")
log_file = "{}.log".format(log_file)
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.
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_file, results[dockerfile] + log_file
]
run_command(cmd)
print("[{}] - {}".format(results[dockerfile], dockerfile))
sys.stdout.flush()
run_command("docker image prune -f")
for dockerfile in dockerfiles:
if results[dockerfile] == "FAILED":
print("[{}] - {}".format(results[dockerfile], dockerfile))
if suite_status == False:
sys.exit(1)
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
sys.exit(1)