# Install esmvalcore from PyPi on different OS's
# and different Python version; test locally with
# act: https://github.com/nektos/act
# Example how to setup conda workflows:
# https://github.com/marketplace/actions/setup-miniconda
# Notes:
#  - you can group commands with | delimiter (or &&) but those will be run
#    in one single call; declaring the shell variable makes the action run each
#    command separately (better for debugging);
#  - can try multiple shells eg pwsh or cmd /C CALL {0} (but overkill for now!);
# TODO: read the cron tasking documentation:
# https://www.netiq.com/documentation/cloud-manager-2-5/ncm-reference/data/bexyssf.html

name: Install from conda-forge

# runs on a push on main and at the end of every day
on:
  # triggering on push without branch name will run tests everytime
  # there is a push on any branch
  # turn it on only if needed
  push:
    branches:
    - main
  # run the test only if the PR is to main
  # turn it on if required
  # pull_request:
  #   branches:
  #     - main
  schedule:
    - cron: '0 4 * * *'

jobs:
  linux:
    runs-on: "ubuntu-latest"
    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.10"]
      # fail-fast set to False allows all other tests
      # in the worflow to run regardless of any fail
      fail-fast: false
    name: Linux Python ${{ matrix.python-version }}
    steps:
      - uses: conda-incubator/setup-miniconda@v2
        with:
          python-version: ${{ matrix.python-version }}
          miniforge-version: "latest"
          miniforge-variant: Mambaforge
          use-mamba: true
      - shell: bash -l {0}
        run: mkdir -p conda_install_linux_artifacts_python_${{ matrix.python-version }}
      - name: Record versions
        shell: bash -l {0}
        run: |
          mamba --version 2>&1 | tee conda_install_linux_artifacts_python_${{ matrix.python-version }}/conda_version.txt
          which conda 2>&1 | tee conda_install_linux_artifacts_python_${{ matrix.python-version }}/conda_path.txt
          which mamba 2>&1 | tee -a conda_install_linux_artifacts_python_${{ matrix.python-version }}/conda_path.txt
          python -V 2>&1 | tee conda_install_linux_artifacts_python_${{ matrix.python-version }}/python_version.txt
      - name: Install ESMValCore
        shell: bash -l {0}
        run: mamba install esmvalcore 2>&1 | tee conda_install_linux_artifacts_python_${{ matrix.python-version }}/install.txt
      - name: Verify installation
        shell: bash -l {0}
        run: |
          esmvaltool --help
          esmvaltool version 2>&1 | tee conda_install_linux_artifacts_python_${{ matrix.python-version }}/version.txt
      - name: Upload artifacts
        if: ${{ always() }}  # upload artifacts even if fail
        uses: actions/upload-artifact@v2
        with:
          name: Conda_Install_Linux_python_${{ matrix.python-version }}
          path: conda_install_linux_artifacts_python_${{ matrix.python-version }}

  osx:
    runs-on: "macos-latest"
    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.10"]
      fail-fast: false
    name: OSX Python ${{ matrix.python-version }}
    steps:
      - uses: conda-incubator/setup-miniconda@v2
        with:
          python-version: ${{ matrix.python-version }}
          miniforge-version: "latest"
          miniforge-variant: Mambaforge
          use-mamba: true
      - shell: bash -l {0}
        run: mkdir -p conda_install_osx_artifacts_python_${{ matrix.python-version }}
      - name: Record versions
        shell: bash -l {0}
        run: |
          mamba --version 2>&1 | tee conda_install_osx_artifacts_python_${{ matrix.python-version }}/conda_version.txt
          which conda 2>&1 | tee conda_install_osx_artifacts_python_${{ matrix.python-version }}/conda_path.txt
          which mamba 2>&1 | tee -a conda_install_osx_artifacts_python_${{ matrix.python-version }}/conda_path.txt
          python -V 2>&1 | tee conda_install_osx_artifacts_python_${{ matrix.python-version }}/python_version.txt
      - name: Install ESMValCore
        shell: bash -l {0}
        run: mamba install esmvalcore 2>&1 | tee conda_install_osx_artifacts_python_${{ matrix.python-version }}/install.txt
      - name: Verify installation
        shell: bash -l {0}
        run: |
          esmvaltool --help
          esmvaltool version 2>&1 | tee conda_install_osx_artifacts_python_${{ matrix.python-version }}/version.txt
      - name: Upload artifacts
        if: ${{ always() }}  # upload artifacts even if fail
        uses: actions/upload-artifact@v2
        with:
          name: Conda_Install_OSX_python_${{ matrix.python-version }}
          path: conda_install_osx_artifacts_python_${{ matrix.python-version }}
