-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_builder.py
71 lines (50 loc) · 2.16 KB
/
image_builder.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
from os.path import exists
import click
import src.config as config
import src.docker_tools as docker_tools
from python_on_whales import docker
@click.command()
@click.option("--image", "-i", default="aws", help="image to build")
@click.option("--version", "-v", default="1", help="image version")
@click.option("--debug", "-d", is_flag=True, help="debug")
def build(image, version, debug):
# Get env variables
env_conf = config.load_ci_env(debug)
# Get image configuration
try:
image_conf = config.load_image_config(image, version)
except KeyError as e:
print(e)
exit(1)
# Build dockerfile directory and path
dockerfile_directory = image
prefixed_dockerfile_path = f"{version}/Dockerfile"
# Set the subdirectory in path because we want dockerfile_directory (aka the build context) to be the parent image directory
dockerfile_path = prefixed_dockerfile_path if exists(
f"{dockerfile_directory}/{prefixed_dockerfile_path}") else "Dockerfile"
# Build image tags list (base tag + archs)
image_tags = config.get_image_tags(image, version, image_conf, env_conf)
with docker_tools.start_local_registry() as local_registry:
# Build, tag and push docker image to local registry
docker_tools.build_image(image_conf, image_tags["localname"], dockerfile_directory, dockerfile_path, debug)
# Run defined test command
docker_tools.run_image(image_tags["localname"], image_conf, debug)
# Push to registry in case of:
# - tag
# - push to master
# - nightly build
if (
env_conf["tag"] != ""
or (env_conf["event_type"] != "pull_request" and env_conf["branch"] == "master")
or env_conf["event_type"] == "schedule"
):
# Login to registry and push
docker_tools.login_to_registry(env_conf)
# Build, tag and push docker image to remote registry (Docker hub)
docker_tools.build_image(image_conf, image_tags["fullname"], dockerfile_directory, dockerfile_path, debug)
@click.group()
def cli():
pass
cli.add_command(build)
if __name__ == "__main__":
cli()