By tomahony
I’m trying to deploy a Django application using a Digital Ocean Postgres database and Redis instance on App Platform.
Here is my App Spec:
alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
databases:
- cluster_name: postgres
engine: PG
name: postgres
production: true
version: "13"
- cluster_name: redis
engine: REDIS
name: redis
production: true
version: "6"
envs:
- key: DJANGO_ALLOWED_HOSTS
scope: RUN_TIME
value: ${APP_DOMAIN}
- key: DATABASE_URL
scope: RUN_TIME
value: ${postgres.DATABASE_URL}
- key: REDIS_URL
scope: RUN_TIME
value: ${redis.REDIS_URL}
name: myapp
region: fra
services:
- environment_slug: python
envs:
- key: DISABLE_COLLECTSTATIC
scope: RUN_AND_BUILD_TIME
value: "1"
github:
branch: main
deploy_on_push: true
repo: mygithubuser/mygithubrepo
http_port: 8080
instance_count: 1
instance_size_slug: basic-xxs
name: myapp
routes:
- path: /
run_command: gunicorn --worker-tmp-dir /dev/shm config.wsgi
source_dir: /
jobs:
- name: migrate
kind: PRE_DEPLOY
github:
repo: mygithubuser/mygithubrepo
branch: main
deploy_on_push: true
run_command: python manage.py migrate
envs:
- key: DISABLE_COLLECTSTATIC
value: "1"
scope: BUILD_TIME
static_sites:
- name: static
github:
repo: mygithubuser/mygithubrepo
branch: main
deploy_on_push: true
output_dir: staticfiles
routes:
- path: /static
I have tested this setup without the static_sites component and it builds and deploys OK, but I need my static files collected by Django with python manage.py collectstatic.
Thefore I’ve added the static_sites component but I now get the error:
django.core.exceptions.ImproperlyConfigured: Set the {postgres.DATABASE_URL} environment variable
I believe this is because the environment variables DATABASE_URL=${postgres.DATABASE_URL} is not available at build time, only at run time?
If so, why is there an example of static files being collected in the example Django app here using this method?
https://github.com/digitalocean/sample-django/blob/main/.do/app.yaml
How can you make collectstatic a part of the build/deploy process?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
Indeed, it seems like that you’ve set the scope of the database URL env variable to RUN_TIME only:
- key: DATABASE_URL
scope: RUN_TIME
value: ${postgres.DATABASE_URL}
You need to change the scope so that it is available in both the runtime and the build time scopes:
https://docs.digitalocean.com/products/app-platform/how-to/use-environment-variables/
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.