image: python:latest

services:
  - postgres:latest

variables:
  POSTGRES_DB: kszkepzes
  CONTAINER_IMAGE: 'registry.kszk.bme.hu/kszk/devteam/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME'

cache:
  key: '$CI_JOB_NAME-$CI_COMMIT_REF_SLUG'
  paths:
    - .cache/pip
    - env/

before_script:
  - python -V
  - python3 -m venv env
  - source env/bin/activate
  - pip install -r requirements/production.txt

stages:
  - Build
  - Docker Build

Flake8:
  stage: Build
  only:
    - dev
    - master
  tags: [docker]
  script:
    - flake8 --max-line-length=125 --exclude=src/kszkepzes,migrations,env

Test:
  stage: Build
  only:
    - dev
    - master
  tags: [docker]
  variables:
    DATABASE_URL: 'postgresql://postgres:postgres@postgres:5432/$POSTGRES_DB'
    SECRET_KEY: 'secret'
    AUTHSCH_KEY: 'secret'
    AUTHSCH_SECRET: 'secret'
  script:
    - python src/manage.py test

Docker build:
  stage: Docker build
  tags: [docker]
  only:
    - tags
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: ['']
  script:
    - echo "{\"auths\":{\"registry.kszk.bme.hu\":{\"username\":\"$REGISTRY_USER\",\"password\":\"$REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CONTAINER_IMAGE
