stages:
  - test
  - build
  - deploy

test_web:
  stage: test
  image: frolvlad/alpine-python3
  services:
    - mdillon/postgis
  tags:
    - test
  except:
    - develop
  variables:
    POSTGRES_USER: "postgres"
    POSTGRES_DB: "homini"
    POSTGRES_PASSWORD: "temp1234"
    DEBUG: "on"
    DATABASE_URL: "postgis://postgres:temp1234@mdillon__postgis:5432/homini"
  before_script:
    - mv ./image/repositories /etc/apk/repositories
    - apk update && apk add postgresql-dev gcc python3-dev musl-dev py-pillow libgcc jpeg-dev freetype-dev zlib-dev openjpeg-dev tiff-dev tk-dev tcl-dev g++
    - apk add --no-cache --virtual .crypto-rundeps --repository http://dl-cdn.alpinelinux.org/alpine/edge/main libressl2.7-libcrypto
    - apk add gdal@testing gdal-dev@testing
    - pip3 install -r requirements.txt
    - cd src
  script:
    - coverage run --source='.' --omit='*/admin.py' manage.py test
    - coverage report -m
  coverage: '/TOTAL.+ ([0-9]{1,3}%)/'


build_develop_web:
  stage: build
  tags:
    - build
  only:
    - develop
  variables:
    BUILD_REF: $CI_BUILD_REF
    PIPELINE_ID: $CI_PIPELINE_ID
  before_script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.recoded.co
  script:
    - docker build -t registry.recoded.co/homini/web -f Dockerfile.release .
    - docker push registry.recoded.co/homini/web

deploy_develop_web:
  stage: deploy
  tags:
    - prod
  only:
    - develop
  except:
    - schedules
  environment: develop
  variables:
    DEBUG: "on"
    SECRET_KEY: "%zhtwec#e&1x=@*vgl&@)tj1x(8kf4)w3g#c)tj#vk*x&f^z@0"
    DATABASE_URL: "postgis://postgres:temp1234@db:5432/homini"
    NOTIFICATIONS: "prod"
  before_script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.recoded.co
  script:
    - docker pull registry.recoded.co/homini/web
    - docker stop homini.recoded.co || true
    - docker rm homini.recoded.co || true
    - docker run --name homini.recoded.co -d -t -v /srv/homini/media:/app/src/media -e VIRTUAL_HOST="homini.recoded.co" -e DEBUG=$DEBUG -e DATABASE_URL=$DATABASE_URL -e NOTIFICATIONS=$NOTIFICATIONS --link postgis:db registry.recoded.co/homini/web
  after_script:
    - docker exec homini.recoded.co python3 manage.py migrate
