Despliegue en PaaS - 23/24

De Wiki de EGC
Saltar a: navegación, buscar
import os 
import dj_database_url

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

ALLOWED_HOSTS = []
RENDER_EXTERNAL_HOSTNAME = os.environ.get('RENDER_EXTERNAL_HOSTNAME')
if RENDER_EXTERNAL_HOSTNAME:
    ALLOWED_HOSTS.append(RENDER_EXTERNAL_HOSTNAME)

SECRET_KEY = os.environ.get('SECRET_KEY')

DEBUG = 'RENDER' not in os.environ

# Modules in use, commented modules that you won't use
MODULES = [
    'authentication',
    'base',
    'booth',
    'census',
    'mixnet',
    'postproc',
    'store',
    'visualizer',
    'voting',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

STATIC_URL='/static/'
if not DEBUG:
    STATIC_ROOT=os.path.join(BASE_DIR, 'staticfiles')
    STATICFILES_STORAGE='whitenoise.storage.CompressedManifestStaticFilesStorage'

BASEURL = 'https://{}'.format(os.environ.get('RENDER_EXTERNAL_HOSTNAME'))

APIS = {
    'authentication': BASEURL,
    'base': BASEURL,
    'booth': BASEURL,
    'census': BASEURL,
    'mixnet': BASEURL,
    'postproc': BASEURL,
    'store': BASEURL,
    'visualizer': BASEURL,
    'voting': BASEURL,
}

DATABASE_URL = os.environ.get('DATABASE_URL')
DATABASES = {
    'default': dj_database_url.parse(DATABASE_URL)
}

ALLOWED_ORIGINS = ['https://{}'.format(os.environ.get('RENDER_EXTERNAL_HOSTNAME'))]
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = 'Strict'
CSRF_TRUSTED_ORIGINS = ALLOWED_ORIGINS.copy()

# number of bits for the key, all auths should use the same number of bits
KEYBITS = 256

  • launch.sh:
#!/bin/sh
cd decide/
cp local_settings.deploy.py local_settings.py
./manage.py createsuperuser --noinput
./manage.py collectstatic --noinput
./manage.py makemigrations
./manage.py migrate
gunicorn -w 5 decide.wsgi:application --timeout=500

  • requirements.txt:
Django==4.1
pycryptodome==3.15.0
djangorestframework==3.14.0
django-cors-headers==3.13.0
requests==2.28.1
django-filter==22.1
psycopg2==2.9.4
coverage==6.5.0
jsonnet==0.18.0
django-nose==1.4.6
django-rest-swagger==2.2.0
selenium==4.7.2
dj-database-url == 2.1.0
pynose == 1.4.8
whitenoise==6.5.0
gunicorn==21.2.0
‎
  • workflow build + deploy:
name: Python application

on:
  push:
    branches:
      - deploy2

jobs:
  build:

    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:14.9
        env:
          POSTGRES_USER: decide
          POSTGRES_PASSWORD: decide
          POSTGRES_DB: decide
        ports:
          - 5432:5432
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python 3.10.12
      uses: actions/setup-python@v4
      with:
        python-version: 3.10.12
    - name: psycopg2 prerequisites
      run: sudo apt-get install libpq-dev
    - name: Install dependencies and config
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
        pip install codacy-coverage
        cp decide/local_settings.gactions.py decide/local_settings.py
    - name: Run migrations (unnecessary)
      run: |
        cd decide
        python manage.py migrate
    - name: Run tests
      run: |
        cd decide
        ./manage.py test  --keepdb 
  
  deploy:
    runs-on: ubuntu-latest
    needs: build
    steps:
    - name: Deploy
      if: github.ref == 'refs/heads/deploy2'
      env:
        deploy_url: ${{secrets.RENDER_DEPLOY_HOOK_URL}}
      run: |
        curl "$deploy_url"