---
variables:
  DOCKER_IMAGE_TAG: ${YC_CONTAINER_REGISTRY}/${CI_PROJECT_NAME}:${CI_COMMIT_REF_SLUG}.${CI_COMMIT_SHORT_SHA}
  RELEASE_DOCKER_IMAGE_TAG: ${YC_CONTAINER_REGISTRY}/${CI_PROJECT_NAME}:${CI_COMMIT_TAG}
  DEPLOY_KUBER_NAMESPACE: spatial

stages:
  - build_django
  - build_static
  # - lint
  - deploy

build-django:
  stage: build_django
  tags:
    - shell
  script:
    - >
        docker build
        --build-arg YC_CONTAINER_REGISTRY=${YC_CONTAINER_REGISTRY}
        -f ./deploy/dockerfiles/Dockerfile
        -t ${DOCKER_IMAGE_TAG} .
    - docker push ${DOCKER_IMAGE_TAG}

build-static-image:
  stage: build_static
  tags:
    - shell
  script:
    - >
        docker build
        --build-arg YC_CONTAINER_REGISTRY=${YC_CONTAINER_REGISTRY}
        --build-arg DJANGO_DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG}
        -f ./deploy/dockerfiles/Dockerfile-nginx
        -t ${DOCKER_IMAGE_TAG}-static .
    - docker push ${DOCKER_IMAGE_TAG}-static

auto-deploy-dev-kuber:
  extends: .deploy_base_kuber
  variables:
    INGRESS_HOST: "postnet.dev.selftech.ru"
  tags:
    - docker
  only:
    refs:
      - dev
  environment:
    name: dev

deploy-dev-kuber:
  extends: .deploy_base_kuber
  variables:
    INGRESS_HOST: "postnet.dev.selftech.ru"
  tags:
    - docker
  except:
    - dev
  environment:
    name: dev
  when: manual

deploy-prod-kuber:
  extends: .deploy_base_kuber
  variables:
    INGRESS_HOST: "postnet.selftech.ru"
  tags:
    - docker-prod
  environment:
    name: prod
  when: manual

dev_restart_martin:
  extends: .restart_martin_base
  tags:
    - docker
  environment:
    name: dev

prod_restart_martin:
  extends: .restart_martin_base
  tags:
    - docker-prod
  environment:
    name: prod

.deploy_base_kuber:
  image: ${YC_CONTAINER_REGISTRY}/public/helm-kubectl-git:1.0.0
  stage: deploy
  before_script:
    - KUBE_CONFIG=`echo ${CI_ENVIRONMENT_NAME}_kubeconfig`
    - mkdir -p ${HOME}/.kube
    - 'cat ${!KUBE_CONFIG} > ${HOME}/.kube/config'
    - chmod -R 700 ${HOME}/.kube
  script:
    - sed -i "s|DEPLOY_IMAGE_TAG|${DOCKER_IMAGE_TAG}|g" ./deploy/django.yml
    - sed -i "s|ADDRESS_INGRESS_HOST|${INGRESS_HOST}|g" ./deploy/django.yml
    - sed -i "s|ADDRESS_INGRESS_HOST|${INGRESS_HOST}|g" ./deploy/django-static.yml
    - sed -i "s|DEPLOY_IMAGE_TAG|${DOCKER_IMAGE_TAG}|g" ./deploy/worker.yml
    - sed -i "s|DEPLOY_IMAGE_TAG|${DOCKER_IMAGE_TAG}|g" ./deploy/beat.yml
    - sed -i "s|DEPLOY_IMAGE_TAG|${DOCKER_IMAGE_TAG}-static|g" ./deploy/django-static.yml
    - kubectl apply -f ./deploy/django.yml
    - kubectl apply -f ./deploy/worker.yml
    - kubectl apply -f ./deploy/beat.yml
    - kubectl apply -f ./deploy/django-static.yml
  dependencies: []
  artifacts:
    paths:
      - ./deploy/django.yml
      - ./deploy/worker.yml
      - ./deploy/beat.yml
      - ./deploy/django-static.yml
    expire_in: 1 week

.restart_martin_base:
  image: ${YC_CONTAINER_REGISTRY}/public/helm-kubectl-git:1.0.0
  stage: .pre
  before_script:
    - KUBE_CONFIG=`echo ${CI_ENVIRONMENT_NAME}_kubeconfig`
    - mkdir -p ${HOME}/.kube
    - 'cat ${!KUBE_CONFIG} > ${HOME}/.kube/config'
    - chmod -R 700 ${HOME}/.kube
  script:
    - kubectl rollout restart deployment martin -n ${DEPLOY_KUBER_NAMESPACE}
  when: manual

# variables:
#   CONTAINERS_NAME: sst_postamates
#   IMAGE_NAME: $CI_REGISTRY_IMAGE

# default:
#   tags:
#     - deploy-remote

# .use_cached_requirements: &use_cached_requirements
#   variables:
#     PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
#   cache:
#     key: $CI_COMMIT_REF_SLUG
#     paths:
#       - .cache/pip
#     policy: pull-push

# build-job:
#   stage: build
#   before_script:
#     - sudo docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
#     - ls
#   script:
#     - sudo docker pull $IMAGE_NAME:latest || true
#     - sudo docker build --cache-from $IMAGE_NAME:latest --tag $IMAGE_NAME:$CI_COMMIT_SHORT_SHA --tag $IMAGE_NAME:latest .
#     - sudo docker push $IMAGE_NAME:$CI_COMMIT_SHORT_SHA
#     - sudo docker push $IMAGE_NAME:latest
#   rules:
#     - if: $CI_COMMIT_BRANCH == 'sst_main'

# .deploy: &deploy-common
#   stage: deploy
#   before_script:
#     - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
#     - eval $(ssh-agent -s)
#     - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
#     - mkdir -p ~/.ssh
#     - chmod 700 ~/.ssh
#     - ssh-keyscan $REMOTE_HOST >> ~/.ssh/known_hosts
#     - chmod 644 ~/.ssh/known_hosts
#     - ssh $REMOTE_USER docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
#   script:
#     - cd $CI_PROJECT_DIR
#     - ssh $REMOTE_USER docker pull $IMAGE_NAME:$CI_COMMIT_SHORT_SHA
#     - DOCKER_HOST="ssh://$REMOTE_USER" docker-compose -f $COMPOSE up -d
#     - DOCKER_HOST="ssh://$REMOTE_USER" docker restart sst_postamates_martin

# deploy-test-job:
#   <<: *deploy-common
#   variables:
#     REMOTE_HOST: $REMOTE_HOST_TEST
#     REMOTE_USER: $REMOTE_USER_TEST
#     COMPOSE: docker-compose.test.yml
#   rules:
#     - if: $CI_COMMIT_BRANCH == 'sst_main'

# pylint:
#   <<: *use_cached_requirements
#   stage: lint
#   before_script:
#     - pip install flake8-codeclimate
#   script:
#     - set -x
#     - FLIST=$(git --no-pager diff --diff-filter=d --name-only $(git merge-base --fork-point origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME) $CI_COMMIT_SHA  | egrep ".py$" | grep -v ".docker/"|| true)
#     - >
#       if [ ! -z "$FLIST" ];
#       then
#         mkdir -p build/reports;
#         flake8 --format junit-xml $FLIST > build/reports/flake8-junit.xml || flake8 $FLIST;
#       else
#         echo "No files to lint"
#         true;
#       fi
#   only:
#     refs:
#       - merge_requests
#   artifacts:
#     reports:
#       junit: ./build/reports/flake8-junit.xml
#     expire_in: 7 days
#   allow_failure: false
