language: python
python:
  - 3.6

cache:
  pip: true

# Use container-based infrastructure
dist: trusty
sudo: required

services:
  - postgresql
  - docker

addons:
  postgresql: '9.6'

env:
  global:
    - DJANGO_SETTINGS_MODULE=eventrack.settings.prod
    - DATABASE_URL=postgres://postgres@localhost/test_db
    - CFG_SECRET_KEY=iamnotsosecret
    - CFG_ALLOWED_HOSTS=localhost

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

install:
  - pip install -r requirements.txt

script:
  # Check migrations and run tests
  - python manage.py check
  - python manage.py makemigrations --check
  - python manage.py test

  # Build a docker image
  - docker build -t fedorselitsky/eventrack .
  - docker images fedorselitsky/eventrack

  # Push an image into the public docker registry
  # Should only work on the master branch.
  #
  # Note that $TRAVIS_BRANCH for builds triggered by a pull request will have
  # the name of the branch targeted by the pull request,
  # so we also need to check $TRAVIS_PULL_REQUEST to prevent pushing
  # on pull requests targeted to the master branch.
  # See https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
  #
  # Another important note is thar we should run this as part of the "script"
  # stage, because later stages (such as "after_script" and "after_success")
  # do not affect the build result and we want to receive notifications,
  # in case of failed attempts to push to the docker registry.
  - >
    if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
      docker login -u "$DOCKER_HUB_USERNAME" -p "$DOCKER_HUB_PASSWORD";
      docker push fedorselitsky/eventrack;
    fi;

Build docker images in CI
Build docker images in CI

Only push from the master branch
