servo/.github/workflows/self-hosted-runner-select.yml
Delan Azabani 8bb739b818
CI: use self-hosted runners for Linux build jobs (#33321)
* CI: use self-hosted runners for Linux build jobs

Signed-off-by: Delan Azabani <dazabani@igalia.com>

* Set ccache and incremental env variables when not self-hosted

Signed-off-by: Delan Azabani <dazabani@igalia.com>

* Force GitHub-hosted runner when in upload mode

Signed-off-by: Delan Azabani <dazabani@igalia.com>

* Revert s/python/python3/ now that our image has python

Signed-off-by: Delan Azabani <dazabani@igalia.com>

* Remove stray comment in timeout workflow

Signed-off-by: Delan Azabani <dazabani@igalia.com>

* Update description of runner-select job

Signed-off-by: Delan Azabani <dazabani@igalia.com>

* Apply suggestions from code review

Address couple minor naming / formatting nits

Signed-off-by: Martin Robinson <mrobinson@igalia.com>

---------

Signed-off-by: Delan Azabani <dazabani@igalia.com>
Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
2024-09-08 15:06:44 +00:00

98 lines
3.9 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: Select Self-hosted Runner
on:
workflow_call:
inputs:
github-hosted-runner-label:
required: true
type: string
self-hosted-image-name:
required: true
type: string
self-hosted-runner-scope:
required: false
type: string
default: /orgs/${{ github.repository_owner }}/actions/runners
monitor-api-base-url:
required: false
type: string
default: https://ci0.servo.org
force-github-hosted-runner:
required: false
type: boolean
default: false
outputs:
unique-id:
value: ${{ jobs.runner-select.outputs.unique-id }}
selected-runner-label:
value: ${{ jobs.runner-select.outputs.selected-runner-label }}
is-self-hosted:
value: ${{ jobs.runner-select.outputs.is-self-hosted }}
jobs:
# Selects a self-hosted runner if available, or else a GitHub-hosted runner.
# We generate a unique id for the workload, then ask our monitor API to
# reserve a self-hosted runner for us.
runner-select:
name: Select Runner
runs-on: ubuntu-latest
outputs:
unique-id: ${{ steps.select.outputs.unique_id }}
selected-runner-label: ${{ steps.select.outputs.selected_runner_label }}
is-self-hosted: ${{ steps.select.outputs.is_self_hosted }}
steps:
- name: Select and reserve best available runner
id: select
run: |
github_hosted_runner_label='${{ inputs.github-hosted-runner-label }}'
self_hosted_image_name='${{ inputs.self-hosted-image-name }}'
self_hosted_runner_scope='${{ inputs.self-hosted-runner-scope }}'
monitor_api_base_url='${{ inputs.monitor-api-base-url }}'
set -euo pipefail
fall_back_to_github_hosted() {
echo 'Falling back to GitHub-hosted runner'
echo "selected_runner_label=$github_hosted_runner_label" | tee -a $GITHUB_OUTPUT
echo 'is_self_hosted=false' | tee -a $GITHUB_OUTPUT
exit 0
}
# Generate a unique id that allows the workload job to find the runner
# we are reserving for it (via runner labels), and allows the timeout
# job to find the workload job run (via the jobs friendly name), even
# if there are multiple instances in the workflow call tree.
unique_id=$(uuidgen)
echo "unique_id=$unique_id" | tee -a $GITHUB_OUTPUT
# Disable self-hosted runners by creating a repository variable named
# NO_SELF_HOSTED_RUNNERS with any non-empty value.
# <https://github.com/servo/servo/settings/variables/actions>
if [ -n '${{ vars.NO_SELF_HOSTED_RUNNERS }}' ]; then
echo 'NO_SELF_HOSTED_RUNNERS is set!'
fall_back_to_github_hosted
fi
if [ '${{ inputs.force-github-hosted-runner }}' = true ]; then
echo 'inputs.force-github-hosted-runner is set!'
fall_back_to_github_hosted
fi
# Use the monitor API to reserve a runner. If we get an object with
# runner details, we succeeded. If we get null, we failed.
take_runner_url=$monitor_api_base_url/$self_hosted_image_name/$unique_id/${{ github.repository }}/${{ github.run_id }}
result=$(mktemp)
echo
echo POST "$take_runner_url"
if ! curl -sS --fail-with-body --connect-timeout 5 --max-time 30 -X POST "$take_runner_url" \
-H 'Authorization: Bearer ${{ secrets.SERVO_CI_MONITOR_API_TOKEN }}' > $result \
|| ! jq -e . $result > /dev/null; then
cat $result
echo
echo
echo 'No self-hosted runners available!'
fall_back_to_github_hosted
fi
echo
echo "selected_runner_label=reserved-for:$unique_id" | tee -a $GITHUB_OUTPUT
echo 'is_self_hosted=true' | tee -a $GITHUB_OUTPUT