From 386e7d1bf88f67ef15661681aa97c44a1f59d9b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Tue, 22 Oct 2019 15:39:51 +0300 Subject: [PATCH] First CI --- compose_ci.yml | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ fabfile.py | 75 ++++++++++++++++++++++++++++++++++++ gitlab-ci.yml | 60 +++++++++++++++++++++++++++++ 3 files changed, 236 insertions(+) create mode 100644 compose_ci.yml create mode 100644 fabfile.py create mode 100644 gitlab-ci.yml diff --git a/compose_ci.yml b/compose_ci.yml new file mode 100644 index 00000000..3c41f200 --- /dev/null +++ b/compose_ci.yml @@ -0,0 +1,101 @@ +version: '3.5' +services: + # PostgreSQL database + db: + build: + context: ./_dockerfiles/db + dockerfile: Dockerfile + hostname: db + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=postgres + ports: + - "5436:5432" + volumes: + - gm-db:/var/lib/postgresql/data/ + + elasticsearch: + image: elasticsearch:7.3.1 + volumes: + - gm-esdata:/usr/share/elasticsearch/data + hostname: elasticsearch + ports: + - 9200:9200 + - 9300:9300 + environment: + - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + - discovery.type=single-node + - xpack.security.enabled=false + + # Redis + redis: + image: redis:2.8.23 + ports: + - "6379:6379" + + # Celery + worker: + build: . + command: ./run_celery.sh + environment: + - SETTINGS_CONFIGURATION=local + - DB_NAME=postgres + - DB_USERNAME=postgres + - DB_HOSTNAME=db + - DB_PORT=5432 + - DB_PASSWORD=postgres + volumes: + - .:/code + links: + - db + - redis + + worker_beat: + build: . + command: ./run_celery_beat.sh + environment: + - SETTINGS_CONFIGURATION=local + - DB_NAME=postgres + - DB_USERNAME=postgres + - DB_HOSTNAME=db + - DB_PORT=5432 + - DB_PASSWORD=postgres + volumes: + - .:/code + links: + - db + - redis + + + # App: G&M + gm_app: + build: . + command: python manage.py runserver 0.0.0.0:8000 + environment: + - SETTINGS_CONFIGURATION=local + - DB_HOSTNAME=db + - DB_PORT=5432 + - DB_NAME=postgres + - DB_USERNAME=postgres + - DB_PASSWORD=postgres + depends_on: + - db + - redis + - worker + - worker_beat + - elasticsearch + volumes: + - .:/code + - gm-media:/media-data + ports: + - "8000:8000" + +volumes: + gm-db: + name: gm-db + + gm-media: + name: gm-media + + gm-esdata: \ No newline at end of file diff --git a/fabfile.py b/fabfile.py new file mode 100644 index 00000000..643080be --- /dev/null +++ b/fabfile.py @@ -0,0 +1,75 @@ +import os # NOQA +from fabric.api import * # NOQA + +user = 'gm' + +env.root = '~/' +env.src = '~/project' + +env.default_branch = 'feature/develop_ci' +env.tmpdir = '~/tmp' + + +env.roledefs = { + 'develop': { + 'branch': env.default_branch, + 'hosts': ['%s@rock.spider.ru:31' % user, ] + }, + 'staging': { + 'hosts': ['%s@5.200.53.99' % user, ] + }, + 'production': { + 'branch': 'master', + 'hosts': ['%s@87.226.166.80' % user, ] + } +} + + +def fetch(branch=None): + with cd(env.src): + role = env.roles[0] + run('git pull origin {}'.format(env.roledefs[role]['branch'])) + run('git submodule update') + + +def migrate(): + with cd(env.src): + run('./manage.py migrate') + + +def install_requirements(): + with cd(env.src): + run('pip install -r requirements/base.txt') + + +def touch(): + with cd(env.src): + run('touch ~/%s.touch' % user) + + +def kill_celery(): + """Kill celery workers for $user.""" + with cd(env.src): + run('ps -u %s -o pid,fname | grep celery | (while read a b; do kill -9 $a; done;)' % user) + + +def collectstatic(): + with cd(env.src): + run('./manage.py collectstatic --noinput') + + +def deploy(branch=None): + fetch() + install_requirements() + migrate() + collectstatic() + touch() + kill_celery() + + +def rev(): + """Show head commit.""" + with hide('running', 'stdout'): + with cd(env.src): + commit = run('git rev-parse HEAD') + return local('git show -q %s' % commit) diff --git a/gitlab-ci.yml b/gitlab-ci.yml new file mode 100644 index 00000000..0de48055 --- /dev/null +++ b/gitlab-ci.yml @@ -0,0 +1,60 @@ +image: docker:latest + +stages: + - hello + +#stages: +# - build +# - test +# - deploy +# - clean +# + +before_script: + - apk add --update python python-dev py-pip gcc libc-dev libffi-dev openssl-dev make + - pip install docker-compose + + +hello: + - echo 'Test GitLab CI' + +#clean: +# stage: clean +# script: +# - docker-compose -f compose-ci.yml stop +# - docker-compose -f compose-ci.yml rm --force gm_app +# when: always +# +# +#buid: +# stage: build +# script: +# - docker-compose -f compose-ci.yml build gm_app +# when: always +# +# +#test: +# stage: test +# script: +# - docker-compose -f compose-ci.yml run agro python manage.py test -v 3 --noinput +# when: always +# +# +#deploy-develop: +# stage: deploy +# only: +# - feature/develop_ci +# script: +# - fab --roles=develop deploy +# environment: +# name: Develop +# +# +##deploy-staging: +## stage: deploy +## only: +## - master +## script: +## - fab --roles=staging deploy +## environment: +## name: Staging