name: CI
on:
  push:
    branches:
      - main
      - 'release/**'
  pull_request:
    branches:
      - '**'
    types:
      - opened
      - reopened
      - synchronize
      - ready_for_review
    
  workflow_dispatch:

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: ${{ !contains(fromJSON('[
      "refs/heads/release/2.60",
      "refs/heads/release/2.61",
      "refs/heads/main"
    ]'), github.ref) }}

jobs:
  tests:
    if: ${{ github.event_name == 'push' || !github.event.pull_request.draft }}
    strategy:
      matrix:
        # list of os: https://github.com/actions/virtual-environments
        os:
          - ubuntu-22.04
          - macos-14
    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-go@v5
        with:
          go-version: '1.22'
          cache: ${{ contains(fromJSON('[
            "refs/heads/release/2.60",
            "refs/heads/release/2.61",
            "refs/heads/main"
            ]'), github.ref) }}

      - name: Install dependencies on Linux
        if: runner.os == 'Linux'
        run: sudo apt update && sudo apt install build-essential

      - name: Build
        run: make all

      - name: Reproducible build test
        run: |
          make erigon
          shasum -a256 ./build/bin/erigon > erigon1.sha256
          make erigon
          shasum -a256 ./build/bin/erigon > erigon2.sha256
          if ! cmp -s erigon1.sha256 erigon2.sha256; then
            echo >&2 "Reproducible build broken"; cat erigon1.sha256; cat erigon2.sha256; exit 1
          fi

      - name: Test
        run: make test

      - name: SonarCloud
        if: runner.os == 'Linux'
        uses: SonarSource/sonarcloud-github-action@v2.3.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        continue-on-error: true

  tests-windows:
    if: ${{ github.event_name == 'push' || !github.event.pull_request.draft }}
    strategy:
      matrix:
        os: [ windows-2022 ]
    runs-on: ${{ matrix.os }}

    steps:
      - name: configure Pagefile
        uses: al-cheb/configure-pagefile-action@v1.4
        with:
          minimum-size: 8GB
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: '1.22'
          cache: ${{ contains(fromJSON('[
            "refs/heads/release/2.60",
            "refs/heads/release/2.61",
            "refs/heads/main"
            ]'), github.ref) }}

      - uses: actions/cache@v4
        with:
          path: |
            C:\ProgramData\chocolatey\lib\mingw
            C:\ProgramData\chocolatey\lib\cmake
          key: chocolatey-${{ matrix.os }}
      - name: Install dependencies
        run: |
          choco upgrade mingw -y --no-progress --version 13.2.0

      - name: Build
        run: .\wmake.ps1 all

      - name: Test
        run: .\wmake.ps1 test

      - name: Test erigon-lib
        run: cd erigon-lib && make test-no-fuzz