reduce production image size without runtime drift
Some checks failed
Upload latest backend image to GHCR / upload (push) Failing after 2m45s
Test Backend / Build and Test Backend (push) Failing after 2m3s
Upload latest frontend image to GHCR / upload (push) Failing after 13s
Test Frontend / Build and Test Frontend (push) Successful in 10m51s
Trivy Security Scans / Trivy Filesystem Scan (Source Code) (push) Failing after 1m43s
Trivy Security Scans / Trivy Docker Image Scan (Backend & Frontend) (push) Failing after 27s

This commit is contained in:
alex wiesner
2026-03-16 15:07:36 +00:00
parent f24aa53575
commit 7a53cc2cc7
6 changed files with 119 additions and 103 deletions

View File

@@ -1,29 +1,45 @@
#!/bin/bash
#!/bin/sh
# Function to check PostgreSQL availability
# Helper to get the first non-empty environment variable
get_env() {
for var in "$@"; do
value="${!var}"
if [ -n "$value" ]; then
echo "$value"
return
fi
done
for var in "$@"; do
eval "value=\${$var:-}"
if [ -n "$value" ]; then
printf '%s\n' "$value"
return 0
fi
done
return 1
}
check_postgres() {
local db_host
local db_user
local db_name
local db_pass
db_host=$(get_env PGHOST)
db_host=${db_host:-localhost}
db_port=$(get_env PGPORT)
db_port=${db_port:-5432}
db_user=$(get_env PGUSER POSTGRES_USER)
db_name=$(get_env PGDATABASE POSTGRES_DB)
db_pass=$(get_env PGPASSWORD POSTGRES_PASSWORD)
db_host=$(get_env PGHOST)
db_user=$(get_env PGUSER POSTGRES_USER)
db_name=$(get_env PGDATABASE POSTGRES_DB)
db_pass=$(get_env PGPASSWORD POSTGRES_PASSWORD)
"${VIRTUAL_ENV:-/opt/venv}/bin/python" - "$db_host" "$db_port" "$db_user" "$db_name" "$db_pass" <<'PY' >/dev/null 2>&1
import sys
PGPASSWORD="$db_pass" psql -h "$db_host" -U "$db_user" -d "$db_name" -c '\q' >/dev/null 2>&1
import psycopg2
host, port, user, dbname, password = sys.argv[1:]
conn = psycopg2.connect(
host=host,
port=int(port),
user=user,
dbname=dbname,
password=password,
connect_timeout=1,
)
conn.close()
PY
}
@@ -39,12 +55,12 @@ done
# psql -h "$PGHOST" -U "$PGUSER" -d "$PGDATABASE" -f /app/backend/init-postgis.sql
# Apply Django migrations
python manage.py migrate
${VIRTUAL_ENV:-/opt/venv}/bin/python manage.py migrate
# Create superuser if environment variables are set and there are no users present at all.
if [ -n "$DJANGO_ADMIN_USERNAME" ] && [ -n "$DJANGO_ADMIN_PASSWORD" ] && [ -n "$DJANGO_ADMIN_EMAIL" ]; then
echo "Creating superuser..."
python manage.py shell << EOF
${VIRTUAL_ENV:-/opt/venv}/bin/python manage.py shell << EOF
from django.contrib.auth import get_user_model
from allauth.account.models import EmailAddress
@@ -76,7 +92,7 @@ fi
# Sync the countries and world travel regions
# Sync the countries and world travel regions
python manage.py download-countries
${VIRTUAL_ENV:-/opt/venv}/bin/python manage.py download-countries
if [ $? -eq 137 ]; then
>&2 echo "WARNING: The download-countries command was interrupted. This is likely due to lack of memory allocated to the container or the host. Please try again with more memory."
exit 1
@@ -84,4 +100,4 @@ fi
cat /code/voyage.txt
exec "$@"
exec "$@"