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:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - apt-get install -y rsync
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - echo $SSH_PRIVATE_KEY >> ansible/private_key
    - ssh-keyscan -t rsa phoenix.hlr.rz.tu-bs.de >> ~/.ssh/known_hosts
    - pip3 install ansible

  script:
    - ansible-playbook -i ansible/hosts.cfg -u $REMOTE_USER ansible/playbook.yml

  parallel:
    matrix:
      - ANSIBLE_MATRIX: 0
        REMOTE_USER:
          - y0054816

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'}"
    - build/bin/basicsTests


###############################################################################
##                                                                           ##
##    Deploy: Code analysis                                                  ##
##                                                                           ##
###############################################################################

clangBuildAnalyzer:
  stage: test

  rules:
    - when: manual

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - cmake --version
    - mpirun --version
    - export CC=clang
    - export CXX=clang++
    - $CXX --version
    - cd /tmp
    - git clone https://github.com/aras-p/ClangBuildAnalyzer.git
    - cd ClangBuildAnalyzer
    - cmake .
    - make
    - export PATH+=:$(pwd)

  script:
    - mkdir $CI_PROJECT_DIR/build
    - cd $CI_PROJECT_DIR/build
    - cmake .. -DBUILD_VF_CPU=ON -DUSE_OPENMP=OFF -DCMAKE_CXX_FLAGS=-ftime-trace
    - ClangBuildAnalyzer --start .
    - make
    - ClangBuildAnalyzer --stop . CBA
    - ClangBuildAnalyzer --analyze CBA


include_what_you_use:
  stage: test

  rules:
    - when: manual

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - cd /tmp
    - git clone https://github.com/include-what-you-use/include-what-you-use.git
    - cd include-what-you-use
    - git checkout clang_10
    - cmake . -DCMAKE_PREFIX_PATH=/usr/lib/llvm-10
    - make
    - export PATH+=:$(pwd)/bin

  script:
    - mkdir $CI_PROJECT_DIR/build
    - cd $CI_PROJECT_DIR/build
    - cmake .. -DBUILD_VF_CPU=ON -DUSE_OPENMP=OFF -DBUILD_VF_INCLUDE_WHAT_YOU_USE=ON
    - make


cppcheck:
  stage: test

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - apt-get install -y cppcheck

  script:
    - cd $CI_PROJECT_DIR
    - cppcheck --version
    - cppcheck src --enable=all --xml 2> report.xml
    - cppcheck-htmlreport --file=report.xml --report-dir=html_report --source-dir=.

  artifacts:
    paths:
      - html_report/


deploy_cppcheck_to_elladan:
  stage: deploy
  needs: ["cppcheck"]

  before_script:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - apt-get install -y rsync
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY_ELLADAN" | tr -d '\r' | ssh-add -
    - ssh-keyscan -t rsa elladan.irmb.bau.tu-bs.de >> ~/.ssh/known_hosts
    - pip3 install ansible

  script:
    - ansible-playbook -i ansible/hosts.cfg -u public_pages ansible/playbook_cppcheck.yml


lizard:
  stage: test

  before_script:
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update
    - cd /tmp
    - git clone https://github.com/terryyin/lizard.git
    - cd lizard
    - python3 setup.py install
    - which lizard
    - lizard --version

  script:
    - cd $CI_PROJECT_DIR
    - lizard -l cpp apps/cpu/LaminarTubeFlow
    - ls -la