Add Docker support to Travis and GitLab CI

* Celerybeat start files wait for table migration so that errors don't occur if a migration does not happen post-build
This commit is contained in:
Andrew-Chen-Wang 2020-04-14 11:52:14 -04:00
parent 484fa4ae77
commit 3c63b3e46e
5 changed files with 132 additions and 14 deletions

View File

@ -181,7 +181,15 @@ def test_travis_invokes_pytest(cookies, context):
with open(f"{result.project}/.travis.yml", "r") as travis_yml:
try:
assert yaml.load(travis_yml, Loader=yaml.FullLoader)["script"] == ["pytest"]
yml = yaml.load(travis_yml, Loader=yaml.FullLoader)["jobs"]["include"]
assert yml[0]["script"] == ["flake8"]
if context.get("use_docker") == "y":
assert yml[1]["script"] == [
"docker-compose -f local.yml run django pytest"
]
else:
assert yml[1]["script"] == ["pytest"]
except yaml.YAMLError as e:
pytest.fail(e)

View File

@ -19,8 +19,19 @@ flake8:
pytest:
stage: test
image: python:3.7
{% if cookiecutter.use_docker == 'y' -%}
tags:
- docker
services:
- docker
before_script:
- docker-compose -f local.yml build
- docker-compose -f local.yml up -d
script:
- docker-compose -f local.yml run django pytest
{%- else %}
tags:
- python
services:
- postgres:11
variables:
@ -31,4 +42,5 @@ pytest:
script:
- pytest
{%- endif %}

View File

@ -1,17 +1,43 @@
dist: xenial
services:
- postgresql
before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq build-essential gettext python-dev zlib1g-dev libpq-dev xvfb
- sudo apt-get install -qq libjpeg8-dev libfreetype6-dev libwebp-dev
- sudo apt-get install -qq graphviz-dev python-setuptools python3-dev python-virtualenv python-pip
- sudo apt-get install -qq firefox automake libtool libreadline6 libreadline6-dev libreadline-dev
- sudo apt-get install -qq libsqlite3-dev libxml2 libxml2-dev libssl-dev libbz2-dev wget curl llvm
language: python
python:
- "3.7"
install:
- pip install -r requirements/local.txt
script:
- "pytest"
services:
- {% if cookiecutter.use_docker == 'y' %}docker{% else %}postgresql{% endif %}
jobs:
include:
- name: "Linter"
before_script:
- pip install -q flake8
script:
- "flake8"
- name: "Django Test"
{% if cookiecutter.use_docker == 'y' -%}
before_script:
- docker-compose -v
- docker -v
- docker-compose -f local.yml build
- docker-compose -f local.yml up -d
script:
- "docker-compose -f local.yml run django pytest"
after_failure:
- docker-compose -f local.yml logs
{%- else %}
before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq build-essential gettext python-dev zlib1g-dev libpq-dev xvfb
- sudo apt-get install -qq libjpeg8-dev libfreetype6-dev libwebp-dev
- sudo apt-get install -qq graphviz-dev python-setuptools python3-dev python-virtualenv python-pip
- sudo apt-get install -qq firefox automake libtool libreadline6 libreadline6-dev libreadline-dev
- sudo apt-get install -qq libsqlite3-dev libxml2 libxml2-dev libssl-dev libbz2-dev wget curl llvm
language: python
python:
- "3.8"
install:
- pip install -r requirements/local.txt
script:
- "pytest"
{%- endif %}

View File

@ -5,4 +5,41 @@ set -o nounset
rm -f './celerybeat.pid'
postgres_ready() {
python << END
import sys
from time import sleep
import psycopg2
try:
conn = psycopg2.connect(
dbname="${POSTGRES_DB}",
user="${POSTGRES_USER}",
password="${POSTGRES_PASSWORD}",
host="${POSTGRES_HOST}",
port="${POSTGRES_PORT}",
)
# Check if table exists yet.
# If not, wait for docker-compose up to migrate all tables.
cur = conn.cursor()
cur.execute(
"select exists(select * from ${POSTGRES_DB}.tables where table_name=%s)",
('django_celery_beat_periodictask',)
)
conn.close()
except psycopg2.OperationalError:
sys.exit(-1)
except psycopg2.errors.UndefinedTable:
conn.close()
sys.exit(-1)
sys.exit(0)
END
}
until postgres_ready; do
>&2 echo 'Waiting for celerybeat models to be migrated...'
sleep 1
done
>&2 echo 'PostgreSQL is ready'
celery -A config.celery_app beat -l INFO

View File

@ -4,5 +4,40 @@ set -o errexit
set -o pipefail
set -o nounset
postgres_ready() {
python << END
import sys
from time import sleep
import psycopg2
try:
conn = psycopg2.connect(
dbname="${POSTGRES_DB}",
user="${POSTGRES_USER}",
password="${POSTGRES_PASSWORD}",
host="${POSTGRES_HOST}",
port="${POSTGRES_PORT}",
)
# Check if table exists yet.
# If not, wait for docker-compose up to migrate all tables.
cur = conn.cursor()
cur.execute(
"select exists(select * from ${POSTGRES_DB}.tables where table_name=%s)",
('django_celery_beat_periodictask',)
)
conn.close()
except psycopg2.OperationalError:
sys.exit(-1)
except psycopg2.errors.UndefinedTable:
conn.close()
sys.exit(-1)
sys.exit(0)
END
}
until postgres_ready; do
>&2 echo 'Waiting for celerybeat models to be migrated...'
sleep 1
done
>&2 echo 'PostgreSQL is ready'
celery -A config.celery_app beat -l INFO