# Optimising CI speed by using tips from https://blog.nimbleways.com/let-s-make-faster-gitlab-ci-cd-pipelines/
image: adinapoli/gargantext:v3.3

variables:
  STACK_ROOT: "${CI_PROJECT_DIR}/.stack-root"
  STACK_OPTS: "--system-ghc"
  CABAL_STORE_DIR: "${CI_PROJECT_DIR}/.cabal"
  CORENLP: "4.5.4"
  FF_USE_FASTZIP: "true"
  ARTIFACT_COMPRESSION_LEVEL: "fast"
  CACHE_COMPRESSION_LEVEL: "fast"

stages:
  - cabal
  - stack
  - bench
  - test

stack:
  stage: stack
  cache:
    key: stack.yaml
    paths:
      - .stack-root/
      - .stack-work/
  script:
    - echo "Building the project from '$CI_PROJECT_DIR'"
    - nix-shell --run "stack build --no-terminal --fast --dry-run"
  allow_failure: false

cabal:
  stage: cabal
  cache:
    key: cabal.project
    paths:
      - dist-newstyle/
      - .cabal/
    policy: pull-push
  script:
    - nix-shell --run "./bin/update-project-dependencies $CABAL_STORE_DIR && cabal --store-dir=$CABAL_STORE_DIR v2-build --flags test-crypto --ghc-options='-O0 -fclear-plugins'"
  allow_failure: false

bench:
  stage: bench
  when: manual # trigger it manually, as it causes full recompilation with optimisations enabled.
  cache:
    key: cabal.project
    paths:
      - dist-newstyle/
      - .cabal/
    policy: pull-push
  script:
    - nix-shell --run "./bin/update-project-dependencies $CABAL_STORE_DIR && cabal --store-dir=$CABAL_STORE_DIR v2-bench --flags +no-phylo-debug-logs --ghc-options='-O2 -fclear-plugins'"
  allow_failure: true

test:
  stage: test
  cache:
    key: cabal.project
    paths:
      - dist-newstyle/
      - .cabal/
    policy: pull-push
  # The tests needs to run as the 'test' user, because they leverage the
  # initdb utility from postgres that cannot be run by 'root'.
  script:
    - |
      mkdir -p /home/test
      mkdir -p /root/.config
      useradd -U test
      chown -R test:test dist-newstyle/
      chown -R test:test /root/
      chown -R test:test $CABAL_STORE_DIR
      export CABAL=$(nix-shell --run "which cabal")
      export TEST_NIX_PATH=$(nix-shell --run "echo -n \$PATH")
      echo $CABAL
      echo $TEST_NIX_PATH
      git config --global --add safe.directory '*'
      nix-shell --run "./bin/update-project-dependencies $CABAL_STORE_DIR"
      mkdir -p /root/.cache/cabal/logs
      chown -R test:test /root/.cache/cabal/logs/
      chown -R test:test /root/.cache/cabal/packages/hackage.haskell.org/

      mkdir -p /builds/gargantext/haskell-gargantext/devops/coreNLP/stanford-corenlp-current
      cp -R /root/devops/coreNLP/stanford-corenlp-${CORENLP}/* /builds/gargantext/haskell-gargantext/devops/coreNLP/stanford-corenlp-current/

      nix-shell --run "chown -R test:test /root/.config/ && su -m test -c \"export PATH=$PATH:$TEST_NIX_PATH && cd /builds/gargantext/haskell-gargantext; $CABAL --store-dir=$CABAL_STORE_DIR v2-test --test-show-details=streaming --flags 'test-crypto no-phylo-debug-logs' --ghc-options='-O0 -fclear-plugins'\""
      chown -R root:root dist-newstyle/
      chown -R root:root /root/
      chown -R root:root $CABAL_STORE_DIR
      chown -R root:root /root/.cache/cabal/logs/
      chown -R root:root /root/.cache/cabal/packages/hackage.haskell.org/
      chown -Rh root:root /builds/gargantext/haskell-gargantext/devops/coreNLP/stanford-corenlp-current

#docs:
#  stage: docs
#  cache:
#    key: stack.yaml
#    paths:
#      - .stack-root/
#      - .stack-work/
#    policy: pull
#  script:
#    - nix-shell --run "stack build --no-terminal --haddock --no-haddock-deps --fast --dry-run"
#    - cp -R "$(stack path --local-install-root)"/doc ./output
#  # FIXME(adinapoli) Currently Gitlab 11.x doesn't support the 'rules' keyword.
#  # rules:
#  # - if: '$CI_MERGE_REQUEST_IID' # Run job on Merge Requests
#  only:
#    - merge_requests
#  artifacts:
#    paths:
#      - ./output
#    expire_in: 1 week
#  allow_failure: true
