From a67e67eadd7d3cde9742360c15156feb19da120d Mon Sep 17 00:00:00 2001
From: Kristoffer Andersson <kristoffer.andersson@gu.se>
Date: Mon, 9 Dec 2024 10:56:40 +0100
Subject: [PATCH] ci: fix benchmark workflows

---
 .github/workflows/benchmarks_base.yml          |  1 +
 .github/workflows/benchmarks_fork_pr_run.yml   | 10 +++++++++-
 .github/workflows/benchmarks_fork_pr_track.yml |  6 ++++++
 .github/workflows/benchmarks_pr.yml            |  7 +++++++
 4 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/benchmarks_base.yml b/.github/workflows/benchmarks_base.yml
index a452f5f..e3045b0 100644
--- a/.github/workflows/benchmarks_base.yml
+++ b/.github/workflows/benchmarks_base.yml
@@ -43,6 +43,7 @@ jobs:
       - uses: bencherdev/bencher@main
       - name: Track base branch benchmarks with Bencher
         run: |
+          source .venv/bin/activate
           bencher run \
           --project json-arrays-py \
           --token '${{ secrets.BENCHER_API_TOKEN }}' \
diff --git a/.github/workflows/benchmarks_fork_pr_run.yml b/.github/workflows/benchmarks_fork_pr_run.yml
index 053cbae..43aba84 100644
--- a/.github/workflows/benchmarks_fork_pr_run.yml
+++ b/.github/workflows/benchmarks_fork_pr_run.yml
@@ -8,9 +8,17 @@ env:
   MINIMUM_PYTHON_VERSION: "3.9"
   UV_VERSION: "0.5.1"
 
+# If new code is pushed to a PR branch, then cancel in progress workflows for that PR. Ensures that
+# we don't waste CI time, and returns results quicker https://github.com/jonhoo/rust-ci-conf/pull/5
+concurrency:
+  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+  cancel-in-progress: true
+
 jobs:
   benchmark_fork_pr_branch:
     name: ubuntu / ${{ matrix.python-version }} / run fork pr benchmarks
+    # DO NOT REMOVE: For handling Fork PRs see Pull Requests from Forks
+    if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository
     runs-on: ubuntu-latest
     strategy:
       max-parallel: 4
@@ -44,7 +52,7 @@ jobs:
       - name: Upload Benchmark Results
         uses: actions/upload-artifact@v4
         with:
-          name: benchmark_results.json
+          name: benchmark_results-${{ matrix.python-version }}.json
           path: ./benchmark_results-${{ matrix.python-version }}.json
       - name: Upload GitHub Pull Request Event
         uses: actions/upload-artifact@v4
diff --git a/.github/workflows/benchmarks_fork_pr_track.yml b/.github/workflows/benchmarks_fork_pr_track.yml
index e9b3db2..3c47477 100644
--- a/.github/workflows/benchmarks_fork_pr_track.yml
+++ b/.github/workflows/benchmarks_fork_pr_track.yml
@@ -5,6 +5,12 @@ on:
     workflows: [benchmarks_fork_pr_run]
     types: [completed]
 
+# If new code is pushed to a PR branch, then cancel in progress workflows for that PR. Ensures that
+# we don't waste CI time, and returns results quicker https://github.com/jonhoo/rust-ci-conf/pull/5
+concurrency:
+  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+  cancel-in-progress: true
+
 jobs:
   track_fork_pr_branch:
     name: ubuntu / ${{ matrix.python-version }} / track fork prs with bencher
diff --git a/.github/workflows/benchmarks_pr.yml b/.github/workflows/benchmarks_pr.yml
index 729387e..3076c89 100644
--- a/.github/workflows/benchmarks_pr.yml
+++ b/.github/workflows/benchmarks_pr.yml
@@ -3,6 +3,12 @@ on:
     types: [opened, reopened, edited, synchronize]
 name: benchmarks_pr
 
+# If new code is pushed to a PR branch, then cancel in progress workflows for that PR. Ensures that
+# we don't waste CI time, and returns results quicker https://github.com/jonhoo/rust-ci-conf/pull/5
+concurrency:
+  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+  cancel-in-progress: true
+
 env:
   MINIMUM_PYTHON_VERSION: "3.9"
   UV_VERSION: "0.5.1"
@@ -44,6 +50,7 @@ jobs:
       - uses: bencherdev/bencher@main
       - name: Track PR Benchmarks with Bencher
         run: |
+          source .venv/bin/activate
           bencher run \
           --project json-arrays-py \
           --token '${{ secrets.BENCHER_API_TOKEN }}' \