Skip to content
Merged
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
47 changes: 25 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# ERCS Deploy

Docker Compose setup for deploying ERCS, including the `backend`, `frontend`, and `cms` applications. The web applications are served through nginx, with Traefik handling the external routing.
Docker Compose setup for deploying ERCS, including the `backend`, `frontend`, and `cms` applications. The web applications are served by nginx, with Traefik handling external routing.

## Setup

Before starting the services, initialize the Git submodules and create the required environment files:

```bash
git submodule update --init --recursive
./scripts/sub-module-sync

cp .env.sample .env

cp env/backend.env.sample env/backend.env

cp env/frontend.env.sample env/frontend.env

cp env/cms.env.sample env/cms.env
```

Expand All @@ -24,29 +22,26 @@ Fill in the required values in the environment files before starting the deploym

## Profiles

The Compose setup is divided into profiles:
The Compose setup is divided into two independent profiles:

| Profile | Services |
| ------------ | ------------------------------------------------------------------- |
| `core` | postgres, redis, ollama, web, worker, worker-beat, nginx |
| `web-builds` | frontend, cms (one-shot builds that output to `./data/web-builds/`) |
| Profile | Services |
| ------------ | ------------------------------------------------------------------------- |
| `core` | postgres, redis, ollama, web, worker, worker-beat, nginx |
| `web-builds` | frontend, cms (one-shot builds that output files to `./data/web-builds/`) |

The profiles do not depend on each other and can be run independently.

## Running the Deployment

The two profiles are independent and do not depend on each other.

### 1. Build the web applications

Run the frontend and CMS builds:
Build the frontend and CMS applications:

```bash
docker compose run --rm --build frontend

docker compose run --rm --build cms
task web-builds
```

These commands build the applications and place the generated files under `./data/web-builds/`.
The generated files are placed under `./data/web-builds/` and are served by nginx.

### 2. Start the core services

Expand All @@ -56,7 +51,15 @@ Once the web applications have been built, start the core services:
docker compose --profile core up -d
```

nginx serves the contents of `./data/web-builds/` directly. Therefore, whenever the frontend or CMS needs to be redeployed, simply repeat step 1 to rebuild the relevant application.
nginx serves the generated files from `./data/web-builds/` directly.

Whenever the frontend or CMS needs to be redeployed, rebuild the relevant application by running:

```bash
task web-builds
```

There is no need to restart nginx unless its configuration has changed.

## Running Migrations

Expand All @@ -68,13 +71,13 @@ docker compose exec web ./manage.py migrate

## Creating a Superuser

To create a new superuser:
Create a new Django superuser with:

```bash
docker compose exec web ./manage.py createsuperuser
```

## Loading Seed Data
## Loading Seed Data (Optional)

The `seed_data/db.json` fixture contains initial data for ERCS, including:

Expand All @@ -89,13 +92,13 @@ docker compose exec web ./manage.py loaddata seed_data/db.json

> **Note:** The fixture includes a pre-created admin account: `admin@togglecorp.com`.
>
> If a superuser with the same email already exists in the database, loading the fixture will fail with a conflict. In that case, either delete the existing user first or skip the user fixture when loading the seed data.
> If a superuser with the same email already exists in the database, loading the fixture may fail due to a conflict. In that case, either delete the existing user first or skip the user fixture when loading the seed data.

## Syncing Geographic Data

Administrative areas such as countries, regions, and zones are sourced from the IFRC GO API.

To synchronize the geographic data with the database, run:
To synchronize geographic data with the database, run:

```bash
docker compose exec web ./manage.py sync_geo
Expand All @@ -117,7 +120,7 @@ To synchronize all forms:
docker compose exec web ./manage.py sync_kobo
```

To synchronize only a specific form, for example the Emergency Alert form:
To synchronize a specific form, for example, the Emergency Alert form:

```bash
docker compose exec web ./manage.py sync_kobo --form alert
Expand Down
22 changes: 22 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# yaml-language-server: $schema=https://taskfile.dev/schema.json

version: '3'
env:
FRONTEND_COMMIT_HASH:
sh: ./scripts/get-commit-hash.sh .git/modules/frontend
CMS_COMMIT_HASH:
sh: ./scripts/get-commit-hash.sh .git/modules/cms


tasks:
web-build-frontend:
cmds:
- docker compose --profile web-builds up --build
web-build-cms:
cmds:
- docker compose --profile web-builds up --build
web-builds:
cmds:
- task: web-build-frontend
- task: web-build-cms
- echo "Success"
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ services:
APP_ENVIRONMENT: ${WEB_APP_ENVIRONMENT?error}
APP_GRAPHQL_ENDPOINT: https://${BACKEND_HOST?error}/graphql/
APP_GRAPHQL_CODEGEN_ENDPOINT: ./backend/schema.graphql
APP_COMMIT_HASH: ${FRONTEND_COMMIT_HASH}
command: sh -c 'pnpm generate:type && pnpm build'
volumes:
- ./data/web-builds/frontend:/code/build
Expand All @@ -186,6 +187,7 @@ services:
APP_ENVIRONMENT: ${WEB_APP_ENVIRONMENT?error}
APP_GRAPHQL_ENDPOINT: https://${BACKEND_HOST?error}/graphql/
APP_GRAPHQL_CODEGEN_ENDPOINT: ./backend/schema.graphql
APP_COMMIT_HASH: ${CMS_COMMIT_HASH}
command: sh -c 'pnpm generate:type && pnpm build'
volumes:
- ./data/web-builds/cms:/code/build
Expand Down
30 changes: 30 additions & 0 deletions scripts/get-commit-hash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# Set the base Git directory (default to current .git directory)
BASE_DIR="${1:-.git}"
HEAD_PATH="$BASE_DIR/HEAD"

# Function to get the current commit SHA
if [[ ! -f "$HEAD_PATH" ]]; then
echo "HEAD file not found: $HEAD_PATH" >&2
exit 1
fi

# Read the content of HEAD
ref=$(<"$HEAD_PATH")
ref=$(echo "$ref" | tr -d '\n')

if [[ "$ref" == ref:\ * ]]; then
# It's a symbolic ref
ref_path="$BASE_DIR/${ref#ref: }"
if [[ -f "$ref_path" ]]; then
commit_hash=$(<"$ref_path")
echo "$commit_hash"
else
echo "Ref file not found: $ref_path" >&2
exit 1
fi
else
# Detached HEAD
echo "$ref"
fi
Empty file modified scripts/run_ollama.sh
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions scripts/sub-module-sync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash -x
git submodule foreach 'git fetch'
git submodule foreach 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo main)'
Loading