Skip to content

Commit

Permalink
Merge pull request #37 from max-pfeiffer/feature/use_github_workflow_…
Browse files Browse the repository at this point in the history
…matrix

Feature/use GitHub workflow matrix
  • Loading branch information
max-pfeiffer authored Sep 23, 2023
2 parents fdf5453 + aa5c06b commit b71fac3
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 272 deletions.
32 changes: 26 additions & 6 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.1
version: 1.6.1
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
Expand All @@ -36,6 +36,12 @@ jobs:
run-build-image-tests:
needs: code-quality
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
python_version: ["3.9.18", "3.10.13", "3.11.5"]
os_variant: ["bookworm", "slim-bookworm"]
poetry_version: ["1.4.2", "1.5.1", "1.6.1"]
steps:
- name: Checkout Repository
uses: actions/checkout@v3
Expand All @@ -48,7 +54,7 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.1
version: 1.6.1
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
Expand All @@ -60,7 +66,11 @@ jobs:
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install --no-interaction --no-root
- name: Run all tests with pytest
- name: Run tests for image builds with pytest
env:
PYTHON_VERSION: ${{ matrix.python_version }}
OS_VARIANT: ${{ matrix.os_variant }}
POETRY_VERSION: ${{ matrix.poetry_version }}
run: |
source .venv/bin/activate
pytest tests/build_image --cov
Expand All @@ -73,6 +83,12 @@ jobs:
run-publish-image-tests:
needs: code-quality
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
python_version: ["3.11.5"]
os_variant: ["slim-bookworm"]
poetry_version: ["1.6.1"]
steps:
- name: Checkout Repository
uses: actions/checkout@v3
Expand All @@ -85,7 +101,7 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.1
version: 1.6.1
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
Expand All @@ -97,7 +113,11 @@ jobs:
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install --no-interaction --no-root
- name: Run all tests with pytest
- name: Run tests for image publishing with pytest
env:
PYTHON_VERSION: ${{ matrix.python_version }}
OS_VARIANT: ${{ matrix.os_variant }}
POETRY_VERSION: ${{ matrix.poetry_version }}
run: |
source .venv/bin/activate
pytest tests/publish_image --cov
Expand Down Expand Up @@ -127,7 +147,7 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.1
version: 1.6.1
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
- id: detect-private-key
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.9.1
hooks:
- id: black
- repo: local
Expand Down
68 changes: 46 additions & 22 deletions build/publish.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from build.utils import get_docker_bake_file, get_context
from build.utils import get_context
from pathlib import Path
from python_on_whales import DockerClient, Builder

Expand All @@ -17,9 +17,29 @@
help="Docker Hub password",
)
@click.option(
"--version-tag", envvar="GIT_TAG_NAME", required=True, help="Version Tag"
"--version-tag", envvar="GIT_TAG_NAME", required=True, help="Version tag"
)
@click.option(
"--python-version",
envvar="PYTHON_VERSION",
required=True,
help="Python version",
)
@click.option(
"--os-variant",
envvar="OS_VARIANT",
required=True,
help="Operating system variant",
)
@click.option(
"--poetry-version",
envvar="POETRY_VERSION",
required=True,
help="Poetry version",
)
@click.option(
"--registry", envvar="REGISTRY", default="docker.io", help="Docker registry"
)
@click.option("--registry", envvar="REGISTRY", help="Docker registry")
@click.option(
"--use-local-cache-storage-backend",
envvar="USE_LOCAL_CACHE_STORAGE_BACKEND",
Expand All @@ -30,25 +50,23 @@ def main(
docker_hub_username: str,
docker_hub_password: str,
version_tag: str,
poetry_version: str,
python_version: str,
os_variant: str,
registry: str,
use_local_cache_storage_backend: bool,
) -> None:
context: Path = get_context()
bake_file: Path = get_docker_bake_file()
variables: dict = {
"CONTEXT": str(context),
"IMAGE_VERSION": version_tag,
}

bake_file_overrides: dict = {}
if use_local_cache_storage_backend:
bake_file_overrides = {
"*.cache-to": "type=local,mode=max,dest=/tmp",
"*.cache-from": "type=local,src=/tmp",
}
tag: str = f"{registry}/pfeiffermax/python-poetry:{version_tag}-poetry{poetry_version}-python{python_version}-{os_variant}"

if registry:
variables["REGISTRY"] = registry
platforms: list[str] = ["linux/amd64", "linux/arm64/v8"]
cache_to: str = "type=gha,mode=max"
cache_from: str = "type=gha"

if use_local_cache_storage_backend:
cache_to = "type=local,mode=max,dest=/tmp"
cache_from = "type=local,src=/tmp"

docker_client: DockerClient = DockerClient()
builder: Builder = docker_client.buildx.create(
Expand All @@ -60,15 +78,21 @@ def main(
username=docker_hub_username,
password=docker_hub_password,
)
build_config: dict = docker_client.buildx.bake(
targets=["python-poetry"],

docker_client.buildx.build(
context_path=context,
target="production-image",
build_args={
"POETRY_VERSION": poetry_version,
"OFFICIAL_PYTHON_IMAGE": f"python:{python_version}-{os_variant}",
},
tags=tag,
platforms=platforms,
builder=builder,
files=[bake_file],
variables=variables,
set=bake_file_overrides,
cache_to=cache_to,
cache_from=cache_from,
push=True,
)
print(build_config)

# Cleanup
docker_client.buildx.stop(builder)
Expand Down
Loading

0 comments on commit b71fac3

Please sign in to comment.