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

stages:
  - setup
  - check
  - build
  - deploy

# TODO: maybe stup some notifications as a last stage?

#==[ JOB TEMPLATES ]============================================================

.node-job: &node-job
  image: node:16-alpine
  # tags:
  #   - shared
  only:
    - pushes

.fe-node-job:
  <<: *node-job
  cache:
    key:
      files:
        - frontend/package-lock.json
    paths:
      - frontend/.npm/
  before_script:
    - cd frontend
    - npm ci --cache .npm --prefer-offline --production=false

.be-node-job:
  <<: *node-job
  cache:
    key:
      files:
        - backend/package-lock.json
    paths:
      - backend/.npm/
  before_script:
    - cd backend
    - npm ci --cache .npm --prefer-offline

#==[ SETUP STAGE ]==============================================================

#--[ Frontend ]-----------------------------------------------------------------

fe-cache-job:
  extends: .fe-node-job
  stage: setup
  script:
    - echo "Done!"

#--[ Backend ]------------------------------------------------------------------

be-cache-job:
  extends: .be-node-job
  stage: setup
  script:
    - echo "Done!"

#--[ Cetera ]-------------------------------------------------------------------

app-tagger-job:
  stage: setup
  variables:
    GIT_STRATEGY: clone
    GIT_DEPTH: 0
  script:
    - GIT_DESCRIPTOR=$(git describe --tags --always)
    - echo Detected app version ${GIT_DESCRIPTOR}
    - echo "APP_VERSION=${GIT_DESCRIPTOR#v}" >> build.env
  artifacts:
    reports:
      dotenv: build.env
  only:
    - tags
    - development

#==[ CHECK STAGE ]==============================================================

#--[ Frontend ]-----------------------------------------------------------------

.fe-check-job: &fe-check-job
  extends: .fe-node-job
  stage: check
  needs:
    - fe-cache-job
  # allow_failure: true # TODO: disallow!

fe-format-job:
  <<: *fe-check-job
  script:
    - npm run format:check

fe-lint-job:
  <<: *fe-check-job
  script:
    - npm run lint:check

fe-test-job:
  <<: *fe-check-job
  script:
    - npm run test

#--[ Backend ]------------------------------------------------------------------

.be-check-job: &be-check-job
  extends: .be-node-job
  stage: check
  needs:
    - be-cache-job
  # allow_failure: true # TODO: disallow!

be-format-job:
  <<: *be-check-job
  script:
    - npm run format:check

be-lint-job:
  <<: *be-check-job
  script:
    - npm run lint:check

be-test-job:
  <<: *be-check-job
  script:
    - npm run test

#==[ BUILD STAGE ]==============================================================

#--[ Frontend ]-----------------------------------------------------------------

prd-fe-build-job:
  extends: .fe-node-job
  stage: build
  variables:
    NODE_ENV: production
    ROLLBAR_ACCESS_TOKEN: $POST_CLIENT_ITEM_ROLLBAR_ACCESS_TOKEN
  script:
    - npm run build -- --dist-dir dist/prd
  artifacts:
    paths:
      - frontend/dist/prd
    expire_in: 1 week
  only:
    - tags

stg-fe-build-job:
  extends: .fe-node-job
  stage: build
  variables:
    NODE_ENV: staging
    ROLLBAR_ACCESS_TOKEN: $POST_CLIENT_ITEM_ROLLBAR_ACCESS_TOKEN
  script:
    - npm run build -- --dist-dir dist/stg
  artifacts:
    paths:
      - frontend/dist/stg
    expire_in: 1 week
  only:
    - development

#--[ Backend ]------------------------------------------------------------------

.be-build-job:
  stage: build
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  variables:
    IMAGE_NAME: $CI_REGISTRY_IMAGE/backend
  before_script:
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json

