forked from lucee/lucee-dockerfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-images.py
More file actions
executable file
·178 lines (139 loc) · 5.5 KB
/
build-images.py
File metadata and controls
executable file
·178 lines (139 loc) · 5.5 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
from collections import namedtuple
import argparse
import subprocess
import sys
import os
import re
import yaml
import attr
@attr.s(frozen=True)
class Config(object):
LUCEE_MINOR = attr.ib()
LUCEE_SERVER = attr.ib()
LUCEE_VARIANT = attr.ib()
TOMCAT_VERSION = attr.ib()
TOMCAT_JAVA_VERSION = attr.ib()
TOMCAT_BASE_IMAGE = attr.ib()
def flatten(lst):
return [x for y in lst for x in y]
def is_release_build(ver):
# release builds are a version number string only
# non-release builds contain a build type suffix such as -BETA/-RC/-SNAPSHOT
return re.sub(r"^\d+\.\d+\.\d+\.\d+(.*)$", r"\1", ver) == ""
def get_minor_version(ver):
return re.sub(r"^(\d+\.\d+).*", r"\1", ver)
def get_jar_url(ver, variant):
if variant == '-light':
return f"https://release.lucee.org/rest/update/provider/light/{ver}"
else:
return f"https://release.lucee.org/rest/update/provider/loader/{ver}"
def run(cmd):
return subprocess.run(cmd, check=True, universal_newlines=True)
def tomcat(config):
return f"tomcat{config.TOMCAT_VERSION}-{config.TOMCAT_JAVA_VERSION}{config.TOMCAT_BASE_IMAGE}"
def discover_images():
LUCEE_MINORS = os.getenv('LUCEE_MINOR').split(',')
LUCEE_SERVERS = os.getenv('LUCEE_SERVER').split(',')
LUCEE_VARIANTS = os.getenv('LUCEE_VARIANTS').split(',')
TOMCAT_VERSION = os.getenv('TOMCAT_VERSION')
TOMCAT_JAVA_VERSION = os.getenv('TOMCAT_JAVA_VERSION')
TOMCAT_BASE_IMAGE = os.getenv('TOMCAT_BASE_IMAGE')
for LUCEE_MINOR in LUCEE_MINORS:
for LUCEE_SERVER in LUCEE_SERVERS:
for LUCEE_VARIANT in LUCEE_VARIANTS:
yield Config(
LUCEE_MINOR=LUCEE_MINOR,
LUCEE_SERVER=LUCEE_SERVER,
LUCEE_VARIANT=LUCEE_VARIANT,
TOMCAT_VERSION=TOMCAT_VERSION,
TOMCAT_JAVA_VERSION=TOMCAT_JAVA_VERSION,
TOMCAT_BASE_IMAGE=TOMCAT_BASE_IMAGE,
)
def find_tags_for_image(config, default_tomcat, tags):
yield f"{os.getenv('LUCEE_VERSION')}{config.LUCEE_VARIANT}{config.LUCEE_SERVER}-{tomcat(config)}"
is_default_tomcat = \
config.TOMCAT_JAVA_VERSION == default_tomcat['TOMCAT_JAVA_VERSION'] and \
config.TOMCAT_VERSION == default_tomcat['TOMCAT_VERSION'] and \
config.TOMCAT_BASE_IMAGE == default_tomcat['TOMCAT_BASE_IMAGE']
if is_default_tomcat:
yield f"{os.getenv('LUCEE_VERSION')}{config.LUCEE_VARIANT}{config.LUCEE_SERVER}"
config_dict = attr.asdict(config)
# only apply plain tags to release builds (exclude plain tags for non-release builds)
if is_release_build(os.getenv('LUCEE_VERSION')):
yield from [
tag_name
for tag_name, tag_requirements in tags.items()
if all([config_dict[key] == tag_requirements[key] for key in set(config_dict.keys())])
]
def config_to_build_args(config, namespace, image_name):
if config.LUCEE_SERVER == '':
build_args = {**attr.asdict(config), 'LUCEE_MINOR': config.LUCEE_MINOR, 'LUCEE_JAR_URL': get_jar_url(os.getenv('LUCEE_VERSION'), config.LUCEE_VARIANT)}
elif config.LUCEE_SERVER == '-nginx':
build_args = {'LUCEE_IMAGE': f"{namespace}/{image_name}:{os.getenv('LUCEE_VERSION')}{config.LUCEE_VARIANT}-{tomcat(config)}"}
else:
build_args = {}
for key, value in build_args.items():
yield from ['--build-arg', f"{key}={value}"]
def pick_dockerfile(config):
if config.LUCEE_SERVER == '-nginx':
return './Dockerfile.nginx'
else:
return './Dockerfile'
def main():
parser = argparse.ArgumentParser(description='Start the build process.')
parser.add_argument('version', nargs='?', default=os.getenv('LUCEE_VERSION'),
help='the version string to build (default: $LUCEE_VERSION)')
parser.add_argument('--no-build', dest='build', action='store_false', default=True,
help='do not run the build')
parser.add_argument('--no-cache', dest='cache', action='store_false', default=True,
help='do not use the cache when building')
parser.add_argument('--no-push', dest='push', action='store_false', default=True,
help='do not push the tags')
parser.add_argument('--list-tags', action='store_true', default=False,
help='only list the tags that would be generated')
args = parser.parse_args()
if args.list_tags:
args.push = False
args.build = False
if args.version == None:
print("version argument missing or $LUCEE_VERSION not set")
sys.exit(1)
with open('./matrix.yaml') as matrix_file:
matrix = yaml.safe_load(matrix_file)
is_master_build = os.getenv('TRAVIS_PULL_REQUEST', None) == 'false'
if os.getenv('CI', None):
print('will we deploy:', 'yes' if is_master_build else 'no')
namespace = matrix['config']['docker_hub_namespace']
image_name = matrix['config']['docker_hub_image']
for config in discover_images():
if config.LUCEE_MINOR == get_minor_version(os.getenv('LUCEE_VERSION')):
docker_args = ["--pull"]
if args.cache == False:
docker_args.append("--no-cache")
build_args = list(config_to_build_args(config, namespace=namespace, image_name=image_name))
dockerfile = pick_dockerfile(config)
tags = find_tags_for_image(config, default_tomcat=matrix['tags'][config.LUCEE_MINOR], tags=matrix['tags'])
if args.list_tags:
print(", ".join(tags))
continue
plain_tags = [f"{namespace}/{image_name}:{tag}" for tag in tags]
tag_args = flatten([["-t", tag] for tag in plain_tags])
command = [
"docker", "build", *docker_args,
*build_args,
"-f", dockerfile,
*tag_args,
".",
]
print(' '.join(command))
if args.build:
run(command)
for tag in plain_tags:
if is_master_build and args.push:
print('pushing', tag)
run(["docker", "push", tag])
else:
print('not on master; skipping deployment of', tag)
if __name__ == '__main__':
main()