Skip to content

Commit

Permalink
Create docker-image.yml (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput authored Oct 19, 2024
1 parent f6d6c09 commit 1c439c6
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 30 deletions.
20 changes: 11 additions & 9 deletions .example.env
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Adjust all values on the right side with your own user defined variable ie `POSTGRES_PASS=docker`
POSTGIS_DB=minisass_data
POSTGRES_USER=docker
POSTGRES_PASS=${POSTGRES_PASS}
POSTGRES_PASS=docker
ALLOW_IP_RANGE=0.0.0.0/0
POSTGRES_MULTIPLE_EXTENSIONS=postgis,hstore,postgis_topology,postgis_raster,pgrouting
POSTGRES_DB=minisass_data
FORCE_SSL=TRUE
FORCE_SSL=FALSE

DATABASE_HOST=db
DATABASE_PORT=5432
Expand All @@ -30,11 +30,11 @@ HTTP_PORT=80
# set this to production if you are deploying on your own domain with proper certificates
LETSENCRYPT_MODE=disabled
# set debug to false on production
DEBUG=
MEDIA_ROOT = /home/web/media
MEDIA_URL = /media/
STATIC_ROOT = /home/web/static
STATIC_URL = /static/
DEBUG=True
MEDIA_ROOT=/home/web/media
MEDIA_URL=/media/
STATIC_ROOT=/home/web/static
STATIC_URL=/static/
SENTRY_KEY=

SMTP_EMAIL=
Expand All @@ -50,8 +50,10 @@ MINIO_ROOT_PASSWORD=secure_minio_secret
BUCKET=demo

# Minio Kartoza
MINIO_ACCESS_KEY=xJBv0IxgtGViYqH6n6Sg
MINIO_SECRET_KEY=jw9Dxc2tAuUz5G1dXgn5lTHJvaInnJv5ML0pshki
MINIO_ACCESS_KEY=random_access_key
MINIO_SECRET_KEY=random_secret_key
AWS_ACCESS_KEY=random_access_key
AWS_SECRET_KEY=random_secret_key
MINIO_AI_BUCKET=minisass
MINIO_ENDPOINT=http://minio:9000
SECURE_CONNECTION=False
Expand Down
72 changes: 72 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Docker Image CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:

runs-on: ubuntu-latest

services:
db:
image: kartoza/postgis:16-3.4
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready -h localhost -p 5432 -U docker"
--health-interval=10s
--health-timeout=5s
--health-retries=5
env:
POSTGRES_DB: minisass_data
POSTGRES_USER: docker
POSTGRES_PASSWORD: docker
ALLOW_IP_RANGE: 0.0.0.0/0

steps:
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Copy docker-compose.override.template.yml to docker-compose.override.yml
run: cp docker-compose.override.template.yml docker-compose.override.yml

- name: Copy .example.env to .env
run: cp .example.env .env

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.event.pull_request.number }}
restore-keys: |
${{ runner.os }}-buildx-${{ github.event.pull_request.number }}
${{ runner.os }}-buildx-
- name: Start Docker Compose services
run: make main
env:
BUILDKIT_PROGRESS: plain
DOCKER_BUILDKIT: 1
BUILDX_CACHE_PATH: /tmp/.buildx-cache
# Wait for PostGIS container to be healthy before running tests
- name: Wait for PostGIS to be ready
run: |
until [ "$(docker inspect --format='{{json .State.Health.Status}}' $(docker compose ps -q db))" == "\"healthy\"" ]; do
echo "Waiting for PostGIS to be healthy..."
sleep 5
done
- name: Wait for PostGIS
run: sleep 30

- name: Run Django tests
run: make test

- name: Stop and remove services
run: docker compose -p minisass -f docker-compose.yml -f docker-compose.override.yml down
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ web:
@# We add --no-recreate so that it does not destroy & recreate the db container
@docker-compose up -d

main:
@echo
@echo "------------------------------------------------------------------"
@echo "Running main services for testing"
@echo "------------------------------------------------------------------"
@docker compose up -d db minio django

test:
@echo
@echo "------------------------------------------------------------------"
@echo "Running test"
@echo "------------------------------------------------------------------"7
@docker compose exec django bash -c "python manage.py test"

dev:
@echo
@echo "------------------------------------------------------------------"
Expand Down Expand Up @@ -133,7 +147,7 @@ migrate:
@echo "------------------------------------------------------------------"
@echo "Running migrate static in production mode"
@echo "------------------------------------------------------------------"
@docker-compose exec django python manage.py migrate
@docker compose exec django python manage.py migrate

# --------------- help --------------------------------

Expand Down
12 changes: 0 additions & 12 deletions deployment/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ fi
# create super user if one doesn't exist
python manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_superuser('${DJANGO_SUPERUSER_USERNAME}', '${DJANGO_SUPERUSER_EMAIL}', '${DJANGO_SUPERUSER_PASSWORD}')" 2>/dev/null || echo 'Superuser already exists, skipping.'

echo 'Updating active field for all users...'
export PGPASSWORD="${POSTGRES_PASS}"
psql -d "${DJANGO_DB}" -p 5432 -U "${POSTGRES_USER}" -h "${DATABASE_HOST}" -c "UPDATE auth_user SET is_active = TRUE;"


# Run tests
# echo 'Running tests.'

# Drop test DB if exists
psql -d "${DJANGO_DB}" -p 5432 -U "${POSTGRES_USER}" -h "${DATABASE_HOST}" -c "DROP DATABASE IF EXISTS test_${DJANGO_DB};"
python manage.py test

