---
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
  - deploy

build-docker-dev:
  stage: build
  tags:
    - shell
  script:
    - >
        docker build
        --build-arg YC_CONTAINER_REGISTRY=${YC_CONTAINER_REGISTRY}
        --build-arg VITE_API_URL="https://postnet.dev.selftech.ru"
        --build-arg VITE_KEYCLOAK_CLIENT_ID="postnet"
        --build-arg VITE_KEYCLOAK_CLIENT_SECRET=${VITE_KEYCLOAK_CLIENT_SECRET}
        --build-arg VITE_KEYCLOAK_URL="https://kk.dev.selftech.ru/"
        -t ${DOCKER_IMAGE_TAG}-dev .
    - docker push ${DOCKER_IMAGE_TAG}-dev
  environment:
    name: dev

build-docker-prod:
  stage: build
  tags:
    - shell
  script:
    - >
        docker build
        --build-arg YC_CONTAINER_REGISTRY=${YC_CONTAINER_REGISTRY}
        --build-arg VITE_API_URL="https://postnet.selftech.ru"
        --build-arg VITE_KEYCLOAK_CLIENT_ID="postnet"
        --build-arg VITE_KEYCLOAK_CLIENT_SECRET=${VITE_KEYCLOAK_CLIENT_SECRET}
        --build-arg VITE_KEYCLOAK_URL="https://auth.selftech.ru/"
        -t ${DOCKER_IMAGE_TAG}-prod .
    - docker push ${DOCKER_IMAGE_TAG}-prod
  environment:
    name: prod

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

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

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

.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|${DEPLOY_DOCKER_IMAGE}|g" ./deploy/front.yaml
    - sed -i "s|ADDRESS_INGRESS_HOST|${INGRESS_HOST}|g" ./deploy/front.yaml
    - kubectl apply -f ./deploy/front.yaml
  dependencies: []
  artifacts:
    paths:
      - ./deploy/front.yaml
    expire_in: 1 week

# variables:
#   APP_NAME: postamates_front
#   IMAGE_NAME: $CI_REGISTRY_IMAGE

# stages:
#   - build
#   - deploy

# .build: &build-common
#   stage: build
#   before_script:
#     - sudo docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
#   script:
#     - sudo docker pull $IMAGE_NAME:builder || true
#     - sudo docker pull $IMAGE_NAME:$CI_COMMIT_SHORT_SHA || true
#     - sudo docker build --cache-from $IMAGE_NAME:builder --memory=2000m --memory-swap=3000m --target builder --tag $IMAGE_NAME:builder .
#     - sudo docker push $IMAGE_NAME:builder
#     - sudo docker build --cache-from $IMAGE_NAME:builder --memory=2000m --memory-swap=3000m --tag $IMAGE_NAME:$IMAGE_TAG --build-arg REACT_APP_DOMAIN_URL=$REACT_APP_DOMAIN_URL --build-arg BUILDKIT_INLINE_CACHE=1 .
#     - sudo docker push $IMAGE_NAME:$IMAGE_TAG
#   tags:
#     - deploy-remote

# build-test-job:
#   <<:  *build-common
#   variables: 
#     IMAGE_TAG: $CI_COMMIT_SHORT_SHA
#     REACT_APP_DOMAIN_URL: https://$DOMAIN/
#   rules:
#     - if: $CI_COMMIT_BRANCH == "sst_main"

# .deploy: &deploy-common
#   stage: deploy
#   before_script:
#     - sudo docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
#     - sudo docker pull $IMAGE_NAME:$IMAGE_TAG
#   script:
#     - id=$(sudo docker create $IMAGE_NAME:$IMAGE_TAG)
#     - sudo docker cp $id:/dist/. /home/toren332/sst_postamates_frontend/dist
#     - sudo docker rm -v $id
#   tags:
#     - deploy-remote

# deploy-test-job:
#   <<: *deploy-common
#   variables:
#     IMAGE_TAG: $CI_COMMIT_SHORT_SHA
#   rules:
#     - if: $CI_COMMIT_BRANCH == "sst_main"
