diff --git a/.circleci/README.md b/.circleci/README.md index ed3c0499d2..f86e7f34d7 100644 --- a/.circleci/README.md +++ b/.circleci/README.md @@ -1,3 +1,15 @@ +# CircleCI Configuration +## Environment Variables +When configuring CircleCI, you will need to set environment varaialbes the database +configuration as follows: +``` +FECFILE_DB_HOST=localhost +FECFILE_DB_USERNAME=postgres +FECFILE_DB_PASSWORD=postgres +FECFILE_DB_NAME=postgres +``` +NOTE that the FECFILE_DB_HOST is different here than what you need for your docker-compose configuration. + # Using CircleCI local CLI ## Install circleci local @@ -22,6 +34,18 @@ This can save a lot of time when trying to debug an issue in CI. circleci local execute --job JOB_NAME ``` +## Necessary Environment Variables +The Django backend expects to find the database login info in the environment. +To run in the local CircleCI for the django unit tests (for example), use the following: + +``` +circleci local execute -e FECFILE_DB_HOST=localhost \ + -e FECFILE_DB_USERNAME=postgres \ + -e FECFILE_DB_PASSWORD=postgres \ + -e FECFILE_DB_NAME=postgres \ + --job unit-test-django +``` + ## CircleCI configuration To get CircleCI to run tests, you have to configure the project in the Circle web applicaiton https://app.circleci.com/ \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index df54955e1b..c5c473d146 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -14,11 +14,7 @@ jobs: # A list of available CircleCI Docker convenience images are available here: https://circleci.com/developer/images/image/cimg/python docker: - image: cimg/python:3.7 - # Checkout the code as the first step. This is a dedicated CircleCI step. - # The python orb's install-packages step will install the dependencies from a Pipfile via Pipenv by default. - # Here we're making sure we use just use the system-wide pip. By default it uses the project root's requirements.txt. - # Then run your tests! - # CircleCI will report the results back to your VCS provider. + steps: - checkout @@ -39,11 +35,8 @@ jobs: # A list of available CircleCI Docker convenience images are available here: https://circleci.com/developer/images/image/cimg/python docker: - image: cimg/python:3.7 - # Checkout the code as the first step. This is a dedicated CircleCI step. - # The python orb's install-packages step will install the dependencies from a Pipfile via Pipenv by default. - # Here we're making sure we use just use the system-wide pip. By default it uses the project root's requirements.txt. - # Then run your tests! - # CircleCI will report the results back to your VCS provider. + - image: cimg/postgres:12.8 + steps: - checkout @@ -51,6 +44,12 @@ jobs: pkg-manager: pip app-dir: ~/project/django-backend/ pip-dependency-file: requirements.txt + + - run: + name: Wait for the database to be active + command: python wait_for_db.py + working_directory: ~/project/django-backend/ + - run: name: Run tests # This assumes pytest is installed via the install-package step above @@ -126,7 +125,7 @@ jobs: workflows: test: # This is the name of the workflow, feel free to change it to better match your workflow. jobs: -# - unit-test-django + - unit-test-django - lint-python - lint-angular # - unit-test-angular diff --git a/db/Dockerfile b/db/Dockerfile index 429d8cac85..a2a99336fd 100644 --- a/db/Dockerfile +++ b/db/Dockerfile @@ -1,4 +1,4 @@ -FROM postgres:10 +FROM postgres:12 ENV POSTGRES_USER=postgres ENV POSTGRES_NAME=postgres diff --git a/django-backend/requirements.txt b/django-backend/requirements.txt index e42970afd6..7ac1b46d0a 100644 --- a/django-backend/requirements.txt +++ b/django-backend/requirements.txt @@ -59,7 +59,7 @@ PyYAML==4.2b1 regex==2018.11.22 requests==2.23.0 requests-toolbelt==0.8.0 -retrying==1.3.3 +retry==0.9.2 s3transfer==0.1.13 simplegeneric==0.8.1 simplejson==3.16.0 diff --git a/django-backend/wait_for_db.py b/django-backend/wait_for_db.py index c6606074ad..0e2179fb03 100644 --- a/django-backend/wait_for_db.py +++ b/django-backend/wait_for_db.py @@ -1,12 +1,16 @@ import psycopg2 import os -from retrying import retry +from retry import retry -@retry(wait_fixed=2000) +@retry(delay=2, tries=15) def postgres_test(): try: + if os.environ.get('FECFILE_DB_NAME') == None and os.environ.get('FECFILE_DB_PASSWORD') == None: + print("Environment settings for database and password not found. Please check your settings and try again") + exit(1) + print("testing connection with dbname={} user={} host={} password={} connect_timeout=3000". format( os.environ.get('FECFILE_DB_NAME'), @@ -22,6 +26,7 @@ def postgres_test(): os.environ.get('FECFILE_DB_HOST'), os.environ.get('FECFILE_DB_PASSWORD'))) conn.close() + print ("DB connection successful") return True except ImportError: return False