prd-be-build-job:
  extends: .be-build-job
  stage: build
  script: >-
    /kaniko/executor
    --context "${CI_PROJECT_DIR}/backend"
    --dockerfile "${CI_PROJECT_DIR}/backend/Dockerfile"
    --target production
    --destination ${CI_REGISTRY_IMAGE}/backend:latest
    --destination ${CI_REGISTRY_IMAGE}/backend:${APP_VERSION}
    --destination ${CI_REGISTRY_IMAGE}/backend:${APP_VERSION%.*}
    --destination ${CI_REGISTRY_IMAGE}/backend:${APP_VERSION%%.*}
    --destination ${CI_REGISTRY_IMAGE}/backend:${CI_COMMIT_SHA}
    --build-arg APP_VERSION=$APP_VERSION
    --build-arg ROLLBAR_ACCESS_TOKEN=$POST_SERVER_ITEM_ROLLBAR_ACCESS_TOKEN
  only:
    - tags

stg-be-build-job:
  extends: .be-build-job
  stage: build
  script: >-
    /kaniko/executor
    --context "${CI_PROJECT_DIR}/backend"
    --dockerfile "${CI_PROJECT_DIR}/backend/Dockerfile"
    --target production
    --destination ${CI_REGISTRY_IMAGE}/backend:${CI_COMMIT_REF_NAME}
    --destination ${CI_REGISTRY_IMAGE}/backend:${APP_VERSION}
    --destination ${CI_REGISTRY_IMAGE}/backend:${CI_COMMIT_SHA}
    --build-arg APP_VERSION=$APP_VERSION
    --build-arg ROLLBAR_ACCESS_TOKEN=$POST_SERVER_ITEM_ROLLBAR_ACCESS_TOKEN
  only:
    - development

#==[ DEPLOY STAGE ]=============================================================

#--[ Frontend ]-----------------------------------------------------------------

.fe-deploy-job:
  image: alpine:latest
  stage: deploy
  # tags:
  #   - shared
  #   - commul # NOTE: we used to require commul because extended seccomp policies are needed
  before_script:
    - ./ci/install-butler-on-alpine.sh
  script:
    - >
      butler push
      $DIST_PATH
      eurac/$GAME_NAME:html5
      --userversion ${USER_VERSION}

prd-fe-deploy-job:
  extends: .fe-deploy-job
  needs:
    - app-tagger-job
    - job: prd-fe-build-job
      artifacts: true
  environment:
    name: production/frontend
    deployment_tier: production
    url: https://eurac.itch.io/oetzit
  variables:
    GAME_NAME: oetzit
    DIST_PATH: frontend/dist/prd
    USER_VERSION: ${APP_VERSION}
  only:
    - tags

stg-fe-deploy-job:
  extends: .fe-deploy-job
  needs:
    - app-tagger-job
    - job: stg-fe-build-job
      artifacts: true
  environment:
    name: staging/frontend
    deployment_tier: staging
    url: https://eurac.itch.io/oetzit-staging
  variables:
    GAME_NAME: oetzit-staging
    DIST_PATH: frontend/dist/stg
    USER_VERSION: ${APP_VERSION}
  only:
    - development

#--[ Backend ]------------------------------------------------------------------

.be-deploy-job:
  image: alpine:latest
  stage: deploy
  variables:
    IMAGE_NAME: $CI_REGISTRY_IMAGE/backend
  before_script:
    - ./ci/install-kubectl-on-alpine.sh
  script:
    - kubectl set image deployment/${K8S_DEPLOYMENT} oetzit=${IMAGE_NAME}:${APP_VERSION} --namespace=${K8S_NAMESPACE}

prd-be-deploy-job:
  extends: .be-deploy-job
  needs:
    - app-tagger-job
    - job: prd-be-build-job
      artifacts: false
  environment:
    name: production/backend
    deployment_tier: production
    url: https://kommul.eurac.edu/oetzit
  variables:
    K8S_NAMESPACE: kommul
    K8S_DEPLOYMENT: oetzit-webserver-deployment
  only:
    - tags

stg-be-deploy-job:
  extends: .be-deploy-job
  needs:
    - app-tagger-job
    - job: stg-be-build-job
      artifacts: false
  environment:
    name: staging/backend
    deployment_tier: staging
    url: https://kommul-dev.eurac.edu/oetzit
  variables:
    K8S_NAMESPACE: kommul-dev
    K8S_DEPLOYMENT: oetzit-webserver-deployment
  only:
    - development
