From a8b521129b03306c19ae51aa700490e546f0101a Mon Sep 17 00:00:00 2001 From: Max Pfeiffer Date: Mon, 18 Dec 2023 19:55:11 +0100 Subject: [PATCH] Made python version and OS variant configurable --- .github/workflows/pipeline.yml | 30 ++++++++++++++++++++++++ cookiecutter.json | 2 ++ poetry.lock | 13 +++++++++- pyproject.toml | 1 + tests/test_custom_config.py | 8 +++++++ tests/test_default_config.py | 6 +++++ {{cookiecutter.project_slug}}/Dockerfile | 2 +- 7 files changed, 60 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 25ae30b..740b40f 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -3,7 +3,37 @@ name: Pipeline on: push jobs: + code-quality: + runs-on: ubuntu-20.04 + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: 3.11 + - name: Install Poetry + uses: snok/install-poetry@v1 + with: + version: 1.7.1 + virtualenvs-in-project: true + - name: Load cached venv + id: cached-poetry-dependencies + uses: actions/cache@v3 + with: + path: .venv + key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} + - name: Install dependencies + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' + run: | + poetry install --no-interaction --no-root + - name: Run pylint and black + run: | + source .venv/bin/activate + black --check . + run-tests: + needs: code-quality runs-on: ubuntu-20.04 steps: - name: Checkout Repository diff --git a/cookiecutter.json b/cookiecutter.json index 4cf435f..d523a93 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -5,5 +5,7 @@ "project_description": "", "author_name": "", "author_email": "", + "python_version": ["3.12.0", "3.10.13", "3.10.13"], + "operating_system_variant": ["slim-bookworm", "bookworm"], "_extensions": ["cookiecutter.extensions.SlugifyExtension"] } \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index db9f66d..7529688 100644 --- a/poetry.lock +++ b/poetry.lock @@ -374,6 +374,17 @@ urllib3 = ">=1.26.0" ssh = ["paramiko (>=2.4.3)"] websockets = ["websocket-client (>=1.3.0)"] +[[package]] +name = "dockerfile-parse" +version = "2.0.1" +description = "Python library for Dockerfile manipulation" +optional = false +python-versions = ">=3.6" +files = [ + {file = "dockerfile-parse-2.0.1.tar.gz", hash = "sha256:3184ccdc513221983e503ac00e1aa504a2aa8f84e5de673c46b0b6eee99ec7bc"}, + {file = "dockerfile_parse-2.0.1-py2.py3-none-any.whl", hash = "sha256:bdffd126d2eb26acf1066acb54cb2e336682e1d72b974a40894fac76a4df17f6"}, +] + [[package]] name = "filelock" version = "3.13.1" @@ -1058,4 +1069,4 @@ test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess [metadata] lock-version = "2.0" python-versions = "3.11.*" -content-hash = "f2156e59035a65868056f381dd50c1e0dea69eaa050e45e76753e1da4a011315" +content-hash = "07c4cba4f84c983c51a94839cf6535617db83980637a5834c5ebca1be7189337" diff --git a/pyproject.toml b/pyproject.toml index efeb792..5ceb0d6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ pre-commit = "3.6.0" pytest-cookies = "0.7.0" pytest-cov = "4.1.0" toml = "0.10.2" +dockerfile-parse = "2.0.1" # https://docs.pytest.org/en/latest/reference/customize.html [tool.pytest.ini_options] diff --git a/tests/test_custom_config.py b/tests/test_custom_config.py index 352dfe3..00943fd 100644 --- a/tests/test_custom_config.py +++ b/tests/test_custom_config.py @@ -2,6 +2,7 @@ import toml from pytest_cookies.plugin import Result +from dockerfile_parse import DockerfileParser def test_custom_config(cookies) -> None: @@ -17,6 +18,8 @@ def test_custom_config(cookies) -> None: "project_description": "A project description which does make a lot of sense.", "author_name": "Jane Doe", "author_email": "jane.doe@unknown.com", + "python_version": "3.10.13", + "operating_system_variant": "bookworm", } ) @@ -35,3 +38,8 @@ def test_custom_config(cookies) -> None: assert toml_data["tool"]["poetry"]["authors"] == [ "Jane Doe " ] + + dockerfile: Path = result.project_path / "Dockerfile" + dfp = DockerfileParser(path=str(dockerfile)) + + assert "3.10.13-bookworm" in dfp.baseimage diff --git a/tests/test_default_config.py b/tests/test_default_config.py index d72620e..7200a8c 100644 --- a/tests/test_default_config.py +++ b/tests/test_default_config.py @@ -2,6 +2,7 @@ import toml from pytest_cookies.plugin import Result +from dockerfile_parse import DockerfileParser def test_default_config(cookies) -> None: @@ -22,3 +23,8 @@ def test_default_config(cookies) -> None: assert toml_data["tool"]["poetry"]["version"] == "0.0.0" assert toml_data["tool"]["poetry"]["description"] == "" assert toml_data["tool"]["poetry"]["authors"] == [] + + dockerfile: Path = result.project_path / "Dockerfile" + dfp = DockerfileParser(path=str(dockerfile)) + + assert "3.12.0-slim-bookworm" in dfp.baseimage diff --git a/{{cookiecutter.project_slug}}/Dockerfile b/{{cookiecutter.project_slug}}/Dockerfile index a4d0371..7986a83 100644 --- a/{{cookiecutter.project_slug}}/Dockerfile +++ b/{{cookiecutter.project_slug}}/Dockerfile @@ -1,6 +1,6 @@ # Be aware that you need to specify these arguments before the first FROM # see: https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact -FROM pfeiffermax/uvicorn-poetry:3.2.0-python3.11.6-slim-bookworm +FROM pfeiffermax/uvicorn-poetry:3.2.0-python{{ cookiecutter.python_version }}-{{ cookiecutter.operating_system_variant }} # install [tool.poetry.dependencies] # this will install virtual environment into /.venv because of POETRY_VIRTUALENVS_IN_PROJECT=true