# caching pnpm deps: https://github.com/actions/setup-node/blob/main/docs/advanced-usage.md#caching-packages-data

name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

env:
  AXIOM_TOKEN: ${{ secrets.TESTING_STAGING_E2E_TOKEN }}
  AXIOM_URL: ${{ secrets.TESTING_STAGING_API_URL }}
  AXIOM_ORG_ID: ${{ secrets.TESTING_STAGING_E2E_ORG_ID }}
  NEXT_PUBLIC_AXIOM_TOKEN: ${{ secrets.TESTING_STAGING_E2E_TOKEN }}
  NEXT_PUBLIC_AXIOM_URL: ${{ secrets.TESTING_STAGING_API_URL }}
  NEXT_PUBLIC_AXIOM_ORG_ID: ${{ secrets.TESTING_STAGING_E2E_ORG_ID }}
  AXIOM_DATASET_SUFFIX: ${{ github.run_id }}
  NODEVERSION: 20.x
  PNPM_VERSION: 9.1.1

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node:
          - 18.x
          - 20.x
          - 22.x
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          cache: 'pnpm'
      - run: pnpm install --frozen-lockfile
      - run: pnpm build && pnpm build:cjs
      - run: pnpm lint
      - run: pnpm test
  
  check-winston-example:
    name: Check Winston example
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node:
          - 18.x
          - 20.x
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          cache: 'pnpm'
      - run: pnpm install --frozen-lockfile
      - run: pnpm build && pnpm build:cjs
      - run: cd examples/winston && pnpm example

  test-integration:
    name: Test (Integration)
    runs-on: ubuntu-latest
    needs: build
    if: github.event.pull_request.head.repo.full_name == github.repository
    strategy:
      matrix:
        node:
          - 18.x
          - 20.x
      max-parallel: 1
      fail-fast: false
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          cache: 'pnpm'
      - run: pnpm install --frozen-lockfile
      - run: pnpm build && pnpm build:cjs
      - env:
          AXIOM_DATASET_SUFFIX: ${{ github.run_id }}-${{ matrix.node }}
        run: pnpm integration

  deploy-e2e-apps:
    name: Deploy E2E apps
    runs-on: ubuntu-latest
    needs: build
    if: github.event.pull_request.head.repo.full_name == github.repository
    steps:
      - uses: actions/checkout@v4
      - uses: amondnet/vercel-action@v25 #deploy
        id: vercel-deploy
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }} # Required
          vercel-org-id: ${{ secrets.VERCEL_ORG_ID}} #Required
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID}} #Required
          scope: ${{ secrets.VERCEL_SCOPE }}
    outputs:
      preview-url: ${{ steps.vercel-deploy.outputs.preview-url }}
    environment:
      name: E2EPreview
      url: ${{ steps.vercel-deploy.outputs.preview-url }}

  test-e2e:
    name: E2E tests
    runs-on: ubuntu-latest
    needs:
      - deploy-e2e-apps
    if: github.event.pull_request.head.repo.full_name == github.repository
    strategy:
      matrix:
        node:
          - 20.x
      max-parallel: 1
      fail-fast: true
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          cache: 'pnpm'
      - run: pnpm install --frozen-lockfile
      - run: pnpm build && pnpm build:cjs # cjs build needed to run tests
      - env:
          TESTING_TARGET_URL: ${{ needs.deploy-e2e-apps.outputs.preview-url }}
        run: pnpm run e2e