## Use workflows from ninenines/ci.erlang.mk to test Cowlib.

name: Check Cowlib

on:
  push:
    branches:
      - master
  pull_request:
  schedule:
      ## Every Monday at 2am.
      - cron: 0 2 * * 1

env:
  CI_ERLANG_MK: 1

jobs:
  cleanup-master:
    name: Cleanup master build
    runs-on: ubuntu-latest
    steps:

    - name: Cleanup master build if necessary
      if: ${{ github.event_name == 'schedule' }}
      run: |
        gh extension install actions/gh-actions-cache
        gh actions-cache delete Linux-X64-Erlang-master -R $REPO --confirm || true
        gh actions-cache delete macOS-X64-Erlang-master -R $REPO --confirm || true
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        REPO: ${{ github.repository }}

  check:
    name: Cowlib
    needs: cleanup-master
    uses: ninenines/ci.erlang.mk/.github/workflows/ci.yaml@master

# The perfs tests are nice to run but typically not
# important. So we run them after we are done with the other
# test suites. At this point we know that Erlang was built
# so we can just use the latest version.

  perfs:
    name: Run performance tests
    needs: check
    runs-on: 'ubuntu-latest'
    if: ${{ !cancelled() }}
    steps:

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

    - name: Output latest Erlang/OTP version
      id: latest_version
      run: |
        {
          echo "latest<<EOF"
          make ci-list | grep -v rc | grep -v master | tail -n1
          echo EOF
        } >> "$GITHUB_OUTPUT"

    - name: Restore CI cache
      uses: actions/cache/restore@v4
      with:
        path: |
          ~/erlang/
        key: ${{ runner.os }}-${{ runner.arch }}-Erlang-${{ steps.latest_version.outputs.latest }}

    - name: Run perfs
      run: make perfs LATEST_ERLANG_OTP=1
