---

stages:
  - setup
  - build
  - unit_tests
  - system_tests
  - deploy

.docker_variables: &docker_variables
  image: docker:19.03.1
  variables:
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  tags:
    - "dind"

.general_definitions: &general_definitions
  tags:
    - "cores:4"

# OS setup
.debian_gcc: &debian_gcc
  <<: *general_definitions
  image: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:debian-gcc-8-17
  before_script:
    - DUNECI_TOOLCHAIN="gcc-8-17"
    - DUNECI_OS="debian"
    - DUNECI_BASE_IMAGE=${CI_REGISTRY}/docker/ci/debian:10
    - DUNE_COPASI_BASE_IMAGE=${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${DUNECI_TOOLCHAIN}
    - export DUNE_OPTIONS_FILE=/duneci/dune.opts
    - export DUNECONTROL=dunecontrol

.debian_clang: &debian_clang
  <<: *general_definitions
  image: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:debian-clang-7-libcpp-17
  before_script:
    - DUNECI_TOOLCHAIN="clang-7-libcpp-17"
    - DUNECI_OS="debian"
    - DUNECI_BASE_IMAGE=${CI_REGISTRY}/docker/ci/debian:10
    - DUNE_COPASI_BASE_IMAGE=${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${DUNECI_TOOLCHAIN}
    - export DUNE_OPTIONS_FILE=/duneci/dune.opts
    - export DUNECONTROL=dunecontrol

.ubuntu_gcc: &ubuntu_gcc
  <<: *general_definitions
  image: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:ubuntu-gcc-7-17
  before_script:
    - DUNECI_TOOLCHAIN="gcc-7-17"
    - DUNECI_OS="ubuntu"
    - DUNECI_BASE_IMAGE=${CI_REGISTRY}/docker/ci/ubuntu:18.04
    - DUNE_COPASI_BASE_IMAGE=${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${DUNECI_TOOLCHAIN}
    - export DUNE_OPTIONS_FILE=/duneci/dune.opts
    - export DUNECONTROL=dunecontrol

.ubuntu_clang: &ubuntu_clang
  <<: *general_definitions
  image: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:ubuntu-clang-6-17
  before_script:
    - DUNECI_TOOLCHAIN="clang-6-17"
    - DUNECI_OS="ubuntu"
    - DUNECI_BASE_IMAGE=${CI_REGISTRY}/docker/ci/ubuntu:18.04
    - DUNE_COPASI_BASE_IMAGE=${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${DUNECI_TOOLCHAIN}
    - export DUNE_OPTIONS_FILE=/duneci/dune.opts
    - export DUNECONTROL=dunecontrol

# Stages
.setup: &setup
  <<: *docker_variables
  stage: setup
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY/copasi/dune-copasi
    - docker build -f docker/dependencies.dockerfile
        --build-arg BASE_IMAGE=${DUNECI_BASE_IMAGE}
        -t ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${DUNECI_TOOLCHAIN} .
    - docker push ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${DUNECI_TOOLCHAIN}
    - docker logout $CI_REGISTRY/copasi/dune-copasi
  only:
    - master

.build: &build
  stage: build
  script:
    - cd ..
    - bash dune-copasi/.ci/build.sh
  artifacts:
    paths:
      - build-cmake
    expire_in: 30 mins

.unit_tests: &unit_tests
  stage: unit_tests
  script: 
    - echo 'CMAKE_FLAGS+=" -DCMAKE_BUILD_TYPE=Debug"' >> /duneci/cmake-flags/dune_copasi.opts
    - cd .. 
    - bash dune-copasi/.ci/unit_tests.sh
  artifacts:
    paths:
      - build-cmake/test
    expire_in: 1 day

.system_tests: &system_tests
  stage: system_tests
  script: 
    - cd ..
    - bash dune-copasi/.ci/system_tests.sh
  artifacts:
    paths:
      - cd ..
      - build-cmake/test
    expire_in: 1 day

.deploy: &deploy
  <<: *docker_variables
  stage: deploy
  script: 
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY/copasi/dune-copasi
    - docker build -f docker/dune-copasi.dockerfile
        --build-arg BASE_IMAGE=${DUNE_COPASI_BASE_IMAGE}
        --build-arg BRANCH=${CI_COMMIT_REF_NAME}
        -t $CI_REGISTRY/copasi/dune-copasi/dune-copasi:${DUNECI_OS}-${DUNECI_TOOLCHAIN}-${CI_COMMIT_REF_NAME} .
    - docker push $CI_REGISTRY/copasi/dune-copasi/dune-copasi:${DUNECI_OS}-${DUNECI_TOOLCHAIN}-${CI_COMMIT_REF_NAME}
    - docker logout $CI_REGISTRY/copasi/dune-copasi
  only:
    - master

# debian gcc
setup:debian_gcc:
  <<: *debian_gcc
  <<: *setup

build:debian_gcc:
  <<: *debian_gcc
  <<: *build
  dependencies:
    - setup:debian_gcc

unit_tests:debian_gcc:
  <<: *debian_gcc
  <<: *unit_tests
  dependencies:
    - build:debian_gcc

system_tests:debian_gcc:
  <<: *debian_gcc
  <<: *system_tests
  dependencies:
    - build:debian_gcc

deploy:debian_gcc:
  <<: *debian_gcc
  <<: *deploy
  dependencies:
    - system_tests:debian_gcc

# # ubuntu gcc
# setup:ubuntu_gcc:
#   <<: *ubuntu_gcc
#   <<: *setup  

# build:ubuntu_gcc:
#   <<: *ubuntu_gcc
#   <<: *build
#   dependencies:
#     - setup:ubuntu_gcc

# unit_tests:ubuntu_gcc:
#   <<: *ubuntu_gcc
#   <<: *unit_tests
#   dependencies:
#     - build:ubuntu_gcc

# system_tests:ubuntu_gcc:
#   <<: *ubuntu_gcc
#   <<: *system_tests
#   dependencies:
#     - build:ubuntu_gcc

# deploy:ubuntu_gcc:
#   <<: *ubuntu_gcc
#   <<: *deploy
#   dependencies:
#     - system_tests:ubuntu_gcc

# debian clang
setup:debian_clang:
  <<: *debian_clang
  <<: *setup

build:debian_clang:
  <<: *debian_clang
  <<: *build
  dependencies:
    - setup:debian_clang

unit_tests:debian_clang:
  <<: *debian_clang
  <<: *unit_tests
  dependencies:
    - build:debian_clang

system_tests:debian_clang:
  <<: *debian_clang
  <<: *system_tests
  dependencies:
    - build:debian_clang

deploy:debian_clang:
  <<: *debian_clang
  <<: *deploy
  dependencies:
    - system_tests:debian_clang

# # ubuntu clang
# setup:ubuntu_clang:
#   <<: *ubuntu_clang
#   <<: *setup  

# build:ubuntu_clang:
#   <<: *ubuntu_clang
#   <<: *build
#   dependencies:
#     - setup:ubuntu_clang

# unit_tests:ubuntu_clang:
#   <<: *ubuntu_clang
#   <<: *unit_tests
#   dependencies:
#     - build:ubuntu_clang

# system_tests:ubuntu_clang:
#   <<: *ubuntu_clang
#   <<: *system_tests
#   dependencies:
#     - build:ubuntu_clang    

# deploy:ubuntu_clang:
#   <<: *ubuntu_clang
#   <<: *deploy
#   dependencies:
#     - system_tests:ubuntu_clang