name: Test CI

on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: rpi5

    env:
      DOCKER_VOLUME: ${{ vars.DOCKER_VOLUME }}

    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Check PWD
      run: |
        echo "Docker volume: $DOCKER_VOLUME"
        echo "PWD: $PWD"
        ls -lRa ./.yarn

    - name: Validate Node and Yarn Environment
      run: |
        if ! command -v node &> /dev/null
        then
          echo "Error: Node.js not found"
          exit 1
        fi
        echo "Node.js version: $(node -v)"

        if ! command -v yarn &> /dev/null
        then
          echo "Error: Yarn.js not found"
          exit 1
        fi
        echo "yarn version: $(yarn -v)"

    - name: Restore Yarn cache
      uses: actions/cache@v4
      with:
        path: |
          .yarn/cache
          .yarn/unplugged
          .yarn/install-state.gz
          .pnp.cjs
        key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock','package.json') }}

    - name: Install Dependencies with yarn
      run: |
        yarn install --immutable
        ls .

    - name: Build Nestjs project
      run: |
        yarn build
        ls .

    - name: Deploy dist
      run: |
        cp -r dist $DOCKER_VOLUME/scheduler/back/
        cp -r node_modules $DOCKER_VOLUME/scheduler/back/
        ls $DOCKER_VOLUME/scheduler/back
        docker exec -it scheduler_back pm2 reload scheduler-back