psql -d "${DJANGO_DB}" -p 5432 -U "${POSTGRES_USER}" -h "${DATABASE_HOST}" -f /home/web/django_project/webmapping/sql/observation.sql

echo "-----------------------------------------------------"
Expand Down
3 changes: 0 additions & 3 deletions django_project/minisass_authentication/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from minisass_authentication.models import Lookup, UserProfile, PasswordHistory
from minisass_authentication.forms import CustomUserAdminForm
from django import forms


@admin.register(Lookup)
Expand Down
42 changes: 41 additions & 1 deletion docker-compose.override.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,44 @@ services:
- "5000:8080"
# for ssh
- "5001:22"
entrypoint: [ ]
entrypoint: [ ]

django:
build:
context: .
dockerfile: ./deployment/docker/Dockerfile
volumes:
- static-data:/home/web/static
- media-data:/home/web/media
# Images on the platform will be stored here
- minio_images:/home/web/minio
environment:
- AWS_ACCESS_KEY=${MINIO_ROOT_USER:-minisass_admin}
- AWS_SECRET_ACCESS_KEY=${MINIO_ROOT_PASSWORD:-secure_minio_secret}
- DJANGO_DB=${POSTGRES_DB:-minisass_data}
- POSTGRES_USER=${POSTGRES_USER:-docker}
- POSTGRES_PASS=${POSTGRES_PASS:-docker}
- DATABASE_HOST=${DATABASE_HOST:-db}
- DATABASE_PORT=${DATABASE_PORT:-5432}
- SENTRY_KEY=${SENTRY_KEY:-}
- MINIO_ROOT=/home/web/minio
- MINIO_BUCKET=${BUCKET:-demo}
- MINIO_ACCESS_KEY=${MINIO_ROOT_USER:-minisass_admin}
- MINIO_SECRET_KEY=${MINIO_ROOT_PASSWORD:-secure_minio_secret}
- MINIO_AI_BUCKET=${BUCKET}
- MINIO_ENDPOINT=${MINIO_URL:-http://minio:9000}
- MINIO_URL=${MINIO_URL:-http://minio:9000}
- ALLOWED_HOSTS='*'
- DEV_SETUP=${DEV_SETUP}
depends_on:
db:
condition: service_healthy
restart: always
ports:
- "8080"
logging:
driver: json-file
options:
max-size: 200m
max-file: '10'

11 changes: 7 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
volumes:
- postgres_data:/var/lib/postgresql
environment:
- POSTGRES_DB=${POSTGIS_DB:-minisass}
- POSTGRES_DB=${POSTGIS_DB:-minisass_data}
- POSTGRES_USER=${POSTGRES_USER:-docker}
- POSTGRES_PASS=${POSTGRES_PASS:-docker}
- ALLOW_IP_RANGE=${ALLOW_IP_RANGE:-0.0.0.0/0}
Expand All @@ -22,7 +22,7 @@ services:
- "32769:5432"
restart: always
healthcheck:
test: "PGPASSWORD=${POSTGRES_PASS} pg_isready -h 127.0.0.1 -U ${POSTGRES_USER} -d minisass"
test: "PGPASSWORD=${POSTGRES_PASS} pg_isready -h 127.0.0.1 -U ${POSTGRES_USER} -d minisass_data"
interval: 5s
timeout: 10s
retries: 30
Expand Down Expand Up @@ -88,6 +88,8 @@ services:
# Images on the platform will be stored here
- minio_images:/home/web/minio
environment:
- AWS_ACCESS_KEY=${MINIO_ROOT_USER:-minisass_admin}
- AWS_SECRET_ACCESS_KEY=${MINIO_ROOT_PASSWORD:-secure_minio_secret}
- DJANGO_DB=${POSTGRES_DB:-minisass_data}
- POSTGRES_USER=${POSTGRES_USER:-docker}
- POSTGRES_PASS=${POSTGRES_PASS:-docker}
Expand All @@ -96,10 +98,11 @@ services:
- SENTRY_KEY=${SENTRY_KEY:-}
- MINIO_ROOT=/home/web/minio
- MINIO_BUCKET=${BUCKET:-demo}
- MINIO_ACCESS_KEY=${AWS_ACCESS_KEY}
- MINIO_SECRET_KEY=${AWS_SECRET_ACCESS_KEY}
- MINIO_ACCESS_KEY=${MINIO_ROOT_USER:-minisass_admin}
- MINIO_SECRET_KEY=${MINIO_ROOT_PASSWORD:-secure_minio_secret}
- MINIO_AI_BUCKET=${BUCKET}
- MINIO_ENDPOINT=${MINIO_URL:-http://minio:9000}
- MINIO_URL=${MINIO_URL:-http://minio:9000}
- ALLOWED_HOSTS='*'
- DEV_SETUP=${DEV_SETUP}
depends_on:
Expand Down
12 changes: 12 additions & 0 deletions wait_for_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# wait_for_db.sh
#!/bin/bash
# This script waits for the PostgreSQL database to be ready

echo "Waiting for the database to be ready..."

until pg_isready -h "$DATABASE_HOST" -p "$DATABASE_HOST" -U "$POSTGRES_USER"; do
echo "Database is not ready yet. Waiting..."
sleep 2
done

echo "Database is ready!"

0 comments on commit 1c439c6

Please sign in to comment.