---

stages:
  - setup
  - build
  - unit_test
  - system_test
  - 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}

.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}

.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}

.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}

# 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:
    - dunecontrol --opts=/duneci/dune.opts --only=dune-copasi all
  artifacts:
    paths:
      - build-cmake
    expire_in: 30 mins

.unit_test: &unit_test
  stage: unit_test
  script: 
    - echo 'CMAKE_FLAGS+=" -DCMAKE_BUILD_TYPE=Debug"' >> /duneci/cmake-flags/dune_copasi.opts
    - dunecontrol --opts=/duneci/dune.opts --only=dune-copasi configure
    - dunecontrol --opts=/duneci/dune.opts --only=dune-copasi make --target build_unit_tests
    - dunecontrol --opts=/duneci/dune.opts --only=dune-copasi bexec ctest -j4 -L "unit"
  artifacts:
    paths:
      - build-cmake/test
    expire_in: 1 day

.system_test: &system_test
  stage: system_test
  script: 
    - dunecontrol --opts=/duneci/dune.opts --only=dune-copasi bexec ctest -j4 -L "DUNE_SYSTEMTEST"
  artifacts:
    paths:
      - 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_test:debian_gcc:
  <<: *debian_gcc
  <<: *unit_test
  dependencies:
    - build:debian_gcc

system_test:debian_gcc:
  <<: *debian_gcc
  <<: *system_test
  dependencies:
    - build:debian_gcc

deploy:debian_gcc:
  <<: *debian_gcc
  <<: *deploy
  dependencies:
    - system_test:debian_gcc

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

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

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

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

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

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

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

unit_test:debian_clang:
  <<: *debian_clang
  <<: *unit_test
  dependencies:
    - build:debian_clang

system_test:debian_clang:
  <<: *debian_clang
  <<: *system_test
  dependencies:
    - build:debian_clang

deploy:debian_clang:
  <<: *debian_clang
  <<: *deploy
  dependencies:
    - system_test:debian_clang

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

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

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

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

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