variables:
  DOCKER_TLS_CERTDIR: ""

stages:
  - build
  - test
  - publish


build-master:
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - VERSION=`grep 'version="*"' core/Dockerfile | cut -d '"' -f2`
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_core"
    - docker pull "$IMAGE":latest || true
    - docker build --cache-from "$IMAGE":latest -t "$IMAGE":latest -t "$IMAGE":$VERSION core/
    - docker push "$IMAGE":$VERSION
    - docker push "$IMAGE":latest
    - VERSION=`grep 'version="*"' preprocessor/Dockerfile | cut -d '"' -f2`
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_preprocessor"
    - docker pull "$IMAGE":latest || true
    - docker build --cache-from "$IMAGE":latest -t "$IMAGE":latest -t "$IMAGE":$VERSION preprocessor/
    - docker push "$IMAGE":$VERSION
    - docker push "$IMAGE":latest
    - VERSION=`grep 'version="*"' client/Dockerfile | cut -d '"' -f2`
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_client"
    - docker pull "$IMAGE":latest || true
    - docker build --cache-from "$IMAGE":latest -t "$IMAGE":latest -t "$IMAGE":$VERSION client/
    - docker push "$IMAGE":$VERSION
    - docker push "$IMAGE":latest
    - VERSION=`grep 'version="*"' cache/Dockerfile | cut -d '"' -f2`
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_cache"
    - docker pull "$IMAGE":latest || true
    - docker build --cache-from "$IMAGE":latest -t "$IMAGE":latest -t "$IMAGE":$VERSION cache/
    - docker push "$IMAGE":$VERSION
    - docker push "$IMAGE":latest
  only:
    - master

build:
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_core"
    - docker pull "$IMAGE":$CI_COMMIT_REF_NAME || true
    - docker build --cache-from "$IMAGE":$CI_COMMIT_REF_NAME -t "$IMAGE":$CI_COMMIT_REF_SLUG -t "$IMAGE":$CI_COMMIT_REF_NAME core/
    - docker push "$IMAGE:$CI_COMMIT_REF_SLUG"
    - docker push "$IMAGE:$CI_COMMIT_REF_NAME"
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_preprocessor"
    - docker pull "$IMAGE":$CI_COMMIT_REF_NAME || true
    - docker build --cache-from "$IMAGE":$CI_COMMIT_REF_NAME -t "$IMAGE":$CI_COMMIT_REF_SLUG -t "$IMAGE":$CI_COMMIT_REF_NAME preprocessor/
    - docker push "$IMAGE:$CI_COMMIT_REF_SLUG"
    - docker push "$IMAGE:$CI_COMMIT_REF_NAME"
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_client"
    - docker pull "$IMAGE":$CI_COMMIT_REF_NAME || true
    - docker build --cache-from "$IMAGE":$CI_COMMIT_REF_NAME -t "$IMAGE":$CI_COMMIT_REF_SLUG -t "$IMAGE":$CI_COMMIT_REF_NAME client/
    - docker push "$IMAGE:$CI_COMMIT_REF_SLUG"
    - docker push "$IMAGE:$CI_COMMIT_REF_NAME"
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_cache"
    - docker pull "$IMAGE":$CI_COMMIT_REF_NAME || true
    - docker build --cache-from "$IMAGE":$CI_COMMIT_REF_NAME -t "$IMAGE":$CI_COMMIT_REF_SLUG -t "$IMAGE":$CI_COMMIT_REF_NAME cache/
    - docker push "$IMAGE:$CI_COMMIT_REF_SLUG"
    - docker push "$IMAGE:$CI_COMMIT_REF_NAME"
  except:
    - master
test:
  image: docker:latest
  stage: test
  variables:
    BUCKET_NAME: pvs_testing
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - apk add --update py-pip
    - pip install -r requirements.txt
    - pytest
    - chmod +x env_setup.sh wait_for_container.sh
    - ./env_setup.sh
    - docker swarm init
    - docker stack deploy -c docker-compose.vhr18.yml -c docker-compose.vhr18.dev.yml vhr18-pvs
    - ./wait_for_container.sh
    - docker ps
    - docker ps -qf "name=vhr18-pvs_renderer"
    # - docker exec -it $(docker ps -qf "name=vhr18-pvs_renderer") python3 /var/www/pvs/dev/pvs_instance/manage.py id list
  # after_script:
  #   - docker stack rm vhr18-pvs
  #   - docker volume rm vhr18-pvs_db-data
  #   - docker volume rm vhr18-pvs_redis-data
  #   - docker volume rm vhr18-pvs_traefik-data
  #   - docker volume rm vhr18-pvs_cache-db