variables:
  DOCKER_TLS_CERTDIR: ""

stages:
  - build

build-tag:
  image: registry.gitlab.eox.at/esa/prism/vs/docker-base-testing:latest
  stage: build
  services:
    - docker:19.03.13-dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - IMAGE_1="$CI_REGISTRY_IMAGE/pvs_core"
    - docker pull "$IMAGE_1":latest || true
    - docker build --cache-from "$IMAGE_1":latest -t "$IMAGE_1":dev -t "$IMAGE_1":$CI_COMMIT_TAG core/
    - IMAGE_2="$CI_REGISTRY_IMAGE/pvs_preprocessor"
    - docker pull "$IMAGE_2":latest || true
    - docker build --cache-from "$IMAGE_2":latest -t "$IMAGE_2":dev -t "$IMAGE_2":$CI_COMMIT_TAG preprocessor/
    - IMAGE_3="$CI_REGISTRY_IMAGE/pvs_client"
    - docker pull "$IMAGE_3":latest || true
    - docker build --cache-from "$IMAGE_3":latest -t "$IMAGE_3":dev -t "$IMAGE_3":$CI_COMMIT_TAG client/
    - IMAGE_4="$CI_REGISTRY_IMAGE/pvs_cache"
    - docker pull "$IMAGE_4":latest || true
    - docker build --cache-from "$IMAGE_4":latest -t "$IMAGE_4":dev -t "$IMAGE_4":$CI_COMMIT_TAG cache/
    - IMAGE_5="$CI_REGISTRY_IMAGE/fluentd"
    - docker pull "$IMAGE_5":latest || true
    - docker build --cache-from "$IMAGE_5":latest -t "$IMAGE_5":dev -t "$IMAGE_5":$CI_COMMIT_TAG fluentd/
    - IMAGE_6="$CI_REGISTRY_IMAGE/pvs_ingestor"
    - docker pull "$IMAGE_6":latest || true
    - docker build --cache-from "$IMAGE_6":latest -t "$IMAGE_6":dev -t "$IMAGE_6":$CI_COMMIT_TAG ingestor/
    - IMAGE_7="$CI_REGISTRY_IMAGE/pvs_sftp"
    - docker pull "$IMAGE_7":latest || true
    - docker build --cache-from "$IMAGE_7":latest -t "$IMAGE_7":dev -t "$IMAGE_7":$CI_COMMIT_TAG sftp/
    - cd ./testing && ./gitlab_test.sh
    - if [ $? -ne 0 ]; then exit 1; fi  # actually fail build
    - docker push "$IMAGE_1":$CI_COMMIT_TAG
    - docker push "$IMAGE_2":$CI_COMMIT_TAG
    - docker push "$IMAGE_3":$CI_COMMIT_TAG
    - docker push "$IMAGE_4":$CI_COMMIT_TAG
    - docker push "$IMAGE_5":$CI_COMMIT_TAG
    - docker push "$IMAGE_6":$CI_COMMIT_TAG
    - docker push "$IMAGE_7":$CI_COMMIT_TAG
  only:
    - tags
