ci: replace shell scripts with python util
This commit is contained in:
parent
8315904427
commit
35080fd3b0
4 changed files with 102 additions and 34 deletions
|
@ -16,21 +16,25 @@ jobs:
|
||||||
status: "Started"
|
status: "Started"
|
||||||
init: true
|
init: true
|
||||||
build-images:
|
build-images:
|
||||||
|
needs: [pre-run]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
image-name: ['ubuntu-2204']
|
image-name: ['ubuntu-2204']
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
- uses: actions/setup-python@v5
|
||||||
|
with:
|
||||||
|
python-version: 3.12
|
||||||
- name: Login to Registry
|
- name: Login to Registry
|
||||||
run: podman login -u ${{ secrets.REGISTRY_USER }} -p ${{ secrets.REGISTRY_TOKEN }} ${{ env.REGISTRY_ENDPOINT }}
|
run: podman login -u ${{ secrets.REGISTRY_USER }} -p ${{ secrets.REGISTRY_TOKEN }} ${{ env.REGISTRY_ENDPOINT }}
|
||||||
- name: Set image metadata
|
- name: Set image metadata
|
||||||
id: image-metadata
|
id: image-metadata
|
||||||
run: |
|
run: |
|
||||||
echo "image-tag=$(./script/get-tag.sh)" >> $GITHUB_OUTPUT
|
echo "image-tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||||
echo "full-image-name=${{ matrix.image-name }}:$(./script/get-tag.sh)" >> $GITHUB_OUTPUT
|
echo "full-image-name=${{ matrix.image-name }}:$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||||
- name: Build image
|
- 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.outputs.full-image-name }} ./images/${{ matrix.image-name }}/Dockerfile
|
||||||
- name: Tag image as latest
|
- name: Tag image as latest
|
||||||
run: podman tag ${{ steps.image-metadata.outputs.full-image-name }} ${{ matrix.image-name }}:latest
|
run: podman tag ${{ steps.image-metadata.outputs.full-image-name }} ${{ matrix.image-name }}:latest
|
||||||
- name: List images
|
- name: List images
|
||||||
|
|
95
build_image.py
Normal file
95
build_image.py
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
"""
|
||||||
|
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)
|
||||||
|
exit(1)
|
|
@ -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
|
|
|
@ -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"
|
|
Loading…
Reference in a new issue