stages:
  - 'Install'
  - 'Test'
  - 'Deploy'

variables:
  CYPRESS_CACHE_FOLDER: '$CI_PROJECT_DIR/cache/Cypress'
  DOCKER_BUILDKIT: 1 # use buildkit for better performance
  DOCKER_DRIVER: overlay2 # better docker driver to avoid copying too many files on each run
  GITLAB_REGISTRY: registry.gitlab.com # We use docker.io for official images and gitlab's registry to store temporary images
  IMAGE_NAME: geovisio/api
  CI_IMAGE_CACHE: $GITLAB_REGISTRY/$IMAGE_NAME:build_cache
  DOCKER_TLS_CERTDIR: ""

before_script:
  ## chmod is unfortunately currently mandatory : https://github.com/nodejs/docker-node/issues/661
  - npm install -g yarn@1.22.19 && chmod +x /usr/local/lib/node_modules/yarn/bin/yarn.js

cache:
  paths:
    - node_modules

install:
  stage: Install
  image: node:18.16.0
  script:
    - yarn install
    - ls node_modules/.bin/cypress

test:unit:
  stage: Test
  image: node:18.16.0
  script:
    - yarn test:unit
  coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)/

test:e2e:
  stage: Test
  image: cypress/browsers:node-18.16.0-chrome-113.0.5672.92-1-ff-113.0-edge-113.0.1774.35-1
  script:
    - yarn install
    - ./node_modules/.bin/cypress install
    - echo "VITE_API_URL=https://geovisio-proxy-dev.osc-fr1.scalingo.io/" > .env
    - echo "VITE_ENV=dev" >> .env
    - PORT=5173 yarn start &
    - yarn test:e2e
  coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)/
  artifacts:
    when: always
    paths:
      - cypress/videos/**/*.mp4
      - cypress/screenshots/**/*.png
    expire_in: 1 day

deploy:
  stage: Deploy
  image: node:18.16.0
  cache:
    paths:
      - node_modules
  variables:
    CI: 1
  script:
    - yarn install
    - yarn build

deploy:develop:
  rules:
    # run job only for fork that have the credentials to pull images from the gitlab-registry
    # and only for merge on 'develop' branch
    - if: $CI_DEPLOY_PASSWORD == null || $CI_DEPLOY_USER == null
      when: never
    - if: $CI_COMMIT_REF_SLUG == "develop"
  stage: Deploy
  image: docker:latest
  services:
      - docker:dind
  before_script:
    # login to the gitlab docker registry to use the cache and to publish
    - echo $CI_DEPLOY_PASSWORD | docker login -u $CI_DEPLOY_USER --password-stdin $GITLAB_REGISTRY
    - docker buildx create --use --name "geovisio-image-builder" --driver=docker-container # use docker-container driver to be able to publish a full cache
    # login to dockerhub
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
  script:
    # build image using repository as cache
    - docker buildx build
      --cache-from "type=registry,ref=$CI_IMAGE_CACHE"
      --cache-to "type=registry,mode=max,ref=$CI_IMAGE_CACHE"
      --tag "$CI_REGISTRY_IMAGE:develop"
      --label "org.opencontainers.image.title=$CI_PROJECT_TITLE"
      --label "org.opencontainers.image.url=$CI_PROJECT_URL"
      --label "org.opencontainers.image.created=$CI_JOB_STARTED_AT"
      --label "org.opencontainers.image.revision=$CI_COMMIT_SHORT_SHA"
      --load
      --progress=plain
      .

    # publish image to dockerhub with the develop tag
    - docker push "$CI_REGISTRY_IMAGE:develop"

deploy:latest:
  # we consider that tag always land on main
  # and they always should publish a tagged image and the `latest` docker image
  only:
    - tags
  stage: Deploy
  image: docker:latest
  services:
      - docker:dind
  before_script:
    # login to the gitlab docker registry to use the cache and to publish
    - echo $CI_DEPLOY_PASSWORD | docker login -u $CI_DEPLOY_USER --password-stdin $GITLAB_REGISTRY
    - docker buildx create --use --name "geovisio-image-builder" --driver=docker-container # use docker-container driver to be able to publish a full cache
    # login to dockerhub
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
  script:
    # build image using repository as cache
    - docker buildx build
      --cache-from "type=registry,ref=$CI_IMAGE_CACHE"
      --cache-to "type=registry,mode=max,ref=$CI_IMAGE_CACHE"
      --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
      --tag "$CI_REGISTRY_IMAGE:latest"
      --label "org.opencontainers.image.title=$CI_PROJECT_TITLE"
      --label "org.opencontainers.image.url=$CI_PROJECT_URL"
      --label "org.opencontainers.image.created=$CI_JOB_STARTED_AT"
      --label "org.opencontainers.image.revision=$GIT_DESCRIBE"
      --load
      --progress=plain
      .

    # publish image to dockerhub
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
    - docker push $CI_REGISTRY_IMAGE:latest
