0

I am trying to build a CI with Travis for my docker app. In my docker compose I import a file called ".env". This file is gitignored so Travis cant use it. To fix the problem, I create the empty file in my .travis.yml file and set the environment variables on the website :

language: python
python:
  - "3.6"

services:
  - docker

before_script:
  - touch .env
  - pip install docker-compose

script:
  - docker-compose run web sh -c "python manage.py test"

When I push on git, everything seem to work Travis side until the test start and Travis come to this line of code in my app :

ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")

There I have this error in Travis logs :

  File "/home/pur_beurre/web/pur_beurre/settings.py", line 29, in <module>
    ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")
AttributeError: 'NoneType' object has no attribute 'split'
1
The command "docker-compose run web sh -c "python manage.py test"" exited with 1.

Note : DJANGO_ALLOWED_HOSTS = localhost

  • if you are importing environmental variables from `.env` you need to use `python-dotenv` package. "os.environ.get()" imports system environmental variables not from .env file – EvilReboot Feb 14 '22 at 15:25
  • @EvilReboot Do you mean that my app is not supposed to work if I dont use python-dotenv package ? Because my problem is only with Travis, my app work. – Barthélémy Déchanet Feb 14 '22 at 15:35

1 Answers1

0

When and where do you run export DJANGO_ALLOWED_HOSTS = localhost?

Also how do you call docker-compose run etc.?

You should consider that in order for your environment variables to be available to your docker-compose.yml file they need to be called from the same terminal where you exported DJANGO_ALLOWED_HOSTS.

You need to source your env file before you call docker-compose up -d as described in this answer:

set -a
source .my-env
docker-compose up -d

I advise you to read the answer I linked above.

pugi
  • 323
  • 1
  • 12
  • I dont use export, I defined the environments variables on the Travis website. My problem is not with the app, its only with Travis. – Barthélémy Déchanet Feb 14 '22 at 15:34
  • When your docker container is up it has its own environment. In order for Travis environment variables to be available to your docker you need to source them before you call run `docker-compose up -d`. – pugi Feb 14 '22 at 15:38
  • I dont call ```docker-compose up -d```. I just push on github, then the travis.yaml file launch on travis.com. My app is fine, my only problem is with Travis, and my .env file is gitinored so in travis.com, .env refer to a empty file. – Barthélémy Déchanet Feb 14 '22 at 15:45
  • so your .env file doesn't have any DJANGO_ALLOWED_HOSTS variable you need to export it after you create your empty .env file – pugi Feb 14 '22 at 16:13