# GitLab CI for limechat-mbr-generator.
#
# Hard rule for this repo: all runtime env vars live in k8s/configmap.yaml.
# This pipeline does not patch env vars via Secret objects or recipe scripts.

stages:
  - build-prod
  - deploy-prod

variables:
  IMAGE: acrlcpub.azurecr.io/limechat-mbr-generator:prod
  NAMESPACE: limekit-prod

before_script:
  - docker info
  - |
    if [ -f .env.gitlabCI ]; then
      export $(grep -v '^\s*#' .env.gitlabCI | grep -v '^\s*$' | xargs)
    fi
  - echo $DOCKERHUB_TOKEN | docker login -u $DOCKERHUB_USER --password-stdin

build_prod:
  stage: build-prod
  tags:
    - awsgitrunner
  script:
    - docker build -f Dockerfile -t "$IMAGE" .
    - az acr login --name acrlcpub
    - docker push "$IMAGE"
  only:
    - main

deploy_prod:
  stage: deploy-prod
  tags:
    - awsgitrunner
  before_script:
    - kubectl config use-context lc-prod-ci-aks
    - kubectl cluster-info
  script:
    - echo "Kube Apply [Prod]"
    - kubectl -n "$NAMESPACE" apply -f k8s/configmap.yaml
    - kubectl -n "$NAMESPACE" apply -f k8s/deployment.yaml
    - kubectl -n "$NAMESPACE" apply -f k8s/service.yaml
    - kubectl -n "$NAMESPACE" apply -f k8s/ingress.yaml
    - echo "Kube Restart [Prod]"
    - kubectl -n "$NAMESPACE" rollout restart deployment limechat-mbr-generator
    - kubectl -n "$NAMESPACE" rollout status deployment limechat-mbr-generator --timeout=5m
  only:
    - main
