From 084cabf1d6d677f5777e130e0ad9e1a0bb7ccd48 Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Wed, 28 Feb 2024 18:22:13 +0100 Subject: [PATCH] feat(action): add basic first action version --- Dockerfile | 9 ++++++ LICENSE | 21 ++++++++++--- README.md | 28 +++++++++++++++-- action.yml | 18 +++++++++++ action/matrix_generator.py | 62 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 Dockerfile create mode 100644 action.yml create mode 100644 action/matrix_generator.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3d1969e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM gcr.io/distroless/python3-debian10 + +COPY action /action + +WORKDIR /action + +ENV PYTHONPATH /action + +CMD ["/action/matrix_generator.py"] diff --git a/LICENSE b/LICENSE index 2116897..c9a37e5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,9 +1,20 @@ -MIT License +The MIT License (MIT) -Copyright (c) 2024 github-actions +Copyright (c) 2017 Bertrand Lanson -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index de074d2..9914b86 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,27 @@ -# docker-matrix-generator +# Action: generate docker build matrix +> This repository is only a mirror. Development and testing is done on a private gitea server. -GitHub Actions to generate a build matrix that's easy to work with. \ No newline at end of file +This action generates a docker build matrix from a json-formatted list of versions + +## Parameters + +The following parameters can be used as `step.with` keys: + +| Name | Type | Default | Required |Description | +| ------------------ | ------ | ------- |--------- |--------------------------------- | +| `versions ` | String | | yes | Json Formatted List as a String | + + +## Example usage + +```yaml +jobs: + publish: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Generate docker build matrix + uses: ednz-cloud/docker-matrix-generator@v1 + with: + versions: '["2.2.8","2.2.7","2.2.4","2.2.3","2.1.5","2.1.4","2.1.3","2.1.2","2.1.1","2.1.0","2.0.20","2.0.19","2.0.18","2.0.17","2.0.16","2.0.15","2.0.14","2.0.13"]' +``` \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..cbd9d29 --- /dev/null +++ b/action.yml @@ -0,0 +1,18 @@ +--- +name: Docker build matrix generator +description: Return a rich matrix from a list of version to ease the tagging of docker images + +inputs: + versions: + description: A list of strings containing the versions to build a matrix for + required: true + +outputs: + version_matrix: + description: The matrix generated from the version list + +runs: + using: docker + image: Dockerfile + env: + VERSIONS: ${{ inputs.versions }} diff --git a/action/matrix_generator.py b/action/matrix_generator.py new file mode 100644 index 0000000..7a648ca --- /dev/null +++ b/action/matrix_generator.py @@ -0,0 +1,62 @@ +import os +import json +import re +from collections import defaultdict +from typing import List, Tuple, Dict + +OUTPUT = "version_matrix" + + +def parse_version(version_str: str) -> Tuple[int, int, int]: + match = re.match(r"(\d+)\.(\d+)\.(\d+)", version_str) + if match: + return tuple(map(int, match.groups())) + else: + raise ValueError("Invalid version format: " + version_str) + + +def get_latest_versions(versions: List[str]) -> List[Dict[str, any]]: + latest_major = defaultdict(lambda: (0, 0, 0)) + latest_minor = defaultdict(lambda: (0, 0, 0)) + + for version in versions: + major, minor, patch = parse_version(version) + if (major, minor, patch) > latest_major[major]: + latest_major[major] = (major, minor, patch) + if (major, minor, patch) > latest_minor[(major, minor)]: + latest_minor[(major, minor)] = (major, minor, patch) + + latest_overall = max(versions) + + version_objects = [] + for version in versions: + major, minor, patch = parse_version(version) + is_latest = version == latest_overall + is_latest_major = (major, minor, patch) == latest_major[major] + is_latest_minor = (major, minor, patch) == latest_minor[(major, minor)] + + version_obj = { + "version": version, + "is_latest": is_latest, + "is_latest_major": is_latest_major, + "is_latest_minor": is_latest_minor, + } + version_objects.append(version_obj) + + return version_objects + + +def main(): + all_versions_str = os.environ.get("VERSION_LIST", "") + all_versions = json.loads(all_versions_str) + + version_objects = get_latest_versions(all_versions) + + value = json.dumps(version_objects) + + with open(os.environ["GITHUB_OUTPUT"], "a") as fh: + print(f"{OUTPUT}='{value}'", file=fh) + + +if __name__ == "__main__": + main()