# This file is based a sample GitLab CI/CD configuration file that demonstrates a basic 3 stage CI/CD pipeline.
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml
# Python-specific template is located at
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html

# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/
image: python:3.10 #Otherwise use 'latest'

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# https://pip.pypa.io/en/stable/topics/caching/
cache:
  paths:
    - .cache/pip

# Include the Code-Quality template
include:
   - template: Jobs/Code-Quality.gitlab-ci.yml

before_script:
  - python --version ; pip --version  # For debugging
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate
  - pip install --upgrade pip setuptools wheel

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy


build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - pip install h5py --only-binary h5py
    - pip install -e .
    - echo "Compile complete."
  artifacts:
    paths:
      - build/*

unit-test-job:   # This job runs in the test stage.
  stage: test
  script:
    - echo "Running unit tests..."
    - pip install pytest
    - pip install pytest-cov
    - pip install -e .
    - pytest --junitxml=report.xml
  artifacts:
    paths:
      - report.xml
    reports:
      junit: report.xml
  rules:
    - changes:
       - src/**/*
       - tests/**/*
      when: always

# Build the pages
# All necessary files need to be moved or build in the 'public' folder
pages:
  stage: deploy
  script:
  # Do all installations
  - apt update
  - apt-get -y install graphviz
  - apt-get -y install dvipng texlive-latex-extra texlive-fonts-recommended cm-super
  - pip install -U sphinx m2r2 furo pyUML pydot==2.0.0 gprof2dot latex
  - pip install graphviz
  - pip install -e .
  # Generate the new directories
  - mkdir public
  - cd public
  - mkdir diagrams
  - mkdir examples
  - cd examples
  - mkdir user_guide
  - cd ../../
  # Copy and build the diagrams
  - cp docs/diagrams/* public/diagrams
  - cd public/diagrams
  - python class_diagram.py
  - cd ../../
  # Copy and build the needed examples
  - cp examples/user_guide/* public/examples/user_guide
  - cd public/examples/user_guide
  - python example_user_guide.py
  - cd ../../../
  # Build the html-pages
  - sphinx-build -b html docs/source public
  artifacts:
    paths:
    - public
  only:
  - master


deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  environment: production
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."
