# Author , IjazAhmad
# iahmad@ebi.ac.uk
image: docker:latest

stages:
  - build
  - deploy

services:
  - docker:dind
  
variables:
  CI_REGISTRY: dockerhub.ebi.ac.uk
  CI_REGISTRY_IMAGE: $CI_REGISTRY/$CI_PROJECT_PATH
  DOCKER_DRIVER: overlay2

before_script:
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY  
build-push:
  stage: build
  script:
     - docker pull $CI_REGISTRY_IMAGE:latest || true
     - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest .
     - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
     - docker push $CI_REGISTRY_IMAGE:latest

deploy:
  stage: deploy
  script:
     - sed -i "s/COMMIT_SHA/$CI_COMMIT_SHA/g" k8s-configs/deployment.yaml
     - kubectl apply --namespace webproduction-prod -f k8s-configs/

  only:
    - master
