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

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

View file

@ -16,21 +16,25 @@ jobs:
status: "Started"
init: true
build-images:
needs: [pre-run]
runs-on: ubuntu-latest
strategy:
matrix:
image-name: ['ubuntu-2204']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Login to Registry
run: podman login -u ${{ secrets.REGISTRY_USER }} -p ${{ secrets.REGISTRY_TOKEN }} ${{ env.REGISTRY_ENDPOINT }}
- 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"