Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install Ruff
run: pip install ruff

- name: Lint with Ruff
run: ruff check app/ tests/

test:
name: Run Tests
runs-on: ubuntu-latest
Expand All @@ -28,6 +39,17 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install dependencies
run: pip install -r requirements.txt

- name: Run tests with coverage
run: pytest --cov=app --cov-report=term-missing --cov-fail-under=80

build:
name: Build Docker Image
runs-on: ubuntu-latest
Expand All @@ -40,3 +62,31 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract image metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
# tag with version and 'latest' on release
type=semver,pattern={{version}}
type=semver,pattern=latest
# tag with short commit SHA on regular pushes
type=sha

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
# only push on release, just build on regular pushes
push: ${{ github.event_name == 'release' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion .github/workflows/markdown2pdf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

- name: Replace links
run: |
cp README.md README_WITH_LINKS.md
cp README.md README_WITH_LINKS.md
sed -i -e "s#\(^\!\[[^]]\+\](\)\(images/\)#\1$URL/\2#g" README_WITH_LINKS.md
for file in sources/*; do sed -i -e "s#($file)#($URL/$file)#g" README_WITH_LINKS.md ; done

Expand Down
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,30 @@ repos:
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
- id: check-yaml
args: ['--unsafe']
exclude: ^helm/

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort

# - repo: https://github.com/returntocorp/semgrep
# rev: v1.68.0
# hooks:
# - id: semgrep
# args: ['--config', 'p/python', '--error']

- repo: https://github.com/Yelp/detect-secrets
rev: v1.5.0
hooks:
- id: detect-secrets

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
Binary file added .secrets.baseline
Binary file not shown.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use Python 3.11 slim image as base
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY app ./app/

# Expose port 8000
EXPOSE 8000

# Run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

# Copy application code
COPY ../app ./app/

# Expose port 8000
EXPOSE 8000

# Run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GitOps with FastAPI

***University of Amsterdam***

# 1. Introduction
# 1. Introduction

In this tutorial, we use GitOps practices with FastAPI, including CI/CD pipelines, code quality tools, and automated testing.

Expand Down Expand Up @@ -35,7 +35,7 @@ In this tutorial, we use GitOps practices with FastAPI, including CI/CD pipeline



# 2. Tutorial
# 2. Tutorial

The steps of this tutorial are as follows:
- [Building REST APIs with FastAPI](#21-setting-up-the-project)
Expand All @@ -60,17 +60,17 @@ Prerequisites:
```

* Set Up the Python Environmentt:

```bash
# Create a virtual environment
python -m venv venv

# Activate the virtual environment
# On Linux/MacOS:
source venv/bin/activate
# On Windows:
venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt
```
Expand Down Expand Up @@ -110,7 +110,7 @@ Prerequisites:
```bash
# Check for issues
ruff check app/ tests/

# Fix auto-fixable issues
ruff check app/ tests/ --fix
```
Expand All @@ -120,7 +120,7 @@ Prerequisites:
```bash
# Check formatting
black --check app/ tests/

# Format code
black app/ tests/
```
Expand All @@ -134,19 +134,19 @@ Pre-commit hooks automatically run checks before each commit to ensure consisten
```bash
# Install pre-commit
pip install pre-commit

# Install the git hooks
pre-commit install
```

* Using Pre-commit:

Pre-commit will now run automatically on `git commit`. You can also run it manually:

```bash
# Run on all files
pre-commit run --all-files

# Run on staged files
pre-commit run
```
Expand Down Expand Up @@ -200,14 +200,14 @@ This repository includes a Helm chart for deploying the application to Kubernete
- Kubernetes 1.19+
- Helm 3.0+

* Install the Helm Chart:
* Install the Helm Chart:

```bash
helm install my-release ./helm/fastapi-gitops-starter
```

* Uninstall the Helm Chart:
* Uninstall the Helm Chart:

```bash
helm uninstall my-release
```
Expand Down Expand Up @@ -239,7 +239,7 @@ including host and paths.
* To make sure we do not commit secrets
* To check code style


## 3.2 Add a New Endpoint

1. Open `app/main.py`
Expand Down
11 changes: 11 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,16 @@ 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)
13 changes: 13 additions & 0 deletions custom-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
autoscaling:
enabled: true
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 10

ingress:
enabled: true
hosts:
- host: minikube.test
paths:
- path: /
pathType: ImplementationSpecific
11 changes: 11 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ 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."""
response = client.post("/api/items?name=Test Item&description=Test Description")
assert response.status_code == 200
data = response.json()
assert data["id"] == 999
assert data["name"] == "Test Item"
assert data["description"] == "Test Description"
assert data["created"] is True