1616from tagging .apps .config import Config
1717from tagging .utils .get_platform import ALL_PLATFORMS
1818from tagging .utils .get_prefix import get_file_prefix_for_platform
19- from tagging .utils .git_helper import GitHelper
2019
2120docker = plumbum .local ["docker" ]
2221
2322LOGGER = logging .getLogger (__name__ )
2423
2524
26- def read_local_tags_from_files (config : Config ) -> tuple [ list [ str ], set [str ] ]:
25+ def read_local_tags_from_files (config : Config ) -> set [str ]:
2726 LOGGER .info (f"Read tags from file(s) for image: { config .image } " )
2827
29- all_local_tags = []
3028 merged_local_tags = set ()
3129 for platform in ALL_PLATFORMS :
3230 LOGGER .info (f"Reading tags for platform: { platform } " )
@@ -42,73 +40,67 @@ def read_local_tags_from_files(config: Config) -> tuple[list[str], set[str]]:
4240
4341 LOGGER .info (f"Tag file: { path } found" )
4442 for tag in path .read_text ().splitlines ():
45- all_local_tags .append (tag )
4643 merged_local_tags .add (tag .replace (platform + "-" , "" ))
4744
4845 LOGGER .info (f"Tags read for image: { config .image } " )
49- return all_local_tags , merged_local_tags
46+ return merged_local_tags
5047
5148
5249@retry (stop = stop_after_attempt (3 ), wait = wait_exponential (multiplier = 1 , min = 4 ))
53- def pull_tag (tag : str ) -> None :
54- LOGGER .info (f"Pulling tag: { tag } " )
55- docker ["pull " , tag ] & plumbum .FG
56- LOGGER .info (f"Tag { tag } pulled successfully " )
50+ def inspect_manifest (tag : str ) -> None :
51+ LOGGER .info (f"Inspecting manifest for tag: { tag } " )
52+ docker ["buildx" , "imagetools" , "inspect " , tag ] & plumbum .FG
53+ LOGGER .info (f"Manifest { tag } exists " )
5754
5855
59- def pull_missing_tags (merged_tag : str , all_local_tags : list [ str ] ) -> list [str ]:
60- existing_platform_tags = []
56+ def find_platform_tags (merged_tag : str ) -> list [str ]:
57+ platform_tags = []
6158
6259 for platform in ALL_PLATFORMS :
6360 platform_tag = merged_tag .replace (":" , f":{ platform } -" )
64- if platform_tag in all_local_tags :
65- LOGGER .info (
66- f"Tag { platform_tag } already exists locally, not pulling it from registry"
67- )
68- existing_platform_tags .append (platform_tag )
69- continue
70-
71- LOGGER .warning (f"Trying to pull: { platform_tag } from registry" )
61+ LOGGER .warning (f"Trying to inspect: { platform_tag } in the registry" )
7262 try :
73- pull_tag (platform_tag )
74- existing_platform_tags .append (platform_tag )
75- LOGGER .info (f"Tag { platform_tag } pulled successfully" )
63+ inspect_manifest (platform_tag )
64+ platform_tags .append (platform_tag )
65+ LOGGER .info (f"Tag { platform_tag } found successfully" )
7666 except RetryError :
77- LOGGER .warning (f"Pull failed, tag { platform_tag } doesn't exist" )
67+ LOGGER .warning (f"Manifest for tag { platform_tag } doesn't exist" )
7868
79- return existing_platform_tags
69+ return platform_tags
8070
8171
82- def merge_tags (
83- merged_tag : str , all_local_tags : list [str ], push_to_registry : bool
84- ) -> None :
72+ def merge_tags (merged_tag : str , push_to_registry : bool ) -> None :
8573 LOGGER .info (f"Trying to merge tag: { merged_tag } " )
8674
87- existing_platform_tags = pull_missing_tags (merged_tag , all_local_tags )
75+ platform_tags = find_platform_tags (merged_tag )
76+ if not platform_tags :
77+ assert not push_to_registry , (
78+ f"No platform tags found for merged tag: { merged_tag } , "
79+ "and push to registry is enabled. "
80+ "Cannot create a manifest for a non-existing image."
81+ )
82+ LOGGER .info (
83+ f"Not running merge for tag: { merged_tag } as no platform tags found"
84+ )
85+ return
86+
8887 args = [
8988 "buildx" ,
9089 "imagetools" ,
9190 "create" ,
92- * existing_platform_tags ,
91+ * platform_tags ,
9392 "--tag" ,
9493 merged_tag ,
9594 ]
9695 if not push_to_registry :
9796 args .append ("--dry-run" )
9897
99- commit_hash_tag = GitHelper .commit_hash_tag ()
100- if not push_to_registry and merged_tag .endswith (commit_hash_tag ):
101- LOGGER .info (
102- f"Not running merge for tag: { merged_tag } as it's a commit SHA tag and it wasn't pushed to registry"
103- )
104- return
105-
10698 LOGGER .info (f"Running command: { ' ' .join (args )} " )
10799 docker [args ] & plumbum .FG
108100 if push_to_registry :
109- LOGGER .info (f"Pushed merged tag: { merged_tag } to registry " )
101+ LOGGER .info (f"Pushed merged tag: { merged_tag } " )
110102 else :
111- LOGGER .info (f"Skipping push for tag: { merged_tag } " )
103+ LOGGER .info (f"Skipped push for tag: { merged_tag } " )
112104
113105
114106if __name__ == "__main__" :
@@ -119,8 +111,8 @@ def merge_tags(
119111
120112 LOGGER .info (f"Merging tags for image: { config .image } " )
121113
122- all_local_tags , merged_local_tags = read_local_tags_from_files (config )
114+ merged_local_tags = read_local_tags_from_files (config )
123115 for tag in merged_local_tags :
124- merge_tags (tag , all_local_tags , push_to_registry )
116+ merge_tags (tag , push_to_registry )
125117
126118 LOGGER .info (f"Successfully merged tags for image: { config .image } " )
0 commit comments