#
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
# Copyright (C) 2019 Henrik tom Wörden
# Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

variables:
  CI_REGISTRY_IMAGE: $CI_REGISTRY/caosdb/src/caosdb-cpplib/testenv:latest
  TRIGGERED_BY_REPO: CPPINTTEST
  TRIGGERED_BY_REF: $CI_COMMIT_REF_NAME
  TRIGGERED_BY_HASH: $CI_COMMIT_SHORT_SHA

stages:
  - setup
  - test

# Build a docker image in which tests for this repository can run
build-testenv:
  tags: [cached-dind]
  image: docker:20.10.6
  stage: setup
  timeout: 2h
  needs: []
  script:
    - echo "Pipeline triggered by $TRIGGERED_BY_REPO@$TRIGGERED_BY_REF ($TRIGGERED_BY_HASH)"
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker pull $CI_REGISTRY_IMAGE|| true
    - docker build
      --build-arg CI_REGISTRY_IMAGE=$CI_REGISTRY_IMAGE
      --file .docker/Dockerfile
      --pull
      --cache-from $CI_REGISTRY_IMAGE
      --tag $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE


# run integration tests
test:
  dependencies:
    - build-testenv
  tags: [ docker ]
  stage: test
  script:
    - mkdir build
    - cd build
    - cmake ..
    - cmake --build .
    - ctest
