name: Gather Bazel Execution Logs
on:
  workflow_dispatch:
    inputs:
      target:
        description: 'A bazel label representing the test target'
        required: true
        default: '//deps/rabbit:rabbit_stream_queue_SUITE'
jobs:
  run-a:
    name: Run A
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        erlang_version:
        - "26"
        include:
        - erlang_version: "26"
          cache_name: ci-bazel-cache-analysis
    timeout-minutes: 120
    steps:
    - name: CHECKOUT REPOSITORY
      uses: actions/checkout@v4
    - name: CONFIGURE BAZEL
      run: |
        cat << EOF >> user.bazelrc
          build:buildbuddy --remote_header=x-buildbuddy-api-key=${{ secrets.BUILDBUDDY_API_KEY }}

          build:buildbuddy --build_metadata=ROLE=CI
          build:buildbuddy --remote_instance_name=buildbuddy-io/buildbuddy/ci-${{ matrix.cache_name }}
        EOF
    - name: RUN TESTS
      run: |
        sudo sysctl -w net.ipv4.tcp_keepalive_time=60
        sudo ethtool -K eth0 tso off gso off gro off tx off rx off lro off
        bazelisk test ${{ github.event.inputs.target }} \
          --config=rbe-${{ matrix.erlang_version }} \
          --execution_log_binary_file=/tmp/exec.log
    - name: SAVE EXECUTION LOG BINARY
      uses: actions/upload-artifact@v4.3.2
      with:
        name: execution-log-binary-A
        path: /tmp/exec.log
  run-b:
    name: Run B
    needs: run-a
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        erlang_version:
        - "26"
        include:
        - erlang_version: "26"
          cache_name: ci-bazel-cache-analysis
    timeout-minutes: 120
    steps:
    - name: CHECKOUT REPOSITORY
      uses: actions/checkout@v4
    - name: CONFIGURE BAZEL
      run: |
        cat << EOF >> user.bazelrc
          build:buildbuddy --remote_header=x-buildbuddy-api-key=${{ secrets.BUILDBUDDY_API_KEY }}

          build:buildbuddy --build_metadata=ROLE=CI
          build:buildbuddy --remote_instance_name=buildbuddy-io/buildbuddy/ci-${{ matrix.cache_name }}
        EOF
    - name: RUN TESTS
      run: |
        sudo sysctl -w net.ipv4.tcp_keepalive_time=60
        sudo ethtool -K eth0 tso off gso off gro off tx off rx off lro off
        bazelisk test ${{ github.event.inputs.target }} \
          --config=rbe-${{ matrix.erlang_version }} \
          --execution_log_binary_file=/tmp/exec.log
    - name: SAVE EXECUTION LOG BINARY
      uses: actions/upload-artifact@v4.3.2
      with:
        name: execution-log-binary-B
        path: /tmp/exec.log
  parse-logs:
    name: Parse Logs
    needs: [run-a, run-b]
    runs-on: ubuntu-22.04
    steps:
    - name: CHECKOUT BAZEL
      uses: actions/checkout@v4
      with:
        repository: bazelbuild/bazel
        path: bazel
    - name: MOUNT BAZEL CACHE
      uses: actions/cache@v4.0.2
      with:
        path: "/home/runner/.cache/bazel"
        key: bazel
    - name: BUILD EXECLOG PARSER
      working-directory: bazel
      run: |
        bazelisk build src/tools/execlog:parser
    - name: FETCH LOGS
      uses: actions/download-artifact@v4
    - name: PARSE LOGS
      run: |
        bazel/bazel-bin/src/tools/execlog/parser \
          --log_path=./execution-log-binary-A/exec.log \
          --log_path=./execution-log-binary-B/exec.log \
          --output_path=/tmp/execution-log-binary-A.log.txt \
          --output_path=/tmp/execution-log-binary-B.log.txt
    - name: SAVE PARSED LOGS
      uses: actions/upload-artifact@v4.3.2
      with:
        name: parsed-logs
        path: /tmp/execution-log-binary-*.log.txt
