Skip to content

πŸ”¬ Sandbox project combining Angular, Django and Google Cloud Platform πŸš€ Deployed via Github Actions, provisioned with Terraform 🎯 For learning and testing purposes β€” not production-ready

Notifications You must be signed in to change notification settings

DracarysBZH/sandbox-gcp-django-angular

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ§ͺ SANDBOX [GCP - DJANGO - ANGULAR]

NOTES

  • This repository is a sandbox for testing and learning purposes.
  • It is not intended for production use.
  • In production, the service account should be created with the least privilege principle
  • The service account to deploy the infrastructure should not have the same permissions as the service account to deploy the application

πŸš€ INFRASTRUCTURE

πŸš€ Deploy Infrastructure

πŸ“Œ Overview

Example of a GitHub Actions workflow to:

  • Run Terraform to create a Cloud Run service
  • Authenticate with GCP using a service account key (stored in GitHub secrets)

🧰 Prerequisites

  1. Access the project

  2. Create a service account

    • IAM & Admin β†’ Service Accounts
    • Create a service account
      • Name
      • ID
    • Create and continue
  3. Assign roles

    • Cloud Run Admin
    • Service Account User
    • Storage Object User
    • Artifact Registry Administrator
    • Secret Manager Secret Accessor
  4. Finish the creation process.

  5. Create a JSON key

    • In the account list, click the newly created service account
    • Keys β†’ Add key β†’ Create new key
      • Choose JSON
      • Download the file
  6. Add this key to GitHub

    • GitHub repo β†’ Settings β†’ Secrets and variables β†’ Actions
    • Add a new secret
      • Name: GCP_SA_KEY
      • Value: downloaded .json file
  7. Add the other secrets

    • GCP_PROJECT_ID
    • GCP_REGION
  8. Create a GCS bucket

    • Cloud Storage β†’ Create bucket
      • Name: gcs-sandbox-gcp-django-angular
      • Region: europe-west1
      • Storage class: Standard
      • Access control: Uniform
  9. In the secrets manager add a DJANGO_SECRET_KEY, generate it with the following command:

    • python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'

πŸ“€ Launch the workflow

Once all the prerequisites are in place:

  1. Go to the Actions tab in the GitHub repository
  2. Select πŸš€ Deploy Infrastructure
  3. Click Run workflow

This will trigger the Terraform deployment to GCP

🐍 DJANGO APP

πŸ“€ Deploy Django app to Cloud Run

πŸ“Œ Overview

This project is a sandbox Django application deployed on Google Cloud Run, following the principles of:

  • Hexagonal Architecture (Ports & Adapters)
  • Continuous Deployment via GitHub Actions
  • Docker for containerization.

🧰 Prerequisites

  1. Create a service account with the following roles:
    • Artifact Registry Administrator
  2. Follow the instruction in πŸš€ INFRASTRUCTURE

πŸ“ Project Structure

djangoapi/
β”œβ”€β”€ djangoapi/                     # Django settings
β”‚   β”œβ”€β”€ settings.py
β”‚   β”œβ”€β”€ urls.py
β”‚   └── wsgi.py
β”œβ”€β”€ todo_list/                     # Todo list application
β”‚   β”œβ”€β”€ adapters/                  # Interface Adapters (HTTP, ORM, etc.)
β”‚   β”‚   β”œβ”€β”€ http/                  # REST views, serializers, urls
β”‚   β”‚   β”œβ”€β”€ factory/               # Object creation logic
β”‚   β”‚   β”œβ”€β”€ repositories/          # ORM implementation of SPI ports
β”‚   β”‚   └── exceptions/
β”‚   β”œβ”€β”€ domain/                    # Business logic and domain entities
β”‚   β”‚   β”œβ”€β”€ entities/
β”‚   β”‚   └── services/
β”‚   β”œβ”€β”€ ports/                     # Ports (interfaces)
β”‚   β”‚   β”œβ”€β”€ api/                   # Input ports (commands from interface)
β”‚   β”‚   └── spi/                   # Output ports (e.g., persistence)
β”‚   β”œβ”€β”€ services/                  # Application services (use cases)
β”‚   β”œβ”€β”€ tests/                     # Unit and integration tests
β”‚   β”œβ”€β”€ migrations/                # Django migrations
β”‚   └── apps.py
β”œβ”€β”€ manage.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ requirements-dev.txt
β”œβ”€β”€ Dockerfile
└── db.sqlite3

