forked from zoneminder-containers/eventserver-base
-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish.py
executable file
·231 lines (207 loc) · 6.29 KB
/
publish.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
"""Script to publish docker images and manifests."""
from __future__ import annotations
import argparse
import shlex
import subprocess
import sys
from dataclasses import dataclass
from os import getenv
ARCH_386 = "i386"
ARCH_AMD64 = "amd64"
ARCH_ARMV6 = "armv6"
ARCH_ARMV7 = "armv7"
ARCH_ARM64 = "arm64"
ARCHS = [ARCH_386, ARCH_AMD64, ARCH_ARMV7, ARCH_ARM64]
PUBLISH_LATEST = "{repo}:{arch}-latest"
PUBLISH_NAMES = [
"{repo}:{arch}{run_number}",
"{repo}:{arch}-{base_tag}",
"{repo}:{arch}-{base_tag}{run_number}",
"{repo}:{arch}-{base_tag}{github_sha}",
]
MANIFEST_NAMES = [
"{repo}:{run_number}",
"{repo}:{base_tag}",
"{repo}:{base_tag}{run_number}",
"{repo}:{base_tag}{github_sha}",
]
parser = argparse.ArgumentParser()
parser.add_argument("--tag", type=str, required=True, help="Base tag to publish")
parser.add_argument(
"--repo",
type=str,
required=True,
help="Repo to publish to (includes registry if not Docker Hub)",
)
parser.add_argument(
"--latest", required=False, action="store_true", help="Publish latest tag"
)
parser.add_argument(
"--dry-run",
required=False,
action="store_true",
help="Print commands only",
)
parser.add_argument("--ci", required=False, action="store_true", help="Running in CI")
parser.add_argument(
"--run-number",
type=str,
required=False,
default="",
help="Run number to append to tags if ci is not specified",
)
parser.add_argument(
"--github-sha",
type=str,
required=False,
default="",
help="GitHub SHA to append to tags if ci is not specified",
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--image", action="store_true", help="Publish architecture image to all tags"
)
group.add_argument(
"--manifest",
action="store_true",
help="Publish manifest to all tags",
)
opts, _ = parser.parse_known_args()
if opts.image:
parser.add_argument(
"--arch",
type=str,
required=True,
help="Architecture to publish",
choices=ARCHS,
)
parser.add_argument(
"--image-name",
type=str,
required=True,
help="Name of image to publish",
)
@dataclass(frozen=True)
class DockerNames:
"""Preformatted docker image names."""
ci_name: str | None
names: list
manifests: list
manifest_base: str | None
manifest_cmd_base: list | None
# Remove duplicates caused by empty run_number or github_sha
# Cleans docker command
@staticmethod
def _clean_list(duplicates: list) -> list:
cleaned = [x.replace(":-", ":") for x in duplicates if not x.endswith(":")]
cleaned = list(set(cleaned))
return cleaned
@classmethod
def from_args(cls, args):
"""Create names from args."""
if args.ci:
run_number = getenv("GITHUB_RUN_NUMBER")
github_sha = getenv("GITHUB_SHA")
else:
if args.run_number:
run_number = args.run_number
else:
run_number = ""
if args.github_sha:
github_sha = args.github_sha
else:
github_sha = ""
run_number = f"-{run_number}"
github_sha = f"-{github_sha}"
if not args.image:
preformatted_names = []
ci_name = None
else:
preformatted_names = [
publish_name.format(
repo=args.repo,
arch=args.arch,
base_tag=args.tag,
run_number=run_number,
github_sha=github_sha,
)
for publish_name in PUBLISH_NAMES
]
if args.latest:
preformatted_names.append(
PUBLISH_LATEST.format(repo=args.repo, arch=args.arch)
)
preformatted_names = cls._clean_list(preformatted_names)
ci_name = args.image_name
if not args.manifest:
preformatted_manifests = []
manifest_base = None
manifest_cmd_base = None
else:
preformatted_manifests = [
manifest_name.format(
repo=args.repo,
arch="{arch}",
base_tag=args.tag,
run_number=run_number,
github_sha=github_sha,
)
for manifest_name in MANIFEST_NAMES
]
if args.latest:
preformatted_manifests.append(f"{args.repo}:latest")
preformatted_manifests = cls._clean_list(preformatted_manifests)
manifest_base = PUBLISH_NAMES[0].format(
repo=args.repo,
arch="{arch}",
base_tag=args.tag,
run_number=run_number,
github_sha=github_sha,
)
manifest_cmd_base = [manifest_base.format(arch=arch) for arch in ARCHS]
return cls(
names=preformatted_names,
manifests=preformatted_manifests,
ci_name=ci_name,
manifest_base=manifest_base,
manifest_cmd_base=manifest_cmd_base,
)
def main():
"""Run main script."""
args = parser.parse_args()
docker_names = DockerNames.from_args(args)
def run_command(*command, ignore_error: bool = False):
"""Run command and print output."""
print(f"$ {shlex.join(list(command))}")
if not args.dry_run:
rc = subprocess.call(list(command))
if rc != 0 and not ignore_error:
print("Command failed")
sys.exit(1)
for name in docker_names.names:
cmd = [
"docker",
"tag",
docker_names.ci_name,
name,
]
run_command(*cmd)
cmd = ["docker", "push", name]
run_command(*cmd)
print(f"Published {name}")
for manifest in docker_names.manifests:
cmd = [
"docker",
"manifest",
"create",
"--amend",
manifest,
*docker_names.manifest_cmd_base,
]
run_command(*cmd)
cmd = ["docker", "manifest", "push", "--purge", manifest]
run_command(*cmd)
print(f"Published {manifest}")
if __name__ == "__main__":
main()