Skip to content

Commit

Permalink
Merge pull request #4 from albertcrowley/ci-database
Browse files Browse the repository at this point in the history
Connect Postgres DB in Circle CI with Django
Automated test failures expected at this point in spinning up the project.
  • Loading branch information
mjtravers authored Dec 13, 2021
2 parents d3453ec + 4445e78 commit 498c053
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
24 changes: 24 additions & 0 deletions .circleci/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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/
21 changes: 10 additions & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -39,18 +35,21 @@ 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

- python/install-packages:
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion db/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM postgres:10
FROM postgres:12

ENV POSTGRES_USER=postgres
ENV POSTGRES_NAME=postgres
Expand Down
2 changes: 1 addition & 1 deletion django-backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions django-backend/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -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
Expand Down

0 comments on commit 498c053

Please sign in to comment.