---

stages:
  - setup
  - build
  - deploy

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

.dind_docker_tags: &dind_docker_tags
  tags:
    - "dind"

.global_variables: &global_variables
  DUNECONTROL: dunecontrol
  BASE_IMAGE_VERSION: v0.4.0-git
  DUNE_COPASI_SD_EXECUTABLE: 'ON'
  CPACK_GENERATORS: 'DEB'
  CPACK_PACKAGE_DIRECTORY: $CI_PROJECT_DIR/packages

.docker_variables: &docker_variables
  DOCKER_BUILDKIT: 1
  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
  BASE_IMAGE: debian:10
  DEPENDENCIES_IMAGE: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${TOOLCHAIN}-${BASE_IMAGE_VERSION}

.debian_clang: &debian_clang
  <<: *global_variables
  TOOLCHAIN: clang-6-17
  DUNECI_OS: debian
  BASE_IMAGE: debian:10
  DEPENDENCIES_IMAGE: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${TOOLCHAIN}-${BASE_IMAGE_VERSION}
  MASTER_IMAGE: "true"

.ubuntu_gcc: &ubuntu_gcc
  <<: *global_variables
  TOOLCHAIN: gcc-7-17
  DUNECI_OS: ubuntu
  BASE_IMAGE: ubuntu:18.04
  DEPENDENCIES_IMAGE: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${TOOLCHAIN}-${BASE_IMAGE_VERSION}

.ubuntu_clang: &ubuntu_clang
  <<: *global_variables
  TOOLCHAIN: clang-6-17
  DUNECI_OS: ubuntu
  BASE_IMAGE: ubuntu:18.04
  DEPENDENCIES_IMAGE: ${CI_REGISTRY}/copasi/dune-copasi/dependencies:${DUNECI_OS}-${TOOLCHAIN}-${BASE_IMAGE_VERSION}

.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
        --build-arg BASE_IMAGE=${BASE_IMAGE}
        --build-arg TOOLCHAIN=${TOOLCHAIN}
        --build-arg BUILDKIT_INLINE_CACHE=1
        --target setup-env
        -t ${DEPENDENCIES_IMAGE} .
    - docker push ${DEPENDENCIES_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: ${DEPENDENCIES_IMAGE}
  stage: build
  script:
    - ./.ci/install /duneci/dune.opts
    - ./.ci/test /duneci/dune.opts
  <<: *default_tags
  artifacts:
    paths:
      - packages
    expire_in: 1h

.deploy: &deploy
  image: ${DOCKER_IMAGE}
  stage: deploy
  script:
    # login into registry
    - 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_DEPLOY_IMAGE=$CI_REGISTRY/copasi/dune-copasi/dune-copasi:${DOCKER_TAG}
    # set directory to get packages from the image
    - mkdir -p $CI_PROJECT_DIR/packages
    # build final production image using packages from artifacts
    - docker build
      --build-arg PRODUCTION_BASE_IMAGE=${BASE_IMAGE}
      --file docker/deploy.dockerfile
      --tag ${DOCKER_DEPLOY_IMAGE} .
    # push images into registry
    - docker push ${DOCKER_DEPLOY_IMAGE}
    # set alternative and simpler names
    - |
      if [[ ! -z "${MASTER_IMAGE}" ]]; then
        DOCKER_TAG=${CI_REGISTRY}/copasi/dune-copasi/dune-copasi:${CI_COMMIT_REF_NAME}
        docker tag ${DOCKER_DEPLOY_IMAGE} ${DOCKER_TAG}
        docker push ${DOCKER_TAG}
        if [[ "$CI_COMMIT_BRANCH" == "latest" ]]; then
          DOCKER_TAG=${CI_REGISTRY}/copasi/dune-copasi/dune-copasi:latest
          docker tag ${DOCKER_DEPLOY_IMAGE} ${DOCKER_TAG}
          docker push ${DOCKER_TAG}
        fi
      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

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

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

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

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

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

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

deploy:debian_clang:
  variables:
    <<: *docker_variables
    <<: *debian_clang
  <<: *deploy
  dependencies:
    - build:debian_clang

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

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

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