Skip to content

Commit 19bfa74

Browse files
committed
Improve merge_tags: runnable multiple times, run in PRs, simpler function
1 parent 5cfcdbd commit 19bfa74

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

.github/workflows/docker-merge-tags.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ jobs:
6666
password: ${{ secrets.REGISTRY_TOKEN }}
6767

6868
- name: Merge tags for the images 🔀
69-
if: env.PUSH_TO_REGISTRY == 'true'
7069
run: |
7170
python3 -m tagging.apps.merge_tags \
7271
--image ${{ inputs.image }} \

tagging/apps/merge_tags.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) Jupyter Development Team.
33
# Distributed under the terms of the Modified BSD License.
44
import logging
5+
import os
56

67
import plumbum
78

@@ -36,36 +37,46 @@ def read_tags_from_files(config: Config) -> set[str]:
3637
return tags
3738

3839

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}")
6066
docker["manifest", "push", tag] & plumbum.FG
61-
6267
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}")
6570

6671

6772
if __name__ == "__main__":
6873
logging.basicConfig(level=logging.INFO)
6974

7075
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

Comments
 (0)