Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Docker image building, testing and pushing to Travis #920

Open
wants to merge 1 commit into
base: master
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
84 changes: 60 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
language: python

python:
- "3.6"

install:
# Install conda
- wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh
- bash miniconda.sh -b -p $HOME/miniconda
- export PATH="$HOME/miniconda/bin:$PATH"
- python -c 'import os,sys,fcntl; flags = fcntl.fcntl(sys.stdout, fcntl.F_GETFL); fcntl.fcntl(sys.stdout, fcntl.F_SETFL, flags&~os.O_NONBLOCK);'
- conda clean --all -y
- ./python_env_build.sh
- source activate pb_env
- python manage.py collectstatic --no-input

addons:
postgresql: "9.4"

before_script:
- psql -c 'create database test_db;' -U postgres

script:
- py.test webapp/apps/
matrix:
include:
- language: ruby # Cannot be 'minimal' in order to have PostgreSQL

sudo: required

services: docker

env: TAG=${TRAVIS_COMMIT::6}

# Need NEW_RELIC_TOKEN to be defined in Travis settings
install:
- make NEW_RELIC_TOKEN=$NEW_RELIC_TOKEN webapp-build
- pip install --user git-lint pylint pycodestyle
- export PATH=$PATH:/$HOME/.local/bin

addons:
postgresql: "9.4"

before_script: psql -c 'create database mypb;' -U postgres

script:
- >
docker run --net host -e PORT=80 -e DJANGO_SETTINGS_MODULE=webapp.settings
-e DROPQ_WORKERS=127.0.0.1:5050
-e DATABASE_URL=postgresql://postgres@localhost/mypb
-it opensourcepolicycenter/web:$TAG
/bin/bash -c "pip install -q pytest-django && py.test webapp/apps"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's move pytest-django into the requirements.txt file. That way people can run these tests locally with ease.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's already in requirements_dev.txt, which gets installed by the python_env_build.sh script which is run by end-users. So I think it actually makes sense the current way, right? It probably shouldn't be in the image that gets deployed to e.g. Heroku.

- if git reset --soft ${TRAVIS_COMMIT_RANGE%...*}; then git lint; fi

before_deploy:
# Need HEROKU_TOKEN to be defined in Travis settings
- curl https://cli-assets.heroku.com/install.sh | sh
- echo "machine api.heroku.com password $HEROKU_TOKEN" > ~/.netrc
- docker login --username=_ --password=$HEROKU_TOKEN registry.heroku.com

deploy:
provider: script
script: make MODE=test webapp-release
on:
branch: travis

- language: minimal

sudo: required

services: docker

# Also need OSPC_ANACONDA_TOKEN, AWS_ACCOUNT_ID, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to be defined in settings
env: TAG=${TRAVIS_COMMIT::6}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doe this grab the first six characters of the git commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, exactly. This doesn't actually get copied anywhere beyond the Travis instance as far as I know, so it's not too important, but the 6-character git commit hash seemed good to use.


install: make OSPC_ANACONDA_TOKEN=$OSPC_ANACONDA_TOKEN dist-build

script: make dist-test

deploy:
provider: script
script: bash distributed/deploy.sh staging
on:
branch: travis
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ webapp-build:
docker build --build-arg NEW_RELIC_TOKEN=$(NEW_RELIC_TOKEN) -t opensourcepolicycenter/web:$(TAG) ./

webapp-push:
docker tag opensourcepolicycenter/web:$(TAG) registry.heroku.com/ospc-$(VERSION)/web
docker push registry.heroku.com/ospc-$(VERSION)/web
docker push opensourcepolicycenter/web:$(TAG)

webapp-release:
heroku container:release web -a ospc-$(VERSION)
docker tag opensourcepolicycenter/web:$(TAG) registry.heroku.com/ospc-$(MODE)/web
docker push registry.heroku.com/ospc-$(MODE)/web
heroku container:release web -a ospc-$(MODE)
21 changes: 21 additions & 0 deletions distributed/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mode=$1
region=us-east-2

pip install --user awscli
eval $(aws ecr get-login --region $region --no-include-email)

function tagpush() {
docker tag opensourcepolicycenter/$1:$TAG $AWS_ACCOUNT_ID.dkr.ecr.$region.amazonaws.com/$mode-celeryflask:$1
docker push $AWS_ACCOUNT_ID.dkr.ecr.$region.amazonaws.com/$mode-celeryflask:$1
}

tagpush flask
tagpush celery

function redeploy() {
aws ecs update-service --cluster $mode-ecs-cluster --service $mode-$1 --region $region --force-new-deployment >/dev/null 2>&1
echo "$1 deploy exit code: $?"
}

redeploy flask
redeploy celery
2 changes: 0 additions & 2 deletions webapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@

# if os.environ.get('DATABASE_URL', None):
# Parse database configuration from $DATABASE_URL
import dj_database_url
TEST_DATABASE = {
'TEST': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
Expand All @@ -120,7 +119,6 @@
}
if os.environ.get('DATABASE_URL', None): # DATABASE_URL var is set
DATABASES = {'default': dj_database_url.config()}
DATABASES.update(TEST_DATABASE)
else: # DATABASE_URL is not set--try default
DATABASES = {
'default': {
Expand Down