diff --git a/.github/actions/runner-select/VENDORED_FROM b/.github/actions/runner-select/VENDORED_FROM new file mode 100644 index 0000000..95cfe2e --- /dev/null +++ b/.github/actions/runner-select/VENDORED_FROM @@ -0,0 +1,3 @@ +33c44bc27724e71095a11d15d4aa145ba4890c53 +Temporary copy. invertase/mellifera is private and this repository is public. +Remove this directory when a public runner-select pin is available. diff --git a/.github/actions/runner-select/action.yml b/.github/actions/runner-select/action.yml new file mode 100644 index 0000000..a8ac8c0 --- /dev/null +++ b/.github/actions/runner-select/action.yml @@ -0,0 +1,79 @@ +name: Mellifera runner select +description: >- + Applies the Mellifera GHA placement pool test and outputs the runs-on label JSON + for the job (hosted default, overflow spill, or safe default on failure). +inputs: + mode: + description: >- + off, fixed, or github-app. Empty reads RUNNER_SELECT_MODE. Empty, off, or + unknown emits the hosted label. + required: false + default: '' + default-runs-on: + description: >- + Hosted label string or JSON array. Empty reads MELLIFERA_DEFAULT_RUNS_ON_MACOS + or MELLIFERA_DEFAULT_RUNS_ON_LINUX by class, then MELLIFERA_DEFAULT_RUNS_ON. + required: false + default: '' + class: + description: macos or other + required: true + placement-url: + description: >- + Full GET URL for /v1/gha/placement (repo and label query included). Empty + reads the full-URL env for the overflow label, then MELLIFERA_PLACEMENT_URL. + required: false + default: '' + placement-token: + description: >- + Bearer token for placement (never logged). Empty reads MELLIFERA_PLACEMENT_TOKEN. + required: false + default: '' + placement-ca: + description: >- + Optional PEM text of the CA that signs the placement host certificate. Empty + reads MELLIFERA_PLACEMENT_CA, then keeps system trust. When set, curl trusts + only this CA. Never logged. + required: false + default: '' + overflow-runs-on: + description: >- + Overflow label string or JSON array when headroom allows spill. Empty reads + MELLIFERA_OVERFLOW_RUNS_ON_MACOS or MELLIFERA_OVERFLOW_RUNS_ON_LINUX when set, + otherwise macos-self-hosted or linux-self-hosted by class. + required: false + default: '' +outputs: + runs-on: + description: JSON string or array for fromJSON() in the workflow + value: ${{ steps.select.outputs.runs-on }} + pool: + description: hosted, overflow, or default + value: ${{ steps.select.outputs.pool }} + reason: + description: >- + off, hosted, overflow, default, error, missing-token, bad-ca, curl-failed, + non-200, or bad-json + value: ${{ steps.select.outputs.reason }} + summary: + description: One-line route decision + value: ${{ steps.select.outputs.summary }} +runs: + using: composite + steps: + - id: select + shell: bash + env: + INPUT_MODE: ${{ inputs.mode }} + INPUT_DEFAULT_RUNS_ON: ${{ inputs.default-runs-on }} + INPUT_CLASS: ${{ inputs.class }} + INPUT_PLACEMENT_URL: ${{ inputs.placement-url }} + INPUT_PLACEMENT_TOKEN: ${{ inputs.placement-token }} + INPUT_PLACEMENT_CA: ${{ inputs.placement-ca }} + INPUT_OVERFLOW_RUNS_ON: ${{ inputs.overflow-runs-on }} + run: | + set -euo pipefail + mapfile -t _lines < <(bash "${{ github.action_path }}/select.sh") + for _line in "${_lines[@]}"; do + echo "$_line" >> "$GITHUB_OUTPUT" + done diff --git a/.github/actions/runner-select/select.sh b/.github/actions/runner-select/select.sh new file mode 100755 index 0000000..8c5673c --- /dev/null +++ b/.github/actions/runner-select/select.sh @@ -0,0 +1,275 @@ +#!/usr/bin/env bash +set -euo pipefail + +log_token() { + local raw="$1" + if [[ "$raw" =~ ^[A-Za-z0-9._-]*$ ]]; then + printf '%s' "$raw" + else + printf 'rejected' + fi +} + +emit() { + local runs_on="$1" + local pool="$2" + local reason="$3" + local http="${4-}" + local placement="${5-}" + local class_log mode_log summary + class_log="$(log_token "$CLASS")" + mode_log="$(log_token "$MODE")" + summary="class=${class_log} mode=${mode_log} default=${DEFAULT_JSON} overflow=${OVERFLOW_JSON-}" + if [[ -n "$http" ]]; then + summary+=" http=${http}" + fi + if [[ "$placement" == 1 ]]; then + summary+=" hosted.total.free=${TOTAL_FREE} hosted.macos.free=${MACOS_FREE} overflow.headroom=${HEADROOM} hosted.mode=${HOSTED_MODE_LOG}" + fi + summary+=" branch=${reason} reason=${reason}" + printf 'runs-on=%s\n' "$runs_on" + printf 'pool=%s\n' "$pool" + printf 'reason=%s\n' "$reason" + printf 'summary=%s\n' "$summary" + printf 'runner-select: %s\n' "$summary" >&2 +} + +fail_usage() { + echo "runner-select: $*" >&2 + exit 2 +} + +require_cmd() { + command -v "$1" >/dev/null 2>&1 || fail_usage "missing required command: $1" +} + +first_nonempty() { + local v + for v in "$@"; do + if [[ -n "$v" ]]; then + printf '%s' "$v" + return 0 + fi + done + return 0 +} + +parse_label_input() { + local raw="$1" + if [[ -z "$raw" ]]; then + return 1 + fi + if printf '%s' "$raw" | jq -e . >/dev/null 2>&1; then + printf '%s' "$raw" | jq -ec . + return 0 + fi + if [[ "$raw" == *[![:alnum:]_.-]* || "$raw" == *'://'* ]]; then + return 1 + fi + jq -nc --arg s "$raw" '$s' +} + +read_default_json() { + local raw="$1" + local parsed + if ! parsed="$(parse_label_input "$raw")"; then + if [[ -z "$raw" ]]; then + echo 'runner-select: default-runs-on is empty' >&2 + exit 1 + fi + echo 'runner-select: invalid default-runs-on' >&2 + exit 1 + fi + DEFAULT_JSON="$parsed" +} + +read_overflow_json() { + local raw="$1" + local parsed + if ! parsed="$(parse_label_input "$raw")"; then + echo 'runner-select: invalid overflow-runs-on' >&2 + exit 1 + fi + OVERFLOW_JSON="$parsed" +} + +overflow_label_key() { + local raw="$1" + local as_string='' + if as_string="$(printf '%s' "$raw" | jq -er 'select(type == "string")' 2>/dev/null)" && [[ -n "$as_string" ]]; then + printf '%s' "$as_string" + return 0 + fi + printf '%s' "$raw" +} + +placement_url_for_overflow() { + local key="$1" + case "$key" in + macos-self-hosted) + first_nonempty "${MELLIFERA_PLACEMENT_URL_MACOS:-}" "${MELLIFERA_PLACEMENT_URL:-}" + ;; + linux-self-hosted) + first_nonempty "${MELLIFERA_PLACEMENT_URL_LINUX:-}" "${MELLIFERA_PLACEMENT_URL:-}" + ;; + linux-nested-virt-self-hosted) + first_nonempty "${MELLIFERA_PLACEMENT_URL_LINUX_NESTED:-}" "${MELLIFERA_PLACEMENT_URL:-}" + ;; + *) + first_nonempty "${MELLIFERA_PLACEMENT_URL:-}" + ;; + esac +} + +placement_body_valid() { + local body="$1" + printf '%s' "$body" | jq -e ' + (.hosted.total.free | type) == "number" + and (.hosted.macos.free | type) == "number" + and (.overflow.headroom | type) == "number" + ' >/dev/null 2>&1 +} + +require_cmd jq +require_cmd curl + +CLASS="${INPUT_CLASS:-}" +MODE="$(first_nonempty "${INPUT_MODE:-}" "${RUNNER_SELECT_MODE:-}")" + +case "$CLASS" in + macos) + DEFAULT_RAW="$(first_nonempty "${INPUT_DEFAULT_RUNS_ON:-}" "${MELLIFERA_DEFAULT_RUNS_ON_MACOS:-}" "${MELLIFERA_DEFAULT_RUNS_ON:-}")" + OVERFLOW_RAW="$(first_nonempty "${INPUT_OVERFLOW_RUNS_ON:-}" "${MELLIFERA_OVERFLOW_RUNS_ON_MACOS:-}" "macos-self-hosted")" + ;; + other) + DEFAULT_RAW="$(first_nonempty "${INPUT_DEFAULT_RUNS_ON:-}" "${MELLIFERA_DEFAULT_RUNS_ON_LINUX:-}" "${MELLIFERA_DEFAULT_RUNS_ON:-}")" + OVERFLOW_RAW="$(first_nonempty "${INPUT_OVERFLOW_RUNS_ON:-}" "${MELLIFERA_OVERFLOW_RUNS_ON_LINUX:-}" "linux-self-hosted")" + ;; + *) + DEFAULT_RAW="$(first_nonempty "${INPUT_DEFAULT_RUNS_ON:-}" "${MELLIFERA_DEFAULT_RUNS_ON:-}")" + OVERFLOW_RAW="$(first_nonempty "${INPUT_OVERFLOW_RUNS_ON:-}")" + ;; +esac + +read_default_json "$DEFAULT_RAW" +if [[ -n "$OVERFLOW_RAW" ]]; then + read_overflow_json "$OVERFLOW_RAW" +fi + +case "$MODE" in + off) + emit "$DEFAULT_JSON" default off + exit 0 + ;; + fixed | github-app) + ;; + '') + emit "$DEFAULT_JSON" default error + exit 0 + ;; + *) + emit "$DEFAULT_JSON" default error + exit 0 + ;; +esac + +case "$CLASS" in + macos | other) ;; + *) + emit "$DEFAULT_JSON" default error + exit 0 + ;; +esac + +TOKEN="$(first_nonempty "${INPUT_PLACEMENT_TOKEN:-}" "${MELLIFERA_PLACEMENT_TOKEN:-}")" +CA_PEM="$(first_nonempty "${INPUT_PLACEMENT_CA:-}" "${MELLIFERA_PLACEMENT_CA:-}")" +if [[ -n "${INPUT_PLACEMENT_URL:-}" ]]; then + URL="${INPUT_PLACEMENT_URL}" +else + URL="$(placement_url_for_overflow "$(overflow_label_key "$OVERFLOW_RAW")")" +fi + +if [[ -z "$TOKEN" ]]; then + emit "$DEFAULT_JSON" default missing-token + exit 0 +fi +if [[ -z "$URL" ]]; then + emit "$DEFAULT_JSON" default error + exit 0 +fi + +if [[ -n "$CA_PEM" && "$CA_PEM" != *'-----BEGIN CERTIFICATE-----'* ]]; then + echo 'runner-select: placement-ca is not a PEM certificate' >&2 + emit "$DEFAULT_JSON" default bad-ca + exit 0 +fi + +TEMP_BASE="${RUNNER_TEMP:-${TMPDIR:-/tmp}}" +CFG="$(mktemp "${TEMP_BASE}/runner-select-curl.XXXXXX")" +BODY_FILE="$(mktemp "${TEMP_BASE}/runner-select-body.XXXXXX")" +CA_FILE='' +chmod 600 "$CFG" +cleanup() { + rm -f "$CFG" "$BODY_FILE" ${CA_FILE:+"$CA_FILE"} +} +trap cleanup EXIT + +{ + printf 'header = "Authorization: Bearer %s"\n' "$TOKEN" +} >"$CFG" + +CURL_TLS=() +if [[ -n "$CA_PEM" ]]; then + CA_FILE="$(mktemp "${TEMP_BASE}/runner-select-ca.XXXXXX")" + chmod 600 "$CA_FILE" + printf '%s\n' "$CA_PEM" >"$CA_FILE" + CURL_TLS=(--cacert "$CA_FILE") +fi + +HTTP_CODE=0 +if ! HTTP_CODE="$(curl --silent --show-error --max-time 10 --config "$CFG" ${CURL_TLS[@]+"${CURL_TLS[@]}"} --output "$BODY_FILE" --write-out '%{http_code}' "$URL" 2>/dev/null)"; then + emit "$DEFAULT_JSON" default curl-failed + exit 0 +fi + +if [[ "$HTTP_CODE" != "200" ]]; then + emit "$DEFAULT_JSON" default non-200 "$HTTP_CODE" + exit 0 +fi + +BODY="$(<"$BODY_FILE")" +if ! placement_body_valid "$BODY"; then + emit "$DEFAULT_JSON" default bad-json "$HTTP_CODE" + exit 0 +fi + +TOTAL_FREE="$(printf '%s' "$BODY" | jq -er '.hosted.total.free | floor')" +MACOS_FREE="$(printf '%s' "$BODY" | jq -er '.hosted.macos.free | floor')" +HEADROOM="$(printf '%s' "$BODY" | jq -er '.overflow.headroom | floor')" +HOSTED_MODE_LOG="absent" +hosted_mode_raw="$(printf '%s' "$BODY" | jq -r 'if (.hosted.mode | type) == "string" then .hosted.mode else empty end')" +if [[ "$hosted_mode_raw" =~ ^[A-Za-z0-9._-]+$ ]]; then + HOSTED_MODE_LOG="$hosted_mode_raw" +fi + +HOSTED=0 +if [[ "$CLASS" == macos ]]; then + if (( TOTAL_FREE > 0 && MACOS_FREE > 0 )); then + HOSTED=1 + fi +elif (( TOTAL_FREE > 0 )); then + HOSTED=1 +fi + +if (( HOSTED )); then + emit "$DEFAULT_JSON" hosted hosted "$HTTP_CODE" 1 + exit 0 +fi + +if (( HEADROOM > 0 )); then + emit "$OVERFLOW_JSON" overflow overflow "$HTTP_CODE" 1 + exit 0 +fi + +emit "$DEFAULT_JSON" default default "$HTTP_CODE" 1 +exit 0 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2677c88..8cf19dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,8 +19,35 @@ env: IOS_DEVICE_NAME: iPhone 17 jobs: + route-linux: + uses: ./.github/workflows/mellifera-route.yml + secrets: + MELLIFERA_PLACEMENT_TOKEN: ${{ secrets.MELLIFERA_PLACEMENT_TOKEN }} + with: + class: other + default-runs-on: ubuntu-latest + route-ios-dynamic: + needs: unit + if: ${{ needs.unit.result == 'success' }} + uses: ./.github/workflows/mellifera-route.yml + secrets: + MELLIFERA_PLACEMENT_TOKEN: ${{ secrets.MELLIFERA_PLACEMENT_TOKEN }} + with: + class: macos + default-runs-on: macos-26 + route-nested: + needs: unit + if: ${{ needs.unit.result == 'success' }} + uses: ./.github/workflows/mellifera-route.yml + secrets: + MELLIFERA_PLACEMENT_TOKEN: ${{ secrets.MELLIFERA_PLACEMENT_TOKEN }} + with: + class: other + default-runs-on: ubuntu-latest + overflow-runs-on: linux-nested-virt-self-hosted unit: - runs-on: ubuntu-latest + runs-on: ${{ fromJSON(needs.route-linux.outputs.runs-on) }} + needs: route-linux steps: - name: Checkout # https://github.com/actions/checkout/releases @@ -72,8 +99,8 @@ jobs: # non-zero fixture LCOV. Expo cannot host this cell (React-Core force-static). e2e-ios-dynamic: name: e2e / iOS dynamic (primary) - runs-on: macos-26 - needs: unit + runs-on: ${{ fromJSON(needs.route-ios-dynamic.outputs.runs-on) }} + needs: [unit, route-ios-dynamic] timeout-minutes: 90 env: CELL: dynamic @@ -145,10 +172,19 @@ jobs: fail_ci_if_error: ${{ github.actor != 'dependabot[bot]' }} # Expo example: staticlib merge (honest static cell). Still asserts fixture hits. + route-ios-static: + needs: unit + if: ${{ needs.unit.result == 'success' }} + uses: ./.github/workflows/mellifera-route.yml + secrets: + MELLIFERA_PLACEMENT_TOKEN: ${{ secrets.MELLIFERA_PLACEMENT_TOKEN }} + with: + class: macos + default-runs-on: macos-26 e2e-ios-static: name: e2e / iOS static (Expo) - runs-on: macos-26 - needs: unit + runs-on: ${{ fromJSON(needs.route-ios-static.outputs.runs-on) }} + needs: [unit, route-ios-static] timeout-minutes: 90 env: CELL: static @@ -224,8 +260,8 @@ jobs: e2e-android: name: e2e / Android (Expo) - runs-on: ubuntu-latest - needs: unit + runs-on: ${{ fromJSON(needs.route-nested.outputs.runs-on) }} + needs: [unit, route-nested] timeout-minutes: 90 steps: - name: Checkout diff --git a/.github/workflows/mellifera-route.yml b/.github/workflows/mellifera-route.yml new file mode 100644 index 0000000..c72c66a --- /dev/null +++ b/.github/workflows/mellifera-route.yml @@ -0,0 +1,63 @@ +name: Mellifera route +on: + workflow_call: + inputs: + class: + description: macos or other + required: true + type: string + default-runs-on: + description: Hosted label string or JSON array + required: true + type: string + overflow-runs-on: + description: Set only for linux-nested-virt-self-hosted + required: false + type: string + default: '' + outputs: + runs-on: + description: JSON for fromJSON() + value: ${{ jobs.route.outputs.runs-on }} + summary: + description: One-line route decision + value: ${{ jobs.route.outputs.summary }} + secrets: + MELLIFERA_PLACEMENT_TOKEN: + description: Bearer token for placement. Absent on fork pull requests. + required: false +jobs: + route: + runs-on: ubuntu-latest + outputs: + runs-on: ${{ steps.sel.outputs.runs-on || steps.fallback.outputs.runs-on }} + summary: ${{ steps.sel.outputs.summary || 'branch=default reason=route-fallback' }} + env: + RUNNER_SELECT_MODE: ${{ vars.RUNNER_SELECT_MODE }} + MELLIFERA_PLACEMENT_TOKEN: ${{ secrets.MELLIFERA_PLACEMENT_TOKEN }} + MELLIFERA_PLACEMENT_CA: ${{ vars.MELLIFERA_PLACEMENT_CA }} + MELLIFERA_PLACEMENT_URL: ${{ vars.MELLIFERA_PLACEMENT_URL }} + MELLIFERA_PLACEMENT_URL_MACOS: ${{ vars.MELLIFERA_PLACEMENT_URL_MACOS || (vars.MELLIFERA_PLACEMENT_ORIGIN && format('{0}/v1/gha/placement?repo={1}&label=macos-self-hosted', vars.MELLIFERA_PLACEMENT_ORIGIN, github.repository)) || '' }} + MELLIFERA_PLACEMENT_URL_LINUX: ${{ vars.MELLIFERA_PLACEMENT_URL_LINUX || (vars.MELLIFERA_PLACEMENT_ORIGIN && format('{0}/v1/gha/placement?repo={1}&label=linux-self-hosted', vars.MELLIFERA_PLACEMENT_ORIGIN, github.repository)) || '' }} + MELLIFERA_PLACEMENT_URL_LINUX_NESTED: ${{ vars.MELLIFERA_PLACEMENT_URL_LINUX_NESTED || (vars.MELLIFERA_PLACEMENT_ORIGIN && format('{0}/v1/gha/placement?repo={1}&label=linux-nested-virt-self-hosted', vars.MELLIFERA_PLACEMENT_ORIGIN, github.repository)) || '' }} + steps: + - id: sel + continue-on-error: true + uses: $/.github/actions/runner-select + with: + class: ${{ inputs.class }} + default-runs-on: ${{ inputs.default-runs-on }} + overflow-runs-on: ${{ inputs.overflow-runs-on }} + - id: fallback + if: steps.sel.outcome != 'success' || steps.sel.outputs.runs-on == '' + env: + DEFAULT_RUNS_ON: ${{ inputs.default-runs-on }} + run: | + set -euo pipefail + if printf '%s' "$DEFAULT_RUNS_ON" | jq -e . >/dev/null 2>&1; then + parsed="$(printf '%s' "$DEFAULT_RUNS_ON" | jq -c .)" + else + parsed="$(jq -nc --arg s "$DEFAULT_RUNS_ON" '$s')" + fi + echo "runs-on=${parsed}" >> "$GITHUB_OUTPUT" + echo "::warning::runner-select: branch=default reason=route-fallback"