# This workflow will be executed when master is updated:
# it will run the configured tests and upload the results on vfc_ci_master.

name: "Verificarlo CI (master)"

on:
  # Triggers the workflow when master is updated
  push:
    branches: [ master ]

  workflow_dispatch:

permissions:
  contents: read

jobs:
  run_verificarlo_tests:
    runs-on: ubuntu-latest
    container: verificarlo/verificarlo
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791
        with:
            fetch-depth: 0

      - name: Install dependencies
        run: |
          ln -s /usr/bin/python3 /usr/bin/python
          apt update
          apt -y install emacs pkg-config wget libhdf5-dev libblas-dev liblapack-dev

      - name: Install trexio
        run: |
          export VERSION=2.1.0
          wget https://github.com/TREX-CoE/trexio/releases/download/v${VERSION}/trexio-${VERSION}.tar.gz
          tar -zxf trexio-${VERSION}.tar.gz
          cd trexio-${VERSION}
          ./configure --prefix=/usr CC="gcc-7" FC="gfortran-7"
          # modify LDFLAGS to include -lhdf5_hl because autoconf sometime fails to detect the HL component
          make LDFLAGS="-L/usr/lib/x86_64-linux-gnu/hdf5/serial -lhdf5_hl"
          make install

      - name: Run tests
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          vfc_ci test -g -r

      - name: Commit test results
        run: |
          git_hash=$(git rev-parse --short "$GITHUB_SHA")

          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"

          git checkout vfc_ci_master
          mkdir -p vfcruns
          mv *.vfcrun.h5 vfcruns
          git add vfcruns/*
          git commit -m "[auto] New test results for commit ${git_hash}"
          git push

      - name: Upload raw results as artifacts
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: ${{github.sha}}.vfcraw
          path: ./*.vfcraw.h5
