stages:
  - build_and_push
  - redeploy

variables:
  DOCKER_BUILDKIT: 1
  COMPOSE_DOCKER_CLI_BUILD: 1

build_and_push:
  image: docker/compose:latest
  stage: build_and_push
  services:
  - docker:dind
  script:
  - docker login docker.io -u "$DOCKER_REGISTRY_USER" -p "$DOCKER_REGISTRY_PASSWORD"
  - echo We download previous build for using as cache from
  - docker-compose pull
  - docker-compose build
  - docker-compose push
  tags:
  - docker
  only:
  - kubernetes

redeploy_at_k8s:
  image: alpine/k8s:1.21.5
  stage: redeploy
  script:
  - kubectl config set-cluster local --server="${K8S_SERVER}"
  - kubectl config set clusters.local.certificate-authority-data "${K8S_CERTIFICATE_AUTHORITY_DATA}"
  - kubectl config set-credentials local --token="${K8S_USER_TOKEN}"
  - kubectl config set-context local --cluster=local --user=local
  - kubectl config use-context local
  - kubectl --insecure-skip-tls-verify version
  - kubectl get deployments ujse -n ujse-dev --insecure-skip-tls-verify
  - kubectl rollout restart deployment ujse -n ujse-dev --insecure-skip-tls-verify
  tags:
  - docker
  only:
  - kubernetes
  when: manual
