feat(action): add basic first action version

This commit is contained in:
Bertrand Lanson 2024-02-28 18:22:13 +01:00
parent 6c4dbcc4ee
commit 084cabf1d6
5 changed files with 131 additions and 7 deletions

9
Dockerfile Normal file
View File

@ -0,0 +1,9 @@
FROM gcr.io/distroless/python3-debian10
COPY action /action
WORKDIR /action
ENV PYTHONPATH /action
CMD ["/action/matrix_generator.py"]

21
LICENSE
View File

@ -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.

View File

@ -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. 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"]'
```

18
action.yml Normal file
View File

@ -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 }}

View File

@ -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()