-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_and_push.py
157 lines (148 loc) · 4.28 KB
/
build_and_push.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""A script to build and push a docker image to a registry."""
import getpass
import os
import sys
DOCKER_REGISTRY = os.environ.get("DOCKER_REGISTRY", "docker.io")
DOCKER_USERNAME = os.environ.get("DOCKER_USERNAME", "ukalwa")
DOCKER_PASSWORD = os.environ.get("DOCKER_PASSWORD")
IS_CI = os.environ.get("CI", None) == "true"
PUSH = os.environ.get("PUSH", None) == "true"
DEBUG = os.environ.get("DEBUG", None) == "true"
# Build and push base images
base_images = ["alpine-base", "ubuntu-base"]
for image in base_images:
print(f"Building {image}")
command = [
"docker",
"build",
"-t",
f"{DOCKER_USERNAME}/{image}",
"-f",
f"{image}/Dockerfile",
".",
]
exit_code = os.system(" ".join(command))
if exit_code != 0:
print(f"Error building {image}")
sys.exit(1)
if not PUSH:
print(f"Skipping push for {image}")
continue
if not DOCKER_PASSWORD:
if IS_CI:
raise Exception("DOCKER_PASSWORD is not set")
DOCKER_PASSWORD = getpass.getpass(prompt="Docker password: ")
# Login to docker
command = [
"docker",
"login",
"-u",
DOCKER_USERNAME,
"-p",
DOCKER_PASSWORD,
DOCKER_REGISTRY,
]
exit_code = os.system(" ".join(command))
if exit_code != 0:
print(f"Error logging in to {DOCKER_REGISTRY}")
sys.exit(1)
print(f"Pushing {image}")
command = ["docker", "push", f"{DOCKER_USERNAME}/{image}"]
exit_code = os.system(" ".join(command))
if exit_code != 0:
sys.exit(1)
# Build and push nodejs images
node_alpine_images = [
"node:alpine-lts",
"node:alpine-16.15.1",
"node:alpine-14.19.3",
]
for image in node_alpine_images:
name, alpine_tag = image.split(":")
NODE_VERSION = alpine_tag.replace("alpine", "").replace("-", "")
print(f"Building {image}")
command = [
"docker",
"build",
"-t",
f"{DOCKER_USERNAME}/{image}",
"-f",
f"{name}/Dockerfile",
"--build-arg",
f"NODE_VERSION={'lts/*' if NODE_VERSION == 'lts' else NODE_VERSION}",
".",
]
exit_code = os.system(" ".join(command))
if exit_code != 0:
print(f"Error building {image}")
sys.exit(1)
print(f"Building {name}:{NODE_VERSION}")
command = [
"docker",
"build",
"-t",
f"{DOCKER_USERNAME}/{name}:{NODE_VERSION}",
"-f",
f"{name}/Dockerfile",
"--build-arg",
"IMAGE=ubuntu",
"--build-arg",
f"NODE_VERSION={'lts/*' if NODE_VERSION == 'lts' else NODE_VERSION}",
".",
]
exit_code = os.system(" ".join(command))
if exit_code != 0:
print(f"Error building {name}:{NODE_VERSION}")
sys.exit(1)
if not PUSH:
print(f"Skipping push for {image}")
continue
print(f"Pushing {image}")
command = ["docker", "push", f"{DOCKER_USERNAME}/{image}"]
exit_code = os.system(" ".join(command))
if exit_code != 0:
print(f"Error pushing {image}")
sys.exit(1)
if not PUSH:
print(f"Skipping push for {image}")
continue
print(f"Pushing {name}:{NODE_VERSION}")
command = ["docker", "push", f"{DOCKER_USERNAME}/{name}:{NODE_VERSION}"]
exit_code = os.system(" ".join(command))
if exit_code != 0:
print(f"Error pushing {name}:{NODE_VERSION}")
sys.exit(1)
# Build and push python images
python_images = [
"python:alpine-3.10",
"python:3.10",
"python:alpine",
"python:latest",
]
for image in python_images:
name, version = image.split(":")
print(f"Building {image}")
command = [
"docker",
"build",
"-t",
f"{DOCKER_USERNAME}/{image}",
"--build-arg",
f"IMAGE={'alpine' if 'alpine' in version else 'ubuntu'}",
"-f",
f"{name}/Dockerfile",
".",
]
exit_code = os.system(" ".join(command))
if exit_code != 0:
print(f"Error building {image}")
sys.exit(1)
if not PUSH:
print(f"Skipping push for {image}")
continue
print(f"Pushing {image}")
command = ["docker", "push", f"{DOCKER_USERNAME}/{image}"]
exit_code = os.system(" ".join(command))
if exit_code != 0:
print(f"Error pushing {image}")
sys.exit(1)