Skip to content

Commit

Permalink
Merge pull request #25 from TheSecretOrganization/23-create-action-to…
Browse files Browse the repository at this point in the history
…-build-containers

Workflow to check that containers are running
  • Loading branch information
antoineverin authored Aug 6, 2024
2 parents e3e2ebc + 5bef3eb commit dc838c6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/deployment-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Deployment Check
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
jobs:
docker-compose:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Create .env file
run: |
cat << EOF > .env
SECRET_KEY="foo"
DJANGO_SUPERUSER_USERNAME="bar"
DJANGO_SUPERUSER_EMAIL="[email protected]"
DJANGO_SUPERUSER_PASSWORD="foobar"
DJANGO_ALLOWED_HOSTS=localhost
DJANGO_CSRF_TRUSTED_ORIGINS=http://localhost
POSTGRES_DB="foodb"
POSTGRES_USER="bar"
POSTGRES_PASSWORD="foobar"
EOF
- name: Build and start Docker containers
run: docker compose up --build -d

- name: Wait for services to initialize
run: sleep 30

- name: Perform health checks
run: |
services=$(docker compose ps --services)
for service in $services; do
status=$(docker inspect -f '{{.State.Health.Status}}' $service)
if [ "$status" != "healthy" ]; then
echo "$service is not healthy"
exit 1
fi
done
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions django/src/rendering/management/commands/healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.core.management.base import BaseCommand
from django.db import connections
from django.db.utils import OperationalError

class Command(BaseCommand):
help = 'Perform a health check'

def handle(self, *args, **kwargs):
db_conn = connections['default']
try:
db_conn.cursor()
self.stdout.write(self.style.SUCCESS('Health check passed'))
except OperationalError:
self.stdout.write(self.style.ERROR('Database is unavailable'))
exit(1)
17 changes: 15 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ services:
depends_on:
postgres:
condition: service_healthy
healthcheck:
test: ["CMD-SHELL", "python manage.py healthcheck || exit 1"]
interval: 2s
timeout: 2s
retries: 5
start_period: 5s

nginx:
container_name: nginx
Expand All @@ -35,7 +41,14 @@ services:
- static-files:/static
- media-files:/media
depends_on:
- django
django:
condition: service_healthy
healthcheck:
test: ["CMD-SHELL", "curl -o /dev/null -s -w '%{http_code}' http://localhost/ | grep 200 || exit 1"]
interval: 2s
timeout: 2s
retries: 5
start_period: 5s

postgres:
container_name: postgres
Expand All @@ -52,7 +65,7 @@ services:
env_file:
- .env
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB} || exit 1"]
interval: 2s
timeout: 2s
retries: 5
Expand Down

0 comments on commit dc838c6

Please sign in to comment.