-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_image.py
More file actions
123 lines (101 loc) · 3.84 KB
/
push_image.py
File metadata and controls
123 lines (101 loc) · 3.84 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
import os, sys
import subprocess
import argparse
from ipydex import IPS
import time
import git
"""
run
`python push_image.py -i <image_name> -v <x.y.z> -m "<message>"`
from the command line
"""
argparser = argparse.ArgumentParser()
argparser.add_argument("-i", "--image", help="image name", metavar="image")
argparser.add_argument("-v", "--version", help="version tag", metavar="version")
argparser.add_argument("-m", "--message", help="description of version", metavar="message")
args = argparser.parse_args()
image = args.image
# assert version is specified
assert args.version is not None, "no version tag specified"
version = args.version
assert len(version.split(".")) == 3, "version tag not in the form of major.minor.fix"
# assert message is specified
assert args.message is not None, "no message specified"
message = args.message
cmd = ["docker", "pull", f"ghcr.io/ackrep-org/{image}:{version}"]
res = subprocess.run(cmd, text=True, capture_output=True)
# print(res.stdout,"\n", res.stderr)
if "Pulling from" in res.stdout:
print("An image with this tag already exists. Pushing will overwrite the existing image.")
q = input("Continue? (y|N)")
if q != "y":
exit("Aborted.")
# manipulate dockerfile to write version description
#! this assumes a lot about naming conventions
root_path = os.path.dirname(__file__)
dockerfile_path = os.path.join(root_path, "dockerfiles/ackrep_core", f"Dockerfile_{image}")
assert os.path.isfile(dockerfile_path), f"Invalid image Name: {image}"
core_version = git.Git("../ackrep_core").log(-1).replace("\n", ", ")
deployment_version = git.Git("../ackrep_deployment").log(-1).replace("\n", ", ")
commit_message = f"{image}:{version}. | " + \
f" Core Version: {core_version} | " + \
f" Deployment Version: {deployment_version} | " + \
f" Message: {message}"
content = f'LABEL org.opencontainers.image.description "{commit_message}"'
print(f"Label of Dockerfile_{image} will look like this:\n\n{commit_message}\n")
q = input("Continue? (y|N)")
if q != "y":
exit("Aborted.")
with open(dockerfile_path, "r") as dockerfile:
lines = dockerfile.readlines()
with open(dockerfile_path, "a") as dockerfile:
dockerfile.write("\n" + content)
# rebuild image to incorporate description
start = time.time()
print("Rebuilding Image")
res = subprocess.run(["docker-compose", "build", image])
assert res.returncode == 0
if time.time() - start > 5:
print("Building took longer than expected.\n"\
+ "Are you sure you were testing with the correct/ most recent image version?")
q = input("Continue? (y|N)")
if q != "y":
# reset dockerfile
with open(dockerfile_path, "w") as dockerfile:
dockerfile.writelines(lines)
exit("Aborted.")
# get image id
prefix = "ackrep_deployment_"
image_name = prefix + image
res = subprocess.run(["docker", "images", image_name, "-q"], text=True, capture_output=True)
assert res.returncode == 0
id = res.stdout.replace("\n", "")
assert len(id) == 12, "unable to find image id"
# publish
repo_name = "ghcr.io/ackrep-org/"
# tag image with version
remote_name = repo_name + image + ":" + version
cmd = ["docker", "tag", id, remote_name]
res = subprocess.run(cmd)
assert res.returncode == 0
# push
print("Pushing to", remote_name)
cmd = ["docker", "image", "push", remote_name]
res = subprocess.run(cmd)
assert res.returncode == 0
# tag image with latest
remote_name = repo_name + image + ":" + "latest"
cmd = ["docker", "tag", id, remote_name]
res = subprocess.run(cmd)
assert res.returncode == 0
# push
print("Pushing to", remote_name)
cmd = ["docker", "image", "push", remote_name]
res = subprocess.run(cmd)
assert res.returncode == 0
# reset dockerfile
with open(dockerfile_path, "w") as dockerfile:
dockerfile.writelines(lines)
print("\nDone")
url = "https://github.com/orgs/ackrep-org/packages/container/package/" + image
print("Check the result:", url)