-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·97 lines (73 loc) · 2.52 KB
/
build.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
#!/usr/bin/env python3
"""
Expected to have following env vars:
IMAGE_NAME
IMAGE_VERSION
GH_IMAGE_ID
REPO_OWNER
And a bunch of org.opencontainers.image metadata
assignments in a file "oc_labels".
"""
import os
import subprocess
import sys
try:
repo_owner = os.environ["REPO_OWNER"]
except KeyError:
repo_owner = "arranstewart"
try:
image_name = os.environ["IMAGE_NAME"]
except KeyError as ex:
raise Exception("expected env var IMAGE_NAME to be defined") from ex
version = os.environ["IMAGE_VERSION"]
gh_image_id = os.environ["GH_IMAGE_ID"]
# org.opencontainers.image metadata
oc_labels = {}
with open("oc_labels", encoding="utf8") as infile:
for line in infile.readlines():
k, v = line.strip().split(sep="=", maxsplit=1)
oc_labels[k] = v
# override version
oc_labels["org.opencontainers.image.version"] = version
# org.label-schema metadata
ls_labels = { "org.label-schema.schema-version": "1.0",
"org.label-schema.build-date": oc_labels["org.opencontainers.image.created"],
"org.label-schema.name": f"{repo_owner}/{image_name}",
"org.label-schema.description": oc_labels["org.opencontainers.image.description"],
"org.label-schema.vcs-url": oc_labels["org.opencontainers.image.url"],
"org.label-schema.vcs-ref": oc_labels["org.opencontainers.image.revision"],
"org.label-schema.version": version }
def verbose_run(cmd, **kwargs):
print("running: ", cmd, file=sys.stderr)
sys.stderr.flush()
sys.stdout.flush()
subprocess.run(cmd, **kwargs)
# pull existing images if there
#cmd = ["docker", "pull", f"{gh_image_id}:{version}-builder"]
#verbose_run(cmd, check=False)
cmd = ["docker", "pull", f"{gh_image_id}:{version}"]
verbose_run(cmd, check=False)
cmd = ["docker", "pull", f"{gh_image_id}:latest"]
verbose_run(cmd, check=False)
## builder image
#cmd = f"""docker build --pull -f Dockerfile --target builder
# --cache-from {gh_image_id}:{version}-builder
# -t {gh_image_id}:{version}-builder .""".split()
#
#verbose_run(cmd, check=True)
dockerfile="Dockerfile"
# main image
cmd = f"""docker build --pull -f {dockerfile}
--cache-from {gh_image_id}:{version}-builder
--cache-from {gh_image_id}:latest
--cache-from {gh_image_id}:{version}
-t {gh_image_id}:{version}""".split()
# build up --label args
for k in oc_labels:
val = oc_labels[k]
cmd += ["--label", f"{k}={val}"]
for k in ls_labels:
val = ls_labels[k]
cmd += ["--label", f"{k}={val}"]
cmd += ["."]
verbose_run(cmd, check=True)