build-master-staging:
  image: registry.gitlab.eox.at/esa/prism/vs/docker-base-testing:latest
  stage: build
  services:
    - docker:19.03.13-dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - if [[ "$CI_COMMIT_BRANCH" = "master" ]] ; then TAG_USED="latest"; else TAG_USED="staging"; fi
    - IMAGE_1="$CI_REGISTRY_IMAGE/pvs_core"
    - docker pull "$IMAGE_1":"$TAG_USED" || true
    - docker build --cache-from "$IMAGE_1":"$TAG_USED" -t "$IMAGE_1":dev -t "$IMAGE_1":"$TAG_USED" core/
    - IMAGE_2="$CI_REGISTRY_IMAGE/pvs_preprocessor"
    - docker pull "$IMAGE_2":"$TAG_USED" || true
    - docker build --cache-from "$IMAGE_2":"$TAG_USED" -t "$IMAGE_2":dev -t "$IMAGE_2":"$TAG_USED" preprocessor/
    - IMAGE_3="$CI_REGISTRY_IMAGE/pvs_client"
    - docker pull "$IMAGE_3":"$TAG_USED" || true
    - docker build --cache-from "$IMAGE_3":"$TAG_USED" -t "$IMAGE_3":dev -t "$IMAGE_3":"$TAG_USED" client/
    - IMAGE_4="$CI_REGISTRY_IMAGE/pvs_cache"
    - docker pull "$IMAGE_4":"$TAG_USED" || true
    - docker build --cache-from "$IMAGE_4":"$TAG_USED" -t "$IMAGE_4":dev -t "$IMAGE_4":"$TAG_USED" cache/
    - IMAGE_5="$CI_REGISTRY_IMAGE/fluentd"
    - docker pull "$IMAGE_5":"$TAG_USED" || true
    - docker build --cache-from "$IMAGE_5":"$TAG_USED" -t "$IMAGE_5":dev -t "$IMAGE_5":"$TAG_USED" fluentd/
    - IMAGE_6="$CI_REGISTRY_IMAGE/pvs_ingestor"
    - docker pull "$IMAGE_6":"$TAG_USED" || true
    - docker build --cache-from "$IMAGE_6":"$TAG_USED" -t "$IMAGE_6":dev -t "$IMAGE_6":"$TAG_USED" ingestor/
    - IMAGE_7="$CI_REGISTRY_IMAGE/pvs_sftp"
    - docker pull "$IMAGE_7":"$TAG_USED" || true
    - docker build --cache-from "$IMAGE_7":latest -t "$IMAGE_7":dev -t "$IMAGE_7":"$TAG_USED" sftp/
    - cd ./testing && ./gitlab_test.sh
    - if [ $? -ne 0 ]; then exit 1; fi  # actually fail build
    - docker push "$IMAGE_1":"$TAG_USED"
    - docker push "$IMAGE_2":"$TAG_USED"
    - docker push "$IMAGE_3":"$TAG_USED"
    - docker push "$IMAGE_4":"$TAG_USED"
    - docker push "$IMAGE_5":"$TAG_USED"
    - docker push "$IMAGE_6":"$TAG_USED"
    - docker push "$IMAGE_7":"$TAG_USED"
  only:
    - staging
    - master
  except:
    - tags
  artifacts:
    when: always
    expire_in: 4 week
    paths:
      - emg-pvs_database.txt
      - emg-pvs_preprocessor.txt
      - emg-pvs_registrar.txt
      - emg-pvs_renderer.txt
build:
  image: registry.gitlab.eox.at/esa/prism/vs/docker-base-testing:latest
  stage: build
  services:
    - docker:19.03.13-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":staging || true
    - docker build --cache-from "$IMAGE":staging -t "$IMAGE":dev core/
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_preprocessor"
    - docker pull "$IMAGE":staging || true
    - docker build --cache-from "$IMAGE":staging -t "$IMAGE":dev preprocessor/
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_client"
    - docker pull "$IMAGE":staging || true
    - docker build --cache-from "$IMAGE":staging -t "$IMAGE":dev client/
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_cache"
    - docker pull "$IMAGE":staging || true
    - docker build --cache-from "$IMAGE":staging -t "$IMAGE":dev cache/
    - IMAGE="$CI_REGISTRY_IMAGE/fluentd"
    - docker pull "$IMAGE":staging || true
    - docker build --cache-from "$IMAGE":staging -t "$IMAGE":dev fluentd/
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_ingestor"
    - docker pull "$IMAGE":staging || true
    - docker build --cache-from "$IMAGE":staging -t "$IMAGE":dev ingestor/
    - IMAGE="$CI_REGISTRY_IMAGE/pvs_sftp"
    - docker pull "$IMAGE":staging || true
    - docker build --cache-from "$IMAGE":staging -t "$IMAGE":dev sftp/
    - cd ./testing && ./gitlab_test.sh
    - if [ $? -ne 0 ]; then exit 1; fi  # actually fail build
  except:
    - tags
    - staging
    - master
  artifacts:
    when: always
    expire_in: 4 week
    paths:
      # - emg-pvs_cache.txt
      # - emg-pvs_client.txt
      - emg-pvs_database.txt
      # - emg-pvs_fluentd.txt
      # - emg-pvs_ingestor.txt
      - emg-pvs_preprocessor.txt
      # - emg-pvs_redis.txt
      - emg-pvs_registrar.txt
      - emg-pvs_renderer.txt
      # - emg-pvs_seeder.txt
      # - emg-pvs_sftp.txt
