# TODO: remove this as submodules aren't used anymore.
variables:
  GIT_SUBMODULE_STRATEGY: recursive

stages:
  - build_docker
  - build_and_deploy_docker
  - build_and_deploy_website
  - test

.website_build: &website_build
  stage: build_and_deploy_website
  image: node:8
  before_script:
    - scripts/install_native_dependencies.sh
    - scripts/install_opam.sh # TODO: or scripts/install_build_environment.sh ?
    - export PATH="/usr/local/bin${PATH:+:}${PATH:-}"
    - eval $(opam config env)
    - scripts/setup_switch.sh
    - eval $(opam config env)
    - scripts/setup_repos.sh

    # install deps for internal documentation
    - scripts/install_vendors_deps.sh
    - opam install -y odoc
    - scripts/build_ligo_local.sh

    # build with odoc
    - dune build @doc

    # npm
    - cd gitlab-pages/website
    - npm install
  script:
    - npm run version next
    - npm run build
    # move internal odoc documentation to the website folder
    - mkdir -p build/ligo/
    - mv ../../_build/default/_doc/_html/ build/ligo/odoc
    - pwd            # for debug
    - ls build/ligo/ # for debug
  after_script:
    - cp -r gitlab-pages/website/build/ligo public
  artifacts:
    paths:
      - public

.docker: &docker
  image: docker:1.11
  services:
    - docker:dind

.docker_build: &docker_build
  script:
    - docker build -t $LIGO_REGISTRY_IMAGE:next -f ./docker/Dockerfile .

.before_script: &before_script
  before_script:
    # Install dependencies
    # rsync is needed by opam to sync a package installed from a local directory with the copy in ~/.opam
    - scripts/install_native_dependencies.sh
    - scripts/install_opam.sh # TODO: or scripts/install_build_environment.sh ?
    - export PATH="/usr/local/bin${PATH:+:}${PATH:-}"
    - eval $(opam config env)
    - scripts/setup_switch.sh
    - eval $(opam config env)
    - scripts/setup_repos.sh

local-dune-job:
  <<: *before_script
  stage: test
  script:
    - scripts/install_vendors_deps.sh
    - scripts/build_ligo_local.sh
    - dune build @ligo-test

# TODO: uncomment this

# TODO
# local-repo-job:
#   <<: *before_script
#   stage: test
#   script:
#     - scripts/install_vendors_deps.sh
#     # TODO: also try from time to time with --build-test
#     - opam install -y ligo

remote-repo-job:
  <<: *before_script
  stage: test
  script:
    # Add repository
    - opam repository add ligo-repository https://gitlab.com/ligolang/ligo.git
    - eval $(opam config env)
    - opam install -y ligo
    # Used in the IDE
    #- opam install -y user-setup
    #- opam install -y merlin
    #- opam install -y ocp-indent
    #- opam user-setup install
  only:
    - master

# TODO: uncomment this

# Run a docker build without publishing to the registry
build-current-docker-image:
  stage: build_docker
  <<: *docker
  <<: *docker_build
  except:
    - master
    - dev

# When a MR/PR is merged to dev
# take the previous build and publish it to Docker Hub
build-and-publish-latest-docker-image:
  stage: build_and_deploy_docker
  <<: *docker
  <<: *docker_build
  after_script:
    - docker login -u $LIGO_REGISTRY_USER -p $LIGO_REGISTRY_PASSWORD
    - docker push $LIGO_REGISTRY_IMAGE:next
  only:
    - dev

# Pages are deployed from both master & dev, be careful not to override 'next'
# in case something gets merged into 'dev' while releasing.
pages:
  <<: *website_build
  only:
    - master
    - dev