---

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

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

.dind_docker_tags: &dind_docker_tags
  tags:
    - "dind"

.global_variables: &global_variables
  DUNE_OPTIONS_FILE: /duneci/dune.opts
  DUNECONTROL: dunecontrol
  BASE_IMAGE_VERSION: v0.4.0-git

.docker_variables: &docker_variables
  DOCKER_IMAGE: docker:19.03.1
  DOCKER_HOST: tcp://docker:2375/
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""

.debian_gcc: &debian_gcc
  <<: *global_variables
  TOOLCHAIN: gcc-8-17
  DUNECI_OS: debian
  DUNECI_BASE_IMAGE: ${CI_REGISTRY}/docker/ci/debian:10
  DUNE_COPASI_BUILD_IMAGE: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${TOOLCHAIN}-${BASE_IMAGE_VERSION}
  DUNE_COPASI_DEPLOY_IMAGE: debian:buster
  DEPLOY_LATEST: "true"

.debian_clang: &debian_clang
  <<: *global_variables
  TOOLCHAIN: clang-6-17
  DUNECI_OS: debian
  DUNECI_BASE_IMAGE: ${CI_REGISTRY}/docker/ci/debian:10
  DUNE_COPASI_BUILD_IMAGE: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${TOOLCHAIN}-${BASE_IMAGE_VERSION}
  DUNE_COPASI_DEPLOY_IMAGE: debian:buster

.ubuntu_gcc: &ubuntu_gcc
  <<: *global_variables
  TOOLCHAIN: gcc-7-17
  DUNECI_OS: ubuntu
  DUNECI_BASE_IMAGE: ${CI_REGISTRY}/docker/ci/ubuntu:18.04
  DUNE_COPASI_BUILD_IMAGE: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${TOOLCHAIN}-${BASE_IMAGE_VERSION}
  DUNE_COPASI_DEPLOY_IMAGE: ubuntu:18.04

.ubuntu_clang: &ubuntu_clang
  <<: *global_variables
  TOOLCHAIN: clang-6-17
  DUNECI_OS: ubuntu
  DUNECI_BASE_IMAGE: ${CI_REGISTRY}/docker/ci/ubuntu:18.04
  DUNE_COPASI_BUILD_IMAGE: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${TOOLCHAIN}-${BASE_IMAGE_VERSION}
  DUNE_COPASI_DEPLOY_IMAGE: ubuntu:18.04

.setup: &setup
  image: ${DOCKER_IMAGE}
  stage: setup
  script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY/copasi/dune-copasi
    - docker build -f docker/dependencies.dockerfile
        --build-arg BASE_IMAGE=${DUNECI_BASE_IMAGE}
        --build-arg TOOLCHAIN=${TOOLCHAIN}
        -t ${DUNE_COPASI_BUILD_IMAGE} .
    - docker push ${DUNE_COPASI_BUILD_IMAGE}
    - docker logout $CI_REGISTRY/copasi/dune-copasi
  rules:
    - if: '$REBUILD_BASE_IMAGE'
    - if: '$CI_COMMIT_TAG'
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - if: '$CI_COMMIT_BRANCH == "latest"'
  <<: *dind_docker_tags

.build: &build
  image: ${DUNE_COPASI_BUILD_IMAGE}
  stage: build
  script:
    - cd ..
    - bash dune-copasi/.ci/build.sh
  artifacts:
    paths:
      - build-cmake
    expire_in: 30 mins
  <<: *default_tags

.unit_tests: &unit_tests
  image: ${DUNE_COPASI_BUILD_IMAGE}
  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
    when: always
  <<: *default_tags

.system_tests: &system_tests
  image: ${DUNE_COPASI_BUILD_IMAGE}
  stage: system_tests
  script:
    - cd ..
    - bash dune-copasi/.ci/system_tests.sh
  artifacts:
    paths:
      - cd ..
      - build-cmake/test
    expire_in: 1 day
    when: always
  <<: *default_tags

.deploy: &deploy
  image: ${DOCKER_IMAGE}
  stage: deploy
  script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY/copasi/dune-copasi
    - DOCKER_TAG="${DUNECI_OS}-${TOOLCHAIN}-${CI_COMMIT_REF_NAME}"
    - docker build -f docker/dune-copasi.dockerfile
      --build-arg BUILD_IMAGE=${DUNE_COPASI_BUILD_IMAGE}
      --build-arg PRODUCTION_IMAGE=${DUNE_COPASI_DEPLOY_IMAGE}
      --build-arg BRANCH=${CI_COMMIT_REF_NAME}
      -t $CI_REGISTRY/copasi/dune-copasi/dune-copasi:${DOCKER_TAG} .
    - docker push $CI_REGISTRY/copasi/dune-copasi/dune-copasi:${DOCKER_TAG}
    - |
      if [ "$CI_COMMIT_BRANCH" == "latest" && $DEPLOY_LATEST ]; then
        docker tag $CI_REGISTRY/copasi/dune-copasi/dune-copasi:${DOCKER_TAG} $CI_REGISTRY/copasi/dune-copasi/dune-copasi:latest
        docker push $CI_REGISTRY/copasi/dune-copasi/dune-copasi:latest
      fi
    - docker logout $CI_REGISTRY/copasi/dune-copasi
  rules:
    - if: '$CI_COMMIT_TAG'
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - if: '$CI_COMMIT_BRANCH == "latest"'
  <<: *dind_docker_tags

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

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

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

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

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

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

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

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

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

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

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

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

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

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

deploy:debian_clang:
  variables:
    <<: *docker_variables
    <<: *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
