From 7f15cf10adc5693ff9b4a2c5cb502b3500fa2a15 Mon Sep 17 00:00:00 2001 From: Noah Date: Sun, 22 Feb 2026 10:32:46 +0100 Subject: [PATCH 1/4] Add a New Endpoint, pre commit hooks and part of the ci --- .github/workflows/ci-cd.yml | 24 +++++++++++++++++++++++ .pre-commit-config.yaml | 38 +++++++++++++++++++++++++++++++++++++ app/main.py | 8 +++++++- tests/test_main.py | 16 ++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index a98b15e..581ca75 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -9,6 +9,18 @@ on: tags-ignore: - '**' +x-setup-python: &setup-python + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install ruff pytest pytest-cov + jobs: lint: name: Lint Code @@ -19,6 +31,10 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - *setup-python + + - name: Ruff Lint + run: ruff check app/ tests/ test: name: Run Tests runs-on: ubuntu-latest @@ -28,6 +44,11 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - *setup-python + + - name: Execute tests + run: pytest --cov=app --cov-report=html --cov-fail-under=80 + build: name: Build Docker Image runs-on: ubuntu-latest @@ -40,3 +61,6 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + + - name: Docker build + run: docker build -t fastapi-gitops-starter . diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d337d10..c5770d4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,43 @@ +# Used https://pre-commit.com/hooks.html repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 hooks: + # To prevent committing large files + - id: check-added-large-files + + # To check YAML files for syntax errors (excluding Helm charts) + - id: check-yaml + exclude: ^helm/ + + # To check code style - id: trailing-whitespace + - id: end-of-file-fixer + + # To check for security issues + - id: detect-private-key + + # To sort imports in Python files + - repo: https://github.com/pycqa/isort + rev: 8.0.0 + hooks: + - id: isort + + # To check for security issues + - repo: https://github.com/PyCQA/bandit + rev: 1.9.3 + hooks: + - id: bandit + exclude: ^tests/ + + # To check code style + - repo: https://github.com/psf/black + rev: 26.1.0 + hooks: + - id: black + + # To make sure we do not commit secrets + - repo: https://github.com/Yelp/detect-secrets + rev: v1.5.0 + hooks: + - id: detect-secrets diff --git a/app/main.py b/app/main.py index ae8adc6..ea377e1 100644 --- a/app/main.py +++ b/app/main.py @@ -49,5 +49,11 @@ async def get_item(item_id: int): } +@app.post("/api/items") +async def create_item(name: str, description: str): + """Create a new item.""" + return {"id": 999, "name": name, "description": description, "created": True} + + if __name__ == "__main__": - uvicorn.run(app, host="0.0.0.0", port=8000) + uvicorn.run(app, host="127.0.0.1", port=8000) diff --git a/tests/test_main.py b/tests/test_main.py index db89e2f..c175bdc 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -41,3 +41,19 @@ def test_get_item(): assert data["id"] == 5 assert data["name"] == "Item 5" assert "item number 5" in data["description"] + + +def test_create_item(): + """Test the create item endpoint.""" + name = "Basketball" + description = "A round object" + + response = client.post( + "/api/items", params={"name": name, "description": description} + ) + assert response.status_code == 200 + data = response.json() + assert data["id"] == 999 + assert data["name"] == name + assert data["description"] == description + assert data["created"] == True From 665cbcadb8d33a74a53b40dbc4aa3939bc016cd8 Mon Sep 17 00:00:00 2001 From: Noah Date: Sun, 22 Feb 2026 13:58:05 +0100 Subject: [PATCH 2/4] try to fix CI/CD --- .github/workflows/ci-cd.yml | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 581ca75..ae143ba 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -9,18 +9,6 @@ on: tags-ignore: - '**' -x-setup-python: &setup-python - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: "3.11" - - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - pip install ruff pytest pytest-cov - jobs: lint: name: Lint Code @@ -31,7 +19,17 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - *setup-python + - &install-python + name: Set up Python + uses: actions/setup-python@v6 + with: + python-version: '3.13' + + - &install-packages + name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt - name: Ruff Lint run: ruff check app/ tests/ @@ -44,7 +42,8 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - *setup-python + - *install-python + - *install-packages - name: Execute tests run: pytest --cov=app --cov-report=html --cov-fail-under=80 From e2f019d4b758246cde898abb2d9e3014980ba681 Mon Sep 17 00:00:00 2001 From: Noah Date: Sun, 22 Feb 2026 14:23:22 +0100 Subject: [PATCH 3/4] fix lint issue --- tests/test_main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_main.py b/tests/test_main.py index c175bdc..f90c483 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -56,4 +56,4 @@ def test_create_item(): assert data["id"] == 999 assert data["name"] == name assert data["description"] == description - assert data["created"] == True + assert data["created"] From 70fcec7734279a551fe2da22f1bb90227d0b9b00 Mon Sep 17 00:00:00 2001 From: Noah Date: Sun, 22 Feb 2026 14:38:25 +0100 Subject: [PATCH 4/4] add docker build --- .github/workflows/ci-cd.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index ae143ba..4bd8d1a 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -1,5 +1,9 @@ name: CI/CD Pipeline +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + on: release: types: [ published ] @@ -62,4 +66,31 @@ jobs: uses: actions/checkout@v4 - name: Docker build + if: github.event_name == 'push' run: docker build -t fastapi-gitops-starter . + + # Documentation: https://docs.github.com/en/actions/tutorials/publish-packages/publish-docker-images#publishing-images-to-github-packages + - name: Log in to the Container registry + if: github.event_name == 'release' + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + if: github.event_name == 'release' + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + if: github.event_name == 'release' + id: push + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }}