From 5c177cf0d2a054f489f83c6f6827872b4f391c63 Mon Sep 17 00:00:00 2001 From: Mystic <215104920@qq.com> Date: Sun, 18 Aug 2024 00:08:49 +0800 Subject: [PATCH] feat(py): update README and refactor Dockerfile and compose --- .../.dockerignore | 5 ++- .../{{cookiecutter.project_slug}}/Dockerfile | 16 ++++--- .../py/{{cookiecutter.project_slug}}/Makefile | 2 + .../{{cookiecutter.project_slug}}/README.md | 44 +++++++++++++++++-- .../{{cookiecutter.project_slug}}/compose.yml | 1 + 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/template/py/{{cookiecutter.project_slug}}/.dockerignore b/template/py/{{cookiecutter.project_slug}}/.dockerignore index 55d189e..84d2c6b 100644 --- a/template/py/{{cookiecutter.project_slug}}/.dockerignore +++ b/template/py/{{cookiecutter.project_slug}}/.dockerignore @@ -2,8 +2,9 @@ .* CHANGELOG.md CODE_OF_CONDUCT.md -CONTRIBUTING.md compose.yml +CONTRIBUTING.md +Dockerfile LICENSE* -Makefile README.md +SECURITY.md diff --git a/template/py/{{cookiecutter.project_slug}}/Dockerfile b/template/py/{{cookiecutter.project_slug}}/Dockerfile index 759ce7a..c21e183 100644 --- a/template/py/{{cookiecutter.project_slug}}/Dockerfile +++ b/template/py/{{cookiecutter.project_slug}}/Dockerfile @@ -18,6 +18,15 @@ RUN uv venv venv && \ . venv/bin/activate && \ uv pip install .[dev] +# Separate stage for validation (build and test) +FROM builder AS validator + +WORKDIR /app +COPY . . + +# Run build and test as part of the validation +RUN hatch build && hatch test + # Final image for running the application FROM python:{{cookiecutter.python_version}}-slim-bullseye @@ -32,11 +41,8 @@ WORKDIR /app # Copy the virtual environment and application code COPY --from=builder /app/venv /app/venv COPY --from=builder /root/.local /root/.local -COPY . . - -# Ensure the application builds and tests are executed correctly -RUN hatch build && hatch test +COPY src ./src HEALTHCHECK --start-period=30s CMD python -c "import requests; requests.get('http://localhost:8000', timeout=2)" -CMD ["python", "src/app.py"] +CMD ["python", "src/{{cookiecutter.package_name}}/app.py"] diff --git a/template/py/{{cookiecutter.project_slug}}/Makefile b/template/py/{{cookiecutter.project_slug}}/Makefile index 577edd1..40d5c2e 100644 --- a/template/py/{{cookiecutter.project_slug}}/Makefile +++ b/template/py/{{cookiecutter.project_slug}}/Makefile @@ -39,6 +39,8 @@ compose-down: # Clean build artifacts clean: @rm -rf build dist *.egg-info htmlcov .coverage coverage.xml coverage.lcov + @docker compose -f ./compose.yml down -v + @docker image rm -f $(APP_NAME) # Show help help: diff --git a/template/py/{{cookiecutter.project_slug}}/README.md b/template/py/{{cookiecutter.project_slug}}/README.md index 840b48b..55a09a8 100644 --- a/template/py/{{cookiecutter.project_slug}}/README.md +++ b/template/py/{{cookiecutter.project_slug}}/README.md @@ -47,14 +47,50 @@ For more detailed usage instructions, please refer to our [Usage Guide](docs/usa To set up the development environment: 1. Ensure `uv`, `ruff`, and `hatch` are installed. + + - Using `pipx` + ```bash + # you can use `pipx` + pipx install uv ruff hatch + + # or + # Firstly, install `uv` (macOS and Linux) + curl -LsSf https://astral.sh/uv/install.sh | sh + # (Windows) + powershell -c "irm https://astral.sh/uv/install.ps1 | iex" + + # Then + uv tool install ruff + uv tool install hatch + ``` + - Using `uv` + ```bash + # macOS and Linux + curl -LsSf https://astral.sh/uv/install.sh | sh + # Windows + powershell -c "irm https://astral.sh/uv/install.ps1 | iex" + + # Then + uv tool install ruff + uv tool install hatch + ``` + - BTW + > `pipx` and `uv tool install` will usually install the binaries in your `~/.local/bin` directory. + > + > Please ensure the `~/.local/bin` directory is in your `PATH` environment variable. + 2. Clone the repository and navigate to the project directory. 3. Run the following commands: ```bash -make init # Create a virtual env using uv -make install-dev # Install development dependencies -make build # Build wheel package -make test # Run tests +make init # Create a virtual env using uv +make install-dev # Install development dependencies +make build # Build wheel package +make test # Run tests +make image # Build Docker image +make compose-up # Run Docker Compose +make compose-down # Stop and remove Docker Compose +make clean # Remove build artifacts ``` For more details, see our [Contributing Guide](CONTRIBUTING.md). diff --git a/template/py/{{cookiecutter.project_slug}}/compose.yml b/template/py/{{cookiecutter.project_slug}}/compose.yml index b7d6a8b..78b1486 100644 --- a/template/py/{{cookiecutter.project_slug}}/compose.yml +++ b/template/py/{{cookiecutter.project_slug}}/compose.yml @@ -1,6 +1,7 @@ services: {{cookiecutter.project_slug}}: build: . + image: {{cookiecutter.project_slug}} ports: - 8000:8000