This layout follows the hexagonal (ports and adapters) architecture, where:

  • domain/ contains core business rules,
  • ports/ defines interfaces for communication (input/output),
  • adapters/ implements those interfaces for HTTP, ORM, etc.,
  • services/ contains orchestration logic (use cases),
  • tests/ includes unit/integration tests.

This separation enhances modularity, testability, and scalability.

πŸš€ Running Locally

  1. Create a virtualenv and install the dependencies.
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install -r requirements-dev.txt
  1. Initialize the migrations
python manage.py makemigrations
python manage.py migrate
  1. Start the local server
python manage.py runserver

πŸ§ͺ Running Tests

python manage.py test todo_list.tests

πŸ“€ Deploy to Cloud Run

Once all the prerequisites are in place, the django app is ready to be deployed to Cloud Run on push to the main branch.

The workflow:

  • Builds the Docker image.
  • Pushes it to Artifact Registry
  • Deploys to Cloud Run using gcloud

πŸ…°οΈ ANGULAR APP

πŸ“€ Deploy Angular app to Cloud Run

πŸ“Œ Overview

This Angular application is deployed on Google Cloud Run, following the principles of:

  • Modular Architecture
  • Continuous Deployment via GitHub Actions
  • Docker for containerization

🧰 Prerequisites

  1. Follow the instructions in πŸš€ INFRASTRUCTURE

πŸ“ Project Structure

angular-app/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ core/                        # Core features module
β”‚   β”‚   β”‚   β”œβ”€β”€ guards/
β”‚   β”‚   β”‚   β”œβ”€β”€ interceptors/
β”‚   β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”‚   └── services/
β”‚   β”‚   β”œβ”€β”€ features/                    # Feature modules
β”‚   β”‚   β”‚   └── main-layout/             # Main layout
β”‚   β”‚   β”‚   └── todo-list/               # Todo list feature
β”‚   β”‚   β”‚       β”œβ”€β”€ components/
β”‚   β”‚   β”‚       β”‚   β”œβ”€β”€ create-item/
β”‚   β”‚   β”‚       β”‚   β”œβ”€β”€ item/
β”‚   β”‚   β”‚       β”‚   └── todo-list/
β”‚   β”‚   β”‚       β”œβ”€β”€ models/              # Todo interfaces & types
β”‚   β”‚   β”‚       └── services/            # Todo-specific services
β”‚   β”‚   β”œβ”€β”€ app.config.ts
β”‚   β”‚   └── app.routes.ts
β”‚   β”œβ”€β”€ environments/                    # Environment configs
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ main.ts
β”‚   └── styles.scss                      # Global styles
β”œβ”€β”€ angular.json                         # Angular CLI configuration
β”œβ”€β”€ Dockerfile                           # Docker configuration
β”œβ”€β”€ package.json                         # Dependencies & scripts
└── tsconfig.json                        # TypeScript configuration

πŸš€ Running Locally

  1. Install dependencies
npm install
  1. Start the development server
npm start

πŸ§ͺ Running Tests

npm run test

πŸ“€ Deploy to Cloud Run

Once all the prerequisites are in place, the Angular app is ready to be deployed to Cloud Run on push to the main branch.

The workflow:

  • Builds the Docker image
  • Pushes it to Artifact Registry
  • Deploys to Cloud Run using gcloud

About

πŸ”¬ Sandbox project combining Angular, Django and Google Cloud Platform πŸš€ Deployed via Github Actions, provisioned with Terraform 🎯 For learning and testing purposes β€” not production-ready

Topics

Resources

Stars

Watchers

Forks