variables:
  DOCKER_TLS_CERTDIR: "" # HACK: see https://gitlab.com/gitlab-org/gitlab-runner/issues/4501

stages:
  - build
  - deploy

#==[ Backend build ]============================================================

# TODO: cache docker layers
# TODO: build ts
be-build-job:
  image: docker:latest
  stage: build
  tags:
    - commul
  services:
    - docker:dind
  variables:
    IMAGE_NAME: $CI_REGISTRY_IMAGE/backend
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN https://$CI_REGISTRY
  script:
    - >
      docker build
      --rm --no-cache
      --target production
      --tag=$IMAGE_NAME:${CI_COMMIT_SHA}
      --tag=$IMAGE_NAME:${CI_COMMIT_REF_NAME}
      backend
    - docker image push --all-tags $IMAGE_NAME
  only:
    - main # TODO: tags
    - development

#==[ Backend deployment ]=======================================================

.be-deploy-job:
  image: docker:latest
  stage: deploy
  needs:
    - job: be-build-job
      artifacts: false # TODO: maybe we can get the build image as an artifact?
  tags:
    - commul
  services:
    - docker:dind
  variables:
    KUBECTL_VERSION: v1.8.10
    IMAGE_NAME: $CI_REGISTRY_IMAGE/backend
  before_script:
    - wget https://storage.googleapis.com/kubernetes-release/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl
    # NOTE: version is too ald and don't have sha256 available
    #- wget https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl.sha256
    # - echo "$(<kubectl.sha256)  kubectl" | sha256sum --check
    - mv kubectl /usr/bin/ && chmod +x /usr/bin/kubectl
    - docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN https://$CI_REGISTRY
  script:
    - kubectl version --client
    - docker pull ${IMAGE_NAME}:${CI_COMMIT_SHA}
    #- kubectl --namespace $K8S_NAMESPACE set image deployment/$K8S_DEPLOYMENT webserver=${IMAGE_NAME}:${CI_COMMIT_SHA}

be-prd-deploy-job:
  extends: .be-deploy-job
  environment:
    name: production/backend
    deployment_tier: production
    url: https://kommul.eurac.edu/oetzi
  variables:
    K8S_NAMESPACE: kommul
    K8S_DEPLOYMENT: oetzi-prd-deployment
  only: # TODO: only on tag
    - main

be-stg-deploy-job:
  extends: .be-deploy-job
  environment:
    name: staging/backend
    deployment_tier: staging
    url: https://kommul-dev.eurac.edu/oetzi
  variables:
    K8S_NAMESPACE: kommul-dev
    K8S_DEPLOYMENT: oetzi-stg-deployment
  only:
    - development

#==[ Frontend build ]===========================================================

fe-build-job:
  image: node:16-alpine
  stage: build
  tags:
    - commul
  cache:
    - key:
        files:
          - frontend/package-lock.json
      paths:
        - frontend/node_modules
  script:
    - cd frontend
    - npm install
    - npm run build
  artifacts:
    paths:
      - frontend/dist/
    # TODO: set expiration in docker registry too
    expire_in: 1 week

#==[ Frontend deployment ]======================================================

.fe-deploy-job:
  image: alpine:latest
  stage: deploy
  needs:
    - job: fe-build-job
      artifacts: true
  tags:
    - commul
  before_script:
    - ./ci/install-butler-on-alpine.sh
  script:
    - butler push frontend/dist eurac/$GAME_NAME:html5

fe-prd-deploy-job:
  extends: .fe-deploy-job
  environment:
    name: production/frontend
    deployment_tier: production
    url: https://eurac.itch.io/oetzi
  variables:
    GAME_NAME: oetzi
  only: # TODO: only on tag
    - main

fe-stg-deploy-job:
  extends: .fe-deploy-job
  environment:
    name: staging/frontend
    deployment_tier: staging
    url: https://eurac.itch.io/oetzi-staging
  variables:
    GAME_NAME: oetzi-staging
  only:
    - development