variables:
  DOCKER_TLS_CERTDIR: ""

stages:
  - build


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_1=`grep 'version="*"' core/Dockerfile | cut -d '"' -f2`
    - IMAGE_1="$CI_REGISTRY_IMAGE/pvs_core"
    - docker pull "$IMAGE_1":latest || true
    - docker build --cache-from "$IMAGE_1":latest -t "$IMAGE_1":latest -t "$IMAGE_1":$VERSION_1 core/
    - VERSION_2=`grep 'version="*"' preprocessor/Dockerfile | cut -d '"' -f2`
    - IMAGE_2="$CI_REGISTRY_IMAGE/pvs_preprocessor"
    - docker pull "$IMAGE_2":latest || true
    - docker build --cache-from "$IMAGE_2":latest -t "$IMAGE_2":latest -t "$IMAGE_2":$VERSION_2 preprocessor/
    - VERSION_3=`grep 'version="*"' client/Dockerfile | cut -d '"' -f2`
    - IMAGE_3="$CI_REGISTRY_IMAGE/pvs_client"
    - docker pull "$IMAGE_3":latest || true
    - docker build --cache-from "$IMAGE_3":latest -t "$IMAGE_3":latest -t "$IMAGE_3":$VERSION_3 client/
    - VERSION_4=`grep 'version="*"' cache/Dockerfile | cut -d '"' -f2`
    - IMAGE_4="$CI_REGISTRY_IMAGE/pvs_cache"
    - docker pull "$IMAGE_4":latest || true
    - docker build --cache-from "$IMAGE_4":latest -t "$IMAGE_4":latest -t "$IMAGE_4":$VERSION_4 cache/
    - chmod +x gitlab_test.sh
    - ./gitlab_test.sh
    - docker push "$IMAGE_1":$VERSION_1
    - docker push "$IMAGE_1":latest
    - docker push "$IMAGE_2":$VERSION_2
    - docker push "$IMAGE_2":latest
    - docker push "$IMAGE_3":$VERSION_3
    - docker push "$IMAGE_3":latest
    - docker push "$IMAGE_4":$VERSION_4
    - docker push "$IMAGE_4":latest
    - chmod +x gitlab_test.sh
    - ./gitlab_test.sh
  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 tag "$IMAGE:$CI_COMMIT_REF_NAME" "$IMAGE:latest"
    - 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 tag "$IMAGE:$CI_COMMIT_REF_NAME" "$IMAGE:latest"
    - 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 tag "$IMAGE:$CI_COMMIT_REF_NAME" "$IMAGE:latest"
    - 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 tag "$IMAGE:$CI_COMMIT_REF_NAME" "$IMAGE:latest"
    - chmod +x gitlab_test.sh
    - ./gitlab_test.sh
    
