-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.py
More file actions
137 lines (111 loc) · 4.64 KB
/
github.py
File metadata and controls
137 lines (111 loc) · 4.64 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
# SPDX-FileCopyrightText: Alliander N. V.
#
# SPDX-License-Identifier: Apache-2.0
import argparse
import os
import sys
import requests
import utils
def select_components(components: str) -> None:
"""Selects components to build, used in the open PR workflow.
Args:
components (str): the components to select, either "all" or "changed".
"""
if components not in {"all", "changed"}:
sys.exit("Invalid argument to select_components, expected 'all' or 'changed'.")
ubuntu_components = set(utils.load_components("ubuntu_images").keys())
cuda_components = set(utils.load_components("cuda_images").keys())
changed_packages = utils.get_changed_packages(verbose=True)
changed_components = {p.removeprefix("alliander_") for p in changed_packages}
if not utils.is_core_files_changed() and components != "all":
ubuntu_components = ubuntu_components.intersection(changed_components)
cuda_components = cuda_components.intersection(changed_components)
variables = [
f"REBUILD_CORE={utils.is_core_docker_changed()}",
f"UBUNTU_COMPONENTS={list(ubuntu_components)}",
f"REBUILD_UBUNTU_IMAGES={bool(ubuntu_components)}",
f"CUDA_COMPONENTS={list(cuda_components)}",
f"REBUILD_CUDA_IMAGES={bool(cuda_components)}",
]
for variable in variables:
print(variable)
if os.environ.get("GITHUB_OUTPUT"):
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as fh:
print(variable, file=fh)
def remove_tags_on_docker_hub(token: str, closed_branch: str = "") -> None:
"""Removes tags on Docker Hub, used in the closed PR workflow.
Args:
token (str): the token to authenticate with the Docker Hub API.
closed_branch (str): the name of the closed branch.
"""
closed_branch = closed_branch if closed_branch else utils.get_git_branch()
print(f"Checking for tags on Docker Hub with name '{closed_branch}' to remove.")
request = requests.get("https://hub.docker.com/v2/repositories/allianderrobotics/")
number_of_repositories = request.json()["count"]
params = {"page_size": number_of_repositories}
request = requests.get(
"https://hub.docker.com/v2/repositories/allianderrobotics/", params=params
)
repositories = [repository["name"] for repository in request.json()["results"]]
print(f"Repositories: {repositories}")
successfull = True
successfull_status_code = 204
for repository in repositories:
request = requests.get(
f"https://hub.docker.com/v2/repositories/allianderrobotics/{repository}/tags/"
)
number_of_tags = request.json()["count"]
params = {"page_size": number_of_tags}
request = requests.get(
f"https://hub.docker.com/v2/repositories/allianderrobotics/{repository}/tags/",
params=params,
)
tags = [
tag["name"]
for tag in request.json()["results"]
if closed_branch in tag["name"]
]
if not tags:
continue
print("\nRepository:", repository)
print("Tags to remove:", tags)
for tag in tags:
headers = {"Accept": "application/json", "Authorization": f"JWT {token}"}
request = requests.delete(
f"https://hub.docker.com/v2/repositories/allianderrobotics/{repository}/tags/{tag}/",
headers=headers,
)
if request.status_code == successfull_status_code:
print(f"| Success | {request.status_code} | {tag} |")
else:
print(f"| Failure | {request.status_code} | {tag} |")
successfull = False
if not successfull:
sys.exit("Failed to remove all tags on Docker Hub.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Builds one or multiple Docker images."
)
parser.add_argument(
"--select-components",
required=False,
metavar="COMPONENTS",
help="Select components for the PR workflow.",
)
parser.add_argument(
"--remove-tags-on-docker-hub",
required=False,
metavar="TOKEN",
help="Remove tags on Docker Hub for the closed PR workflow.",
)
parser.add_argument(
"--branch",
required=False,
default="",
help="Pass the branch name to remove tags for, used in the closed PR workflow.",
)
args = parser.parse_args()
if args.select_components:
select_components(args.select_components)
elif args.remove_tags_on_docker_hub:
remove_tags_on_docker_hub(args.remove_tags_on_docker_hub, args.branch)