image: irmb/virtualfluids-python-deps

stages:
  - build
  - test
  - deploy
  - release

build:
  stage: build

  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
    paths:
      - _skbuild

  artifacts:
    paths:
      - _skbuild/
      - Python/venv

  before_script:
    - python3 -m venv --system-site-packages Python/venv
    - source Python/venv/bin/activate

  script:
    - python3 setup.py install


build_singularity_image:
  stage: build
  rules:
    - if: $CI_COMMIT_TAG

  artifacts:
    paths:
      - Containers/VirtualFluidsOpenMPI.sif

  script:
    - singularity build Containers/VirtualFluidsOpenMPI.sif Containers/VirtualFluidsOpenMPI.def
    - ls -sh Containers/VirtualFluidsOpenMPI.sif


virtual_fluids_basics_test:
  stage: test

  before_script:
    - export VF_UNITTESTS=$(find _skbuild -name basicsTests)

  script:
    - $VF_UNITTESTS


test_python_bindings:
  stage: test

  artifacts:
    when: on_failure
    paths:
      - output/mq/mq10000/

  before_script:
    - export PYTHONPATH="Python"
    - source Python/venv/bin/activate

  script:
    - python3 -m unittest discover -s Python -v

deploy_to_phoenix:
  stage: deploy
  needs: ["build"]

  before_script:
    - pip3 install ansible

  script:
    - ls -l /root/.ssh
    - chmod 600 /root/.ssh/config
    - chown root /root/.ssh/config
    - ls -l /root/.ssh
    - ansible-playbook -i ansible/hosts.cfg ansible/playbook.yml

create_release:
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  stage: release
  needs: ["build_singularity_image"]
  rules:
    - if: $CI_COMMIT_TAG

  script:
    - echo "Creating release with tag $CI_COMMIT_TAG"
    - release-cli create --name "VirtualFluids $CI_COMMIT_TAG" \
      --description "VirtualFluids CFD Simulator" \
      --tag-name "$CI_COMMIT_TAG" \
      --ref "$CI_COMMIT_SHA" \
      --job-token "$CI_JOB_TOKEN" \
      --assets-link="{'name':'VirtualFluidsSingularityImage_OpenMPI','url':'','type':'other','filepath':'Containers/VirtualFluidsOpenMPI.sif'}"