name: Benchmark

# only one can tun at a time.
# Actions access a common cache entry and may corrupt it.
concurrency: cd-benchmark-${{ github.ref }}

on:
  push:
    branches:
      - stable
      - unstable
      - benchmark # For debugging
  pull_request:
    branches:
      - stable
      - unstable

jobs:
  run:
    runs-on: ubuntu-latest
    # Skip for dependabot PRs, secrets are not made available and will fail
    # Don't run on forks. Forks don't have access to the S3 credentials and the workflow will fail
    if: |
      github.actor != 'dependabot[bot]' &&
      ( github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository )

    steps:
      # <common-build> - Uses YAML anchors in the future
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 18
      - name: Node.js version
        id: node
        run: echo "v8CppApiVersion=$(node --print "process.versions.modules")" >> $GITHUB_OUTPUT
      - name: Restore dependencies
        uses: actions/cache@master
        id: cache-deps
        with:
          path: |
            node_modules
            packages/*/node_modules
          key: ${{ runner.os }}-${{ steps.node.outputs.v8CppApiVersion }}-${{ hashFiles('**/yarn.lock', '**/package.json') }}
      - name: Install & build
        if: steps.cache-deps.outputs.cache-hit != 'true'
        run: yarn install --frozen-lockfile --ignore-optional && yarn build
      - name: Build
        run: yarn build
        if: steps.cache-deps.outputs.cache-hit == 'true'
      # </common-build>

      # Restore performance downloaded states
      - name: Restore performance state cache
        uses: actions/cache@master
        with:
          path: packages/state-transition/test-cache
          key: perf-states-${{ hashFiles('packages/state-transition/test/perf/params.ts') }}

      - name: Run benchmarks
        run: yarn benchmark
        env:
          # To download content for tests
          INFURA_ETH2_CREDENTIALS: ${{ secrets.INFURA_ETH2_CREDENTIALS }}
          # To write to PRs and commits
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          # benchmark options
          BENCHMARK_S3: true
          # S3 credentials
          S3_ACCESS_KEY: ${{ secrets.S3_BENCH_LODESTAR_ACCESS_KEY }}
          S3_SECRET_KEY: ${{ secrets.S3_BENCH_LODESTAR_SECRET_KEY }}
          S3_REGION: ${{ secrets.S3_BENCH_LODESTAR_REGION }}
          S3_BUCKET: ${{ secrets.S3_BENCH_LODESTAR_BUCKET }}
          S3_ENDPOINT: ${{ secrets.S3_BENCH_LODESTAR_ENDPOINT }}
          # Key prefix to separate benchmark data from multiple repositories
          S3_KEY_PREFIX: ${{ github.repository }}/${{ runner.os }}
