================
My project description
-
Install Poetry
-
Set config for venv in local
poetry config virtualenvs.in-project true poetry env use 3.11 poetry shell poetry install --without prod
-
(Postgres only) Create and run required databases
docker compose up -d
-
(Postgres only) Apply migrations from VSCode launcher
# For migrations on Cloud SQL instance, ensure creating unix socket & starting Cloud SQL Proxy first # sudo mkdir /var/cloudsql && sudo chmod 777 /var/cloudsql # cloud-sql-proxy -u /var/cloudsql sandbox-ahourlier:europe-west9:fastapi-template-instance
# WITHOUT DOCKER (Guess ADC from env)
uvicorn app.main:app --reload # Or from VSCode launcher
# WITH DOCKER
Run the service named 'app' in docker-compose.yml
poetry run pytest --cov=app --cov-report=term # Uses SQLALCHEMY_DATABASE_URI in pyproject.toml
fastapi_template
│
├── .cloudbuild - Cloud Build configuration
│ └── cloudbuild.yaml
│
├── .github
│ └── workflows - Github Actions
│
├── .vscode
│ ├── launch.json - Launch to execute the app
│ └── tasks.json - For dockerized launch
│
├── Dockerfile.prod - Used to build and deploy on Cloud Run
│
├── alembic.ini - Local Database configuration
│
├── app - Web stuffs
│ ├── api - Global deps & routing
│ └── core
│ ├── google_apis - Google API's utils
│ ├── cloud_logging.py - Logging wrapper
│ ├── config.py - Global app configuration
│ └── google_clients.py - Google API's client builders
│ │
│ ├── firestore - CRUD, Endpoints, Models for Firestore
│ ├── models - Common models for Firestore or PostgreSQL
│ ├── sqlmodel - CRUD, Endpoints, Models for SQLAlchemy
│ ├── main.py - Entrypoint, app instanciation & middleware
│ └── middleware.py - Middleware definitions (Metric, Logs, Exceptions)
│
├── iac - Terraform resources
│
├── migrations - PostgreSQL migrations
│
├── tests - PostgresSQL unit tests
│
├── deploy.sh - Deployment script
│
├── format.sh - Linting and formatting script
│
├── docker-compose.yml - Provide database local containers
│
├── main.tf - Terraform configuration for deployment
│
└── pyproject.toml
First, make sure ADC is configured correctly.
-
Init required resources and start deployment:
gcloud components update && gcloud components install beta
./deploy.sh -e <dev|staging|prod> # Will get the correct .env.x file & inject variables
# Creates required resources & IAM permissions
# - Secret in Secret Manager filled with .env.dev
# - Cloud Storage bucket to store terraform state
# - Artifact registry repository to store Cloud Run images
# - Required IAM permissions for Cloud Build default SA
# - run.admin
# - artifactregistry.admin
# - datastore.owner
# - cloudsql.admin
# - secretmanager.secretAccessor
# - storage.admin
# - serviceusage.serviceUsageAdmin
#
# - Cloud Build trigger to run deployment on push
# Then it starts the Cloud Build trigger
Cloud Build is now ready to auto deploy new Cloud Run revision after each push
./deploy.sh -e <dev|staging|prod> # Will get the correct .env.x file & inject variables
# - Replaces secret version content if it differs from .env.x
# - Runs the existing Cloud Build trigger
Enable Github Actions API in your repository
Actions are configured to run linting for every Pull Request on develop, uat and main branches
On push, .cloudbuild/cloudbuild.yaml will:
- Build and push new image
- re-apply the iac/main.tf infrastructure to ensure consistency
- Deploy the new Cloud Run revision
Use iac/main.tf to deploy new GCP resources if possible to make terraform aware of it
Digital Lab [email protected]