-
Notifications
You must be signed in to change notification settings - Fork 65
/
matrix_generator.py
71 lines (57 loc) · 2.07 KB
/
matrix_generator.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
import json
from glob import glob
from os.path import exists
import yaml
from git import Repo
matrix = {"include": []}
excluded_files = [ # Changes to those files shouldn't trigger a build
'.gitignore',
'CHANGELOG.md',
'README.md',
'.github/dependabot.yml',
]
def get_diff_files_list():
repo = Repo('.')
modified_files = repo.commit("origin/master").diff(repo.commit())
changedFiles = [item.a_path for item in modified_files]
return changedFiles
def filter_excluded_files(changedFiles):
filteredFiles = [
file for file in changedFiles if file not in excluded_files
]
return filteredFiles
def get_paths(changedFiles, unfilteredFiles):
paths = []
if changedFiles == []:
if unfilteredFiles == []:
# Master or tag job with no diff, builds everything
return glob("*/")
else:
# All files were previously excluded, builds nothing
return []
for file in changedFiles:
if "/" not in file or "/src" in file or ".github" in file:
return glob("*/")
else:
split_path = file.split("/")
paths.append(split_path[0])
return set(paths)
def generate_matrix(paths):
for image_folder in paths:
with open("base_config.yml", 'r') as base_config:
if exists("{}/config.yml".format(image_folder)):
with open("{}/config.yml".format(image_folder), 'r') as config:
full_config = base_config.read() + config.read()
image_config = yaml.safe_load(full_config)
for version in image_config["versions"]:
matrix["include"].append(
{
"image": image_folder.replace("/", ""),
"version": str(version)
}
)
print(json.dumps(matrix))
changedFiles = get_diff_files_list()
filteredFiles = filter_excluded_files(changedFiles)
paths = get_paths(filteredFiles, changedFiles)
generate_matrix(paths)