Files
iroh/.github/workflows/pick-runner.yml
Asmir Avdicevic c08ccf80af chore(ci): introduce alt runners (#4164)
## Description

We've signed up with blacksmith for a trial run to test out if the added
bandwidth helps our queue times. I've selectively converted some but not
all of the jobs. Will do similar on noq.

## Breaking Changes

<!-- Optional, if there are any breaking changes document them,
including how to migrate older code. -->

## Notes & open questions

<!-- Any notes, remarks or open questions you have to make about the PR.
-->

## Change checklist
<!-- Remove any that are not relevant. -->
- [ ] Self-review.
- [ ] Documentation updates following the [style
guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text),
if relevant.
- [ ] Tests if relevant.
- [ ] All breaking changes documented.
- [ ] List all breaking changes in the above "Breaking Changes" section.
- [ ] Open an issue or PR on any number0 repos that are affected by this
breaking change. Give guidance on how the updates should be handled or
do the actual updates themselves. The major ones are:
    - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc)
    - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip)
    - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs)
    - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe)
    - [ ] [`sendme`](https://github.com/n0-computer/sendme)

<!-- codesmith:footer -->
---
<a
href="https://app.blacksmith.sh/n0-computer/codesmith/iroh/pr/4164"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-in-codesmith-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-in-codesmith-light.svg"><img
alt="View in Codesmith"
src="https://pr-comments-assets.blacksmith.sh/codesmith/view-in-codesmith-dark.svg"></picture></a>
<sup>Codesmith can help with this PR — just tag <code>@codesmith</code>
or enable autofix.</sup>

- [ ] Autofix CI and bot reviews
<!-- /codesmith:footer -->
2026-04-23 15:47:49 +02:00

256 lines
11 KiB
YAML

# Reusable workflow: pick-runner — live-availability overflow routing
# with per-slot spreading.
#
# Lives at `.github/workflows/pick-runner.yml` in `n0-computer/tools`.
#
# HOW ROUTING WORKS:
# pick-runner queries self-hosted linux X64 idle count once at dispatch
# time, then emits up to 16 per-slot runs-on arrays (`overflow_1` through
# `overflow_16`). Each caller job picks a slot number; the first `idle`
# slots resolve to self-hosted, the rest resolve to Blacksmith-4vcpu.
#
# Example, with idle=2 and 5 overflow jobs (slots 1..5):
# slot 1 → self-hosted (1 <= 2)
# slot 2 → self-hosted (2 <= 2)
# slot 3 → Blacksmith (3 > 2)
# slot 4 → Blacksmith
# slot 5 → Blacksmith
#
# MIN_FREE_RUNNERS acts as a quality gate: if idle < min_free_runners, we
# don't spread at all and send the whole batch to Blacksmith. This avoids
# stranding a single job on a nearly-full self-hosted pool where the batch
# wall-clock would be dominated by that one job's queue wait.
#
# CALLER PATTERN:
# jobs:
# pick-runner:
# uses: n0-computer/tools/.github/workflows/pick-runner.yml@main
# with:
# min_free_runners: 3
# secrets:
# RUNNER_READ_TOKEN: ${{ secrets.RUNNER_READ_TOKEN }}
#
# clippy_check:
# needs: pick-runner
# runs-on: ${{ fromJSON(needs.pick-runner.outputs.overflow_1) }}
# ...
#
# check_docs:
# needs: pick-runner
# runs-on: ${{ fromJSON(needs.pick-runner.outputs.overflow_2) }}
# ...
#
# Assign slot numbers 1..N to the overflow jobs in your workflow. Slot
# assignment is arbitrary (jobs run in parallel regardless) — it just
# determines the routing split.
#
# PREREQUISITES:
# 1. Shared repo accessible to callers: Settings → Actions → General →
# Access: "Accessible from repositories in the 'n0-computer' organization"
# on the tools repo.
# 2. RUNNER_READ_TOKEN secret (per-repo or at org level scoped to callers).
# Required permission for GET /orgs/{org}/actions/runners:
# - Fine-grained PAT: Organization permissions →
# "Self-hosted runners: Read" ← recommended
# - Classic PAT: `admin:org` scope (broader; use if fine-grained tokens
# aren't available — note `read:org` alone is NOT sufficient for
# this endpoint, despite the name).
# - GitHub App: Organization permissions → Self-hosted runners: Read.
# Without the secret, pick-runner falls back to all-self-hosted (safe).
name: Pick runner (reusable, per-slot)
on:
workflow_call:
inputs:
org:
description: GitHub org that owns the self-hosted runners
type: string
default: n0-computer
min_free_runners:
description: >
Below this idle count, don't spread — send the whole batch to
Blacksmith. Avoids stranding a single job behind a slow self-hosted
queue. Set to 1 to always use any idle self-hosted slot.
type: number
default: 3
core_runner:
description: JSON array for the `core` class (always this)
type: string
default: '["self-hosted","linux","X64"]'
overflow_runner:
description: JSON array for overflow slots that land on Blacksmith
type: string
default: '["blacksmith-4vcpu-ubuntu-2204"]'
max_blacksmith_concurrent:
description: >
Soft cap on total Blacksmith runners busy across the org. If the
current busy count is >= this value, this batch is forced to
self-hosted regardless of idle count. Prevents unbounded overflow
spend when multiple repos spill simultaneously. Set to 0 to disable.
type: number
default: 10
outputs:
core:
description: JSON array for `runs-on` — always self-hosted
value: ${{ jobs.pick.outputs.core }}
idle:
description: Idle self-hosted linux X64 runners at decision time
value: ${{ jobs.pick.outputs.idle }}
decision:
description: Human-readable routing summary (for logs/debugging)
value: ${{ jobs.pick.outputs.decision }}
# Per-slot overflow outputs. Caller picks a slot per job.
overflow_1:
description: overflow slot 1 (lowest — most likely self-hosted)
value: ${{ jobs.pick.outputs.overflow_1 }}
overflow_2:
description: overflow slot 2
value: ${{ jobs.pick.outputs.overflow_2 }}
overflow_3:
description: overflow slot 3
value: ${{ jobs.pick.outputs.overflow_3 }}
overflow_4:
description: overflow slot 4
value: ${{ jobs.pick.outputs.overflow_4 }}
overflow_5:
description: overflow slot 5
value: ${{ jobs.pick.outputs.overflow_5 }}
overflow_6:
description: overflow slot 6
value: ${{ jobs.pick.outputs.overflow_6 }}
overflow_7:
description: overflow slot 7
value: ${{ jobs.pick.outputs.overflow_7 }}
overflow_8:
description: overflow slot 8
value: ${{ jobs.pick.outputs.overflow_8 }}
overflow_9:
description: overflow slot 9
value: ${{ jobs.pick.outputs.overflow_9 }}
overflow_10:
description: overflow slot 10
value: ${{ jobs.pick.outputs.overflow_10 }}
overflow_11:
description: overflow slot 11
value: ${{ jobs.pick.outputs.overflow_11 }}
overflow_12:
description: overflow slot 12
value: ${{ jobs.pick.outputs.overflow_12 }}
overflow_13:
description: overflow slot 13
value: ${{ jobs.pick.outputs.overflow_13 }}
overflow_14:
description: overflow slot 14
value: ${{ jobs.pick.outputs.overflow_14 }}
overflow_15:
description: overflow slot 15
value: ${{ jobs.pick.outputs.overflow_15 }}
overflow_16:
description: overflow slot 16
value: ${{ jobs.pick.outputs.overflow_16 }}
secrets:
RUNNER_READ_TOKEN:
description: PAT with read:org scope for runner availability query
required: false
jobs:
pick:
name: Pick runner (live availability, per-slot)
runs-on: ubuntu-latest
timeout-minutes: 2
outputs:
core: ${{ steps.p.outputs.core }}
idle: ${{ steps.p.outputs.idle }}
blacksmith_busy: ${{ steps.p.outputs.blacksmith_busy }}
decision: ${{ steps.p.outputs.decision }}
overflow_1: ${{ steps.p.outputs.overflow_1 }}
overflow_2: ${{ steps.p.outputs.overflow_2 }}
overflow_3: ${{ steps.p.outputs.overflow_3 }}
overflow_4: ${{ steps.p.outputs.overflow_4 }}
overflow_5: ${{ steps.p.outputs.overflow_5 }}
overflow_6: ${{ steps.p.outputs.overflow_6 }}
overflow_7: ${{ steps.p.outputs.overflow_7 }}
overflow_8: ${{ steps.p.outputs.overflow_8 }}
overflow_9: ${{ steps.p.outputs.overflow_9 }}
overflow_10: ${{ steps.p.outputs.overflow_10 }}
overflow_11: ${{ steps.p.outputs.overflow_11 }}
overflow_12: ${{ steps.p.outputs.overflow_12 }}
overflow_13: ${{ steps.p.outputs.overflow_13 }}
overflow_14: ${{ steps.p.outputs.overflow_14 }}
overflow_15: ${{ steps.p.outputs.overflow_15 }}
overflow_16: ${{ steps.p.outputs.overflow_16 }}
steps:
- id: p
env:
MIN_FREE_RUNNERS: ${{ inputs.min_free_runners }}
MAX_BLACKSMITH_CONCURRENT: ${{ inputs.max_blacksmith_concurrent }}
CORE_RUNNER: ${{ inputs.core_runner }}
OVERFLOW_RUNNER: ${{ inputs.overflow_runner }}
ORG: ${{ inputs.org }}
GH_TOKEN: ${{ secrets.RUNNER_READ_TOKEN || github.token }}
shell: bash
run: |
set -euo pipefail
echo "core=${CORE_RUNNER}" >> "$GITHUB_OUTPUT"
# One paginated fetch, counted twice. Avoid a second round-trip.
RUNNERS_JSON="$(gh api "/orgs/${ORG}/actions/runners" --paginate 2>/dev/null || echo '')"
IDLE="$(echo "$RUNNERS_JSON" | jq -s '[.[] | .runners[]?]
| [.[] | select(.status=="online" and .busy==false
and ([.labels[].name | ascii_downcase]
| contains(["self-hosted","linux","x64"])))]
| length' 2>/dev/null || echo "")"
# Count busy runners whose labels include any "blacksmith-" prefixed
# label. Blacksmith runners are ephemeral — they only exist while
# executing a job, so busy==true is always the relevant state.
BS_BUSY="$(echo "$RUNNERS_JSON" | jq -s '[.[] | .runners[]?]
| [.[] | select(.busy==true
and ([.labels[].name | ascii_downcase]
| any(startswith("blacksmith-"))))]
| length' 2>/dev/null || echo "")"
# Decide how many slots get self-hosted.
#
# API failed -> all self-hosted (safe default)
# blacksmith at cap -> all self-hosted (prevent runaway spend)
# idle < min threshold -> all Blacksmith (don't strand jobs)
# idle >= min threshold -> first `idle` slots self-hosted, rest Blacksmith
if [[ -z "$IDLE" || ! "$IDLE" =~ ^[0-9]+$ ]]; then
echo "::warning::runner availability query failed; defaulting to all self-hosted"
IDLE="unknown"
BS_BUSY="unknown"
SELF_HOSTED_SLOTS=16
ROUTE="all self-hosted (api-failed)"
elif (( MAX_BLACKSMITH_CONCURRENT > 0 )) && [[ "$BS_BUSY" =~ ^[0-9]+$ ]] && (( BS_BUSY >= MAX_BLACKSMITH_CONCURRENT )); then
SELF_HOSTED_SLOTS=16
ROUTE="all self-hosted (blacksmith at cap: bs_busy=${BS_BUSY} >= max=${MAX_BLACKSMITH_CONCURRENT})"
elif (( IDLE < MIN_FREE_RUNNERS )); then
SELF_HOSTED_SLOTS=0
ROUTE="all Blacksmith (idle=${IDLE} < min=${MIN_FREE_RUNNERS})"
else
SELF_HOSTED_SLOTS="$IDLE"
ROUTE="spread (idle=${IDLE}: first ${IDLE} slots self-hosted, rest Blacksmith)"
fi
for i in $(seq 1 16); do
if (( i <= SELF_HOSTED_SLOTS )); then
echo "overflow_${i}=${CORE_RUNNER}" >> "$GITHUB_OUTPUT"
else
echo "overflow_${i}=${OVERFLOW_RUNNER}" >> "$GITHUB_OUTPUT"
fi
done
echo "decision=${ROUTE}" >> "$GITHUB_OUTPUT"
echo "idle=${IDLE}" >> "$GITHUB_OUTPUT"
echo "blacksmith_busy=${BS_BUSY}" >> "$GITHUB_OUTPUT"
echo "--------------------------------------------------------"
echo "Idle self-hosted linux-X64: ${IDLE} (min threshold: ${MIN_FREE_RUNNERS})"
echo "Blacksmith runners busy: ${BS_BUSY} (max: ${MAX_BLACKSMITH_CONCURRENT})"
echo "Self-hosted slots: 1..${SELF_HOSTED_SLOTS}"
echo "Blacksmith slots: $((SELF_HOSTED_SLOTS+1))..16"
echo "Decision: ${ROUTE}"
echo "--------------------------------------------------------"