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

stages:
  - setup
  - check
  - build
  - deploy

#==[ Prepare node ]=============================================================

fe-cache-node-job:
  image: node:16-alpine
  stage: setup
  tags:
    - commul
  cache:
    key:
      files:
        - frontend/package-lock.json
    paths:
      - frontend/.npm/
  script:
    - cd frontend
    - npm ci --cache .npm --prefer-offline
  only:
    - tags
    - development

#==[ Frontend checks ]==========================================================

fe-check-node-job:
  needs:
    - fe-cache-node-job
  image: node:16-alpine
  stage: check
  tags:
    - commul
  cache:
    key:
      files:
        - frontend/package-lock.json
    paths:
      - frontend/.npm/
  script:
    - cd frontend
    - npm ci --cache .npm --prefer-offline
    - npm run test
  only:
    - tags
    - development

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

# TODO: cache docker layers
# TODO: build ts
.be-build-job:
  image: $DOCKER_IMAGE
  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}
      --tag=$IMAGE_NAME:${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}
      backend
    - docker image push --all-tags $IMAGE_NAME
  only:
    - tags
    - development

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

.be-deploy-job:
  image: alpine:latest
  stage: deploy
  needs:
    - job: be-build-job
      artifacts: false
  tags:
    - commul
  variables:
    IMAGE_NAME: $CI_REGISTRY_IMAGE/backend
  before_script:
    - ./ci/install-kubectl-on-alpine.sh
  script:
    - kubectl set image deployment/${K8S_DEPLOYMENT} oetzi=${IMAGE_NAME}:${CI_COMMIT_SHA} --namespace=${K8S_NAMESPACE}

.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-webserver-deployment
  only:
    - tags

.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-webserver-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
  only:
    - tags
    - development

#==[ 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
      --userversion ${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}

.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:
    - tags

.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
