From bd69655745e90b7edc88ead2b0ebd7e370904964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Fri, 16 Jul 2021 14:42:19 +0200 Subject: [PATCH] Publish the tag latest --- README.md | 4 ++++ c2cciutils/configuration.py | 1 + c2cciutils/publish.py | 45 ++++++++++++++++++++++++++--------- c2cciutils/schema.json | 5 ++++ c2cciutils/scripts/publish.py | 13 +++++++++- 5 files changed, 56 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5bfed87ad..a03de5381 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ versions: The config is like this: ```yaml +latest: True images: - name: # The base name of the image we want to publish repository: @@ -143,4 +144,7 @@ repository: # and publish them with `c2cciutils-publish --group=` ``` +By default the last line of the `SECURITY.md` file will be published (`docker`) with the tag +`latest`. Set `latest` to `False` to disable it. + With the `c2cciutils-clean` the images on Docker hub for `feature_branch` will be removed on branch removing. diff --git a/c2cciutils/configuration.py b/c2cciutils/configuration.py index c56976d3a..cd58ef271 100644 --- a/c2cciutils/configuration.py +++ b/c2cciutils/configuration.py @@ -352,6 +352,7 @@ class Publish(TypedDict, total=False): # # The configuration used to publish on Docker class PublishDockerConfig(TypedDict, total=False): + latest: bool images: List["PublishDockerImage"] repository: Dict[str, "PublishDockerRepository"] diff --git a/c2cciutils/publish.py b/c2cciutils/publish.py index 1ed172775..c35bde698 100644 --- a/c2cciutils/publish.py +++ b/c2cciutils/publish.py @@ -17,7 +17,6 @@ from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build -import c2cciutils import c2cciutils.configuration @@ -208,7 +207,7 @@ def main_calendar() -> None: Run the calendar main function. """ parser = argparse.ArgumentParser( - description="Interact with google API for the docker publishing calendar" + description="Interact with google API for the Docker publishing calendar" ) parser.add_argument( "--refresh-gopass-credentials", @@ -292,9 +291,10 @@ def docker( image_config: c2cciutils.configuration.PublishDockerImage, tag_src: str, tag_dst: str, + latest: bool, ) -> bool: """ - Publish to a docker registry. + Publish to a Docker registry. config is like: server: # The server fqdn @@ -308,6 +308,7 @@ def docker( image_config: The image config tag_src: The source tag (usually latest) tag_dst: The tag used for publication + latest: Publish also the tag latest """ print("::group::Publishing {}:{} to {}".format(image_config["name"], tag_dst, name)) @@ -316,38 +317,60 @@ def docker( try: if "server" in config: - subprocess.check_call( + subprocess.run( [ "docker", "tag", "{}:{}".format(image_config["name"], tag_src), "{}/{}:{}".format(config["server"], image_config["name"], tag_dst), - ] + ], + check=True, ) - subprocess.check_call( + subprocess.run( [ "docker", "push", "{}/{}:{}".format(config["server"], image_config["name"], tag_dst), - ] + ], + check=True, ) + if latest: + subprocess.run( + [ + "docker", + "push", + "{}/{}:{}".format(config["server"], image_config["name"], tag_src), + ], + check=True, + ) else: if tag_src != tag_dst: - subprocess.check_call( + subprocess.run( [ "docker", "tag", "{}:{}".format(image_config["name"], tag_src), "{}:{}".format(image_config["name"], tag_dst), - ] + ], + check=True, ) - subprocess.check_call( + subprocess.run( [ "docker", "push", "{}:{}".format(image_config["name"], tag_dst), - ] + ], + check=True, ) + if latest: + subprocess.run( + [ + "docker", + "push", + "{}:{}".format(image_config["name"], tag_src), + ], + check=True, + ) print("::endgroup::") except subprocess.CalledProcessError as exception: print(f"Error: {exception}") diff --git a/c2cciutils/schema.json b/c2cciutils/schema.json index 8f47ee383..696e0f06a 100644 --- a/c2cciutils/schema.json +++ b/c2cciutils/schema.json @@ -458,6 +458,11 @@ "description": "The configuration used to publish on Docker", "type": "object", "properties": { + "latest": { + "description": "Publish the latest version on tag latest", + "default": true, + "type": "boolean" + }, "images": { "description": "List of images to be published", "type": "array", diff --git a/c2cciutils/scripts/publish.py b/c2cciutils/scripts/publish.py index 203a538e3..e36cf95e4 100644 --- a/c2cciutils/scripts/publish.py +++ b/c2cciutils/scripts/publish.py @@ -12,6 +12,7 @@ import c2cciutils.configuration import c2cciutils.publish +import c2cciutils.security from c2cciutils.publish import GoogleCalendar @@ -169,6 +170,14 @@ def main() -> None: if config.get("publish", {}).get("google_calendar", False) else {}, ) + + latest = False + if os.path.exists("SECURITY.md") and docker_config["latest"] is True: + with open("SECURITY.md") as security_file: + security = c2cciutils.security.Security(security_file.read()) + version_index = security.headers.index("Version") + latest = security.data[-1][version_index] == version + for image_conf in docker_config.get("images", []): if image_conf.get("group", "") == args.group: for tag_config in image_conf.get("tags", []): @@ -183,7 +192,9 @@ def main() -> None: ) ) else: - success &= c2cciutils.publish.docker(conf, name, image_conf, tag_src, tag_dst) + success &= c2cciutils.publish.docker( + conf, name, image_conf, tag_src, tag_dst, latest + ) if version_type in google_calendar_config.get("on", []): if not google_calendar: google_calendar = GoogleCalendar()