Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Continuous deployment via travis-ci #7

Open
wants to merge 4 commits into
base: feature/heroku
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ language: python
python:
- 3.6

script: flake8
script:
- flake8
- python manage.py test

deploy:
provider: heroku
app: caciviclab-ballot-api
run: python manage.py migrate
on: master
api_key:
secure: "FE+fi/wY2+LfPP2WZW1Tvvw24+ijldCr7BqIzeeHtKIdu8QK5WOhgdZ8rgkUXtoeGWx3j9vvGeaRCdmQr06kfJ2qk8dsidKSxiAPu3uvL4ppGwqhB+EqgDdMNoUKvStYeBdSNYOpytWYUOdKMu6DPUGwMxGQwItwmtNAHYTZ4ciTno6sdIaFi0/UXoPixGAgvxR+VzgNANy6+86Kl4dO9LMAHmeaPPebmQFOV4WvutEoryHsPklOJHDFEBcUnqRA1YK1FPPZ9Fpy88ZPF6Alx8O0f5B3TxadSgtEqgTMp7FGkpcGE9dh4/Tb6MESm/6l3EANNEEMs9iCqpvA3dboTcSqUkb7yqbEqt5Th5pB/gWxthgvlM25EiE/hCa36jB8kUAO/XjaH1QKBr7SwtvywzDdW4Xn5JXlsunQa3eW/GDUV4oADwRjGjq6kqQADj+FX19jLMR+NhBjZvJ4qQx43wSz8yKBWURl/oN/9B2YuEJJ//OtA8mEtLtL0vphiNXp4T6w/RcVL8hj5Jfe61ULgfX51QwRjbnzXJf0JurvfOZJBbO48K8ZPv0u1FTZ7Bn2QjSGo/bha+IHwUyqjaBEudrpbShE+BQ+YNcBOB2YnR0Zgi1n4snO1uZ3+V0HirtTsiHfDJdSA07l8jDWW4x9lbDFTmXEA6UwYOsOohTwZgc="

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should put this key in an environment variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think you could store Travis deploy secrets in an environment variable. As far as I know, storing encrypted secrets in the Travis config is the preferred way to do this.

31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Build
Status](https://travis-ci.org/adborden/ballot-api.svg?branch=master)](https://travis-ci.org/adborden/ballot-api)
Status](https://travis-ci.org/caciviclab/ballot-api.svg?branch=master)](https://travis-ci.org/caciviclab/ballot-api)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we be using the master branch for CI? or do we want to use an integration (or other name) for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just start with master. If we add additional environments we can change the branch later.


# Ballot API

Expand All @@ -20,3 +20,32 @@ California.
$ python manage.py runserver

And open your browser to http://localhost:8000


## Development

Please don't forget to run the tests.

$ flake8
$ python manage.py test


## Deployment

The ballot-api can easily be deployed to Heroku. After creating an initial app
in Heroku, you'll want to set these environment variables. `DATABASE_URL` should
be set automatically after enabling the PostgreSQL add-on.

| Environment variable | Description | Example |
| ------------------------ | ----------- | ------- |
| `ALLOWED_HOSTS` | Comma-separated list of host names for the app | caciviclab-ballot-api.herokuapp.com |
| `DATABASE_URL` | [Database connection URL](https://pypi.python.org/pypi/dj-database-url) (set by PostgreSQL add-on) | postgresql://user:password@hostname:port/database_name?options |
| `DJANGO_SETTINGS_MODULE` | Settings module to use for Django | ballot_api.settings.heroku |
| `SECRET_KEY` | Django secret key | a-random-string-of-50-characters |


### Continuous deployment

The `caciviclab-ballot-api` is deployed continuously to Heroku via Travis by merging to
the `master` branch. Once tests and linting checks are good, `master` is
deployed and any pending migrations will be run.
17 changes: 12 additions & 5 deletions ballot_api/settings/heroku.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

DEBUG = False

ALLOWED_HOSTS = [
'caciviclab-ballot-api.herokuapp.com',
]

if 'ALLOWED_HOSTS' not in os.environ:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this:

ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', ['*'])

When it's stored in an env variable, we should be able to format like so:
export ALLOWED_HOSTS="['host.com', 'herokuapp.com', '*.localhost']"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll try that.

raise Exception('ALLOWED_HOSTS environment variable must be set to a comma-separated list of host names for heroku configuration.')

ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS').split(',')


if 'DATABASE_URL' not in os.environ:
raise Exception('DATABASE_URL environment variable must be set for heroku configuration.')

DATABASES['default'] = dj_database_url.config()

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

if not SECRET_KEY:
if 'SECRET_KEY' not in os.environ:
raise Exception('SECRET_KEY environment variable must be set for heroku configuration.')

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for ALLOWED_HOSTS

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