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

stages:
  - build
  - deploy
  - setup

.be-build-job:
  variables:
    IMAGE_NAME: $CI_REGISTRY_IMAGE/backend
  stage: build
  image: docker:latest
  services:
    - docker:dind
  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

.be-deploy-job:
  variables:
    KUBECTL_VERSION: v1.8.10
    IMAGE_NAME: $CI_REGISTRY_IMAGE/backend

  stage: deploy
  tags:
    - commul
  image: docker:latest
  services:
    - docker:dind
  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
    - echo YAY!
    - docker pull ${IMAGE_NAME}:main
    #- kubectl --namespace kommul-dev set image deployment/merlin browser=${CONTAINER_IMAGE}:${CI_COMMIT_SHA}
  only:
    - main

.be-setup-job:
  image: node:16-alpine
  stage: setup
  cache:
    - key:
        files:
          - backend/package-lock.json
      paths:
        - backend/node_modules
  script:
    # TODO: drop python once we stop using sqlite3 in CI (soon, please)
    - apk add --update python2 build-base
    - cd backend
    - npm install
    - echo Success!

.fe-setup-job:
  image: node:16-alpine
  stage: setup
  cache:
    - key:
        files:
          - frontend/package-lock.json
      paths:
        - frontend/node_modules
  script:
    - cd frontend
    - npm install
    - echo Success!

fe-deploy-job:
  variables:
    GLIBC_URL: https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.34-r0/glibc-2.34-r0.apk
    BUTLER_URL: https://broth.itch.ovh/butler/linux-amd64/LATEST/archive/default
    # NOTE: this key is a throwaway, we'll store one in secrets
    BUTLER_API_KEY: 0UsKHAvtO2aXBJMPeR6bZHxSv9BRQ0zIvFPdFdRW
  image: node:16-alpine
  stage: deploy
  cache:
    - key:
        files:
          - frontend/package-lock.json
      paths:
        - frontend/node_modules
  before_script:
    - "#=[ install tools ]====================="
    # NOTE: Alpine Linux stuff if linked with muslc. Butler is linked with glibc, and this causes an error -- see https://stackoverflow.com/a/66974607/6438061 for details. We need to install a compatibility layer: https://github.com/sgerrand/alpine-pkg-glibc#installing
    - "#-[ install glibc ]---------------------"
    - wget -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
    - wget -O glibc.apk $GLIBC_URL
    - apk add glibc.apk
    - rm glibc.apk
    - "#-[ install unzip ]---------------------"
    - apk add --no-cache unzip
    - "#-[ install butler ]--------------------"
    - wget -O butler.zip $BUTLER_URL
    - unzip butler.zip *.so -d /usr/local/lib
    - unzip butler.zip butler -d /usr/local/bin
    - chmod +x /usr/local/bin/butler
    - rm butler.zip
    - butler -V
  script:
    - cd frontend
    - npm install
    - npm run build
    - butler push dist paolobrasolin/oetzi:html5