|
2 | 2 | # Copyright (c) Jupyter Development Team.
|
3 | 3 | # Distributed under the terms of the Modified BSD License.
|
4 | 4 | import logging
|
| 5 | +import os |
5 | 6 |
|
6 | 7 | import plumbum
|
7 | 8 |
|
@@ -36,36 +37,46 @@ def read_tags_from_files(config: Config) -> set[str]:
|
36 | 37 | return tags
|
37 | 38 |
|
38 | 39 |
|
39 |
| -def merge_tags(config: Config) -> None: |
40 |
| - LOGGER.info(f"Merging tags for image: {config.image}") |
41 |
| - |
42 |
| - all_tags = read_tags_from_files(config) |
43 |
| - for tag in all_tags: |
44 |
| - LOGGER.info(f"Trying to merge tag: {tag}") |
45 |
| - existing_images = [] |
46 |
| - for platform in ALL_PLATFORMS: |
47 |
| - image_with_platform = tag.replace(":", f":{platform}-") |
48 |
| - LOGGER.info(f"Trying to pull: {image_with_platform}") |
49 |
| - try: |
50 |
| - docker["pull", image_with_platform] & plumbum.FG |
51 |
| - existing_images.append(image_with_platform) |
52 |
| - LOGGER.info("Pull success") |
53 |
| - except plumbum.ProcessExecutionError: |
54 |
| - LOGGER.info( |
55 |
| - "Pull failed, image with this tag and platform doesn't exist" |
56 |
| - ) |
57 |
| - |
58 |
| - LOGGER.info(f"Found images: {existing_images}") |
59 |
| - docker["manifest", "create", tag][existing_images] & plumbum.FG |
| 40 | +def merge_tags(tag: str, push_to_registry: bool) -> None: |
| 41 | + LOGGER.info(f"Trying to merge tag: {tag}") |
| 42 | + all_platform_tags = [] |
| 43 | + for platform in ALL_PLATFORMS: |
| 44 | + platform_tag = tag.replace(":", f":{platform}-") |
| 45 | + LOGGER.info(f"Trying to pull: {platform_tag}") |
| 46 | + try: |
| 47 | + docker["pull", platform_tag] & plumbum.FG |
| 48 | + all_platform_tags.append(platform_tag) |
| 49 | + LOGGER.info("Pull success") |
| 50 | + except plumbum.ProcessExecutionError: |
| 51 | + LOGGER.info("Pull failed, image with this tag and platform doesn't exist") |
| 52 | + |
| 53 | + LOGGER.info(f"Found images: {all_platform_tags}") |
| 54 | + try: |
| 55 | + docker["manifest", "rm", tag] & plumbum.FG |
| 56 | + LOGGER.info(f"Manifest {tag} already exists, removing it") |
| 57 | + except plumbum.ProcessExecutionError: |
| 58 | + LOGGER.info(f"Manifest {tag} doesn't exist") |
| 59 | + |
| 60 | + LOGGER.info(f"Creating manifest for tag: {tag}") |
| 61 | + docker["manifest", "create", tag][all_platform_tags] & plumbum.FG |
| 62 | + LOGGER.info(f"Successfully created manifest for tag: {tag}") |
| 63 | + |
| 64 | + if push_to_registry: |
| 65 | + LOGGER.info(f"Pushing manifest for tag: {tag}") |
60 | 66 | docker["manifest", "push", tag] & plumbum.FG
|
61 |
| - |
62 | 67 | LOGGER.info(f"Successfully merged and pushed tag: {tag}")
|
63 |
| - |
64 |
| - LOGGER.info(f"All tags merged for image: {config.image}") |
| 68 | + else: |
| 69 | + LOGGER.info(f"Skipping push for tag: {tag}") |
65 | 70 |
|
66 | 71 |
|
67 | 72 | if __name__ == "__main__":
|
68 | 73 | logging.basicConfig(level=logging.INFO)
|
69 | 74 |
|
70 | 75 | config = common_arguments_parser(image=True, variant=True, tags_dir=True)
|
71 |
| - merge_tags(config) |
| 76 | + push_to_registry = os.environ.get("PUSH_TO_REGISTRY", "false").lower() == "true" |
| 77 | + |
| 78 | + LOGGER.info(f"Merging tags for image: {config.image}") |
| 79 | + all_tags = read_tags_from_files(config) |
| 80 | + for tag in all_tags: |
| 81 | + merge_tags(tag, push_to_registry) |
| 82 | + LOGGER.info(f"Successfully merged tags for image: {config.image}") |
0 commit comments