ci: replace shell scripts with python util
Some checks failed
/ pre-run (push) Successful in 27s
/ post-run (push) Has been skipped
/ build-images (ubuntu-2204) (push) Failing after 45s

This commit is contained in:
Marc 2024-07-28 23:17:03 -04:00
parent 8315904427
commit 61626e2fd7
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 98 additions and 34 deletions

View file

@ -16,6 +16,7 @@ jobs:
status: "Started"
init: true
build-images:
needs: [pre-run]
runs-on: ubuntu-latest
strategy:
matrix:
@ -27,10 +28,10 @@ jobs:
- name: Set image metadata
id: image-metadata
run: |
echo "image-tag=$(./script/get-tag.sh)" >> $GITHUB_OUTPUT
echo "full-image-name=${{ matrix.image-name }}:$(./script/get-tag.sh)" >> $GITHUB_OUTPUT
echo "image-tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "full-image-name=${{ matrix.image-name }}:$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Build image
run: ./script/build-image.sh ${{ matrix.image-name }} ${{ steps.image-metadata.outputs.image-tag }}
run: python ./build_image.py ${{ steps.image-metadata.full-image-name }} ./images/${{ matrix.image-name }}/Dockerfile
- name: Tag image as latest
run: podman tag ${{ steps.image-metadata.outputs.full-image-name }} ${{ matrix.image-name }}:latest
- name: List images

94
build_image.py Normal file
View file

@ -0,0 +1,94 @@
"""
Builds a container image with the provided image name and tag.
Usage:
python build_image.py <image-name> <tag> [<image-path>]
"""
import subprocess
import pathlib
import logging
import typing
import sys
import os
import re
logger = logging.getLogger("build-image")
def get_tag(is_ci: bool) -> str:
"""
Gets an image tag composed of the short sha of the current commit
and, depending on the is_ci flag, a "-dev" suffix.
"""
result = subprocess.run(
"git rev-parse --short HEAD", shell=True, capture_output=True, check=True
)
sha = re.sub(r"\n", "", str(result.stdout.decode('utf-8')))
if not is_ci:
return f"{sha}-dev"
return sha
def build_image(image_name: str, tag: str, image_path: typing.Optional[pathlib.Path]):
"""
Calls Podman to build the container image defined at image_path, which defaults to
the current directory.
The built image is named and tagged using image_name and tag.
"""
if image_path is None:
image_path = pathlib.Path("./Dockerfile")
cwd = image_path.parent
image_path = image_path.relative_to(cwd)
subprocess.run(
f"podman build -t {image_name}:{tag} -f {str(image_path)}",
shell=True,
check=True,
cwd=cwd,
)
def run(args: list[str]):
"""
CLI entrypoint.
"""
if len(args) < 1:
raise ValueError(
"There should be at least one argument. "
"Correct usage: python build_image.py <image-name:tag> [image-path]"
)
if len(args) > 2:
raise ValueError(
"Unrecognized arguments. "
"Correct usage: python build_image.py <image-name:tag> [image-path]"
)
tagged_image_name = args[0]
image_name_parts = tagged_image_name.split(":")
name = image_name_parts[0]
tag = (
image_name_parts[1]
if len(image_name_parts) == 2
else get_tag(bool(os.environ.get("CI", False)))
)
image_path = args[1] if len(args) == 2 else None
build_image(name, tag, pathlib.Path(image_path))
if __name__ == "__main__":
try:
run(sys.argv[1:])
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(e)

View file

@ -1,19 +0,0 @@
#!/bin/bash
image_name=$1
image_tag=$2
if [[ -z $image_name ]]; then
echo "An image name must be provided."
exit 1
fi
if [[ -z $image_tag ]]; then
echo "An image tag must be provided."
exit 1
fi
(
cd "images/$image_name"
podman build -t "$image_name:$image_tag" -f ./Dockerfile
) || exit 1

View file

@ -1,12 +0,0 @@
#!/bin/bash
has_changes=$(git status --short)
head_sha=$(git rev-parse --short HEAD)
tag=$head_sha
if [[ -n $has_changes ]]; then
tag="$tag-dev"
fi
echo "$tag"