name: Continuous benchmark

on:
  workflow_dispatch:
  pull_request: 
    branches:
      - main
    paths:
      - 'packages/astro/src/**/*.ts'
      - 'benchmark/**'
  push:
    branches:
      - main
    paths:
      - 'packages/astro/src/**/*.ts'
      - 'benchmark/**'

permissions: {}

env:
  TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
  TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
  FORCE_COLOR: true
  CODSPEED: true

jobs:
  codspeed:
    if: ${{ github.repository_owner == 'withastro' }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        shard: [ "1/4", "2/4", "3/4", "4/4" ]
    permissions:
      contents: read # Checkout only
      pull-requests: write # CodSpeed posts benchmark results as PR comment
    steps:
      - name: Check out repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Setup PNPM
        uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6.0.8

      - name: Setup Node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 24.15.0
          cache: "pnpm"

      - name: Install dependencies
        run: pnpm install

      - name: Build
        run: pnpm run build
        
      - name: Setup benchmark projects
        working-directory: ./benchmark
        run: pnpm run build:bench
        timeout-minutes: 15
        
      - name: Run the benchmarks
        uses: CodSpeedHQ/action@658a901452bb54c799643e060733b7afe9121b8d # v4.14.0
        timeout-minutes: 30
        with:
          working-directory: ./benchmark
          run: pnpm bench --shard=${{ matrix.shard }}
          token: ${{ secrets.CODSPEED_TOKEN }}
          mode: simulation
