stages:
  - build
  - staging

build_image:
  stage: build
  image: docker:20.10.9
  services:
    - docker:20.10.9-dind
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
  only:
    - main

deploy_to_staging:
  stage: staging
  image:
    name: bitnami/kubectl
    entrypoint: [""]
  script:
    - echo "$KUBECONFIG_BASE64" | base64 -d > ./kubeconfig
    - export KUBECONFIG=$(pwd)/kubeconfig
    - sed -i "s/<VERSION>/$CI_COMMIT_SHORT_SHA/" k8s/staging/deployment.yaml
    - kubectl apply -f k8s/staging/namespace.yaml
    - kubectl apply -f k8s/staging/deployment.yaml
    - kubectl apply -f k8s/staging/service.yaml
    - kubectl apply -f k8s/staging/ingress.yaml
  only:
    - main
