stages:
  - environment
  - build
  - check
  - package
  - deploy

before_script:
  - which docker && echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" --username "$CI_REGISTRY_USER" --password-stdin

variables:
  BRANCH: "$CI_COMMIT_REF_NAME"
  IMAGE_CI: "$CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME/ci:$CI_COMMIT_REF_SLUG"
  IMAGE_CI_MASTER: "$CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME/ci:master"
  IMAGE: "$CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME"
  SLUG: "$CI_COMMIT_REF_SLUG"
  FROM_SCRATCH:
    value: ""
    description: "Rebuild Docker images from scratch if not empty"

.docker:
  image: registry.gitlab.routine.co/routine/logistic/ci-builder

.ocaml:
  image: $IMAGE_CI$VARIANT

.ocaml-toolchain:
  extends: .docker
  stage: environment
  script:
    - make -f .logistic/ci/Makefile ci/image
    - docker push "$IMAGE_CI$VARIANT"

.ocaml-build:
  extends: .ocaml
  stage: build
  script:
    - make -f .logistic/ci/Makefile build
    - make -f .logistic/ci/Makefile lint
  artifacts:
    expire_in: 10 days
    paths:
      - _build/

.ocaml-check:
  extends: .ocaml
  stage: check
  script:
    - make -f .logistic/ci/Makefile check
  artifacts:
    expire_in: 10 days
    paths:
      - _build/

.ocaml-package:
  extends: .ocaml
  stage: package
  script:
    - make -f .logistic/ci/Makefile package
  after_script:
    - make -f .logistic/ci/Makefile package/clean

.ocaml-opam:
  extends: .ocaml
  stage: package
  script:
    - make -f .logistic/ci/Makefile opam
  rules:
    - if: '$CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+/'
