Sub topic 8
Just-in-time runner configuration through the API
This chapter assumes you can read GitHub Actions workflows, use a Linux shell, and have registered a runner with config.sh before. It also assumes you know what the --ephemeral option does: the runner takes one job and then removes itself.
The problem with registration tokens
Registering an ephemeral runner with config.sh needs a registration token. That token is short-lived, but it is still a secret that must reach every machine that starts a runner. The config.sh step also writes configuration files to disk before the runner starts.
Just-in-time (JIT) configuration removes both steps. You ask the REST API to create a runner registration, and the API returns an encoded configuration. The runner starts from that value alone.
What JIT configuration does
A JIT runner has these properties:
- The registration is created at the moment you call the API, not when the runner starts.
- The runner is ephemeral. It accepts one job and is then removed.
- The returned configuration is passed to
run.sh, so there is no separateconfig.shstep. - You choose the name, the runner group, and the labels in the API call.
Because the labels are fixed in the request, the runner can only receive jobs that match them.
Requesting a JIT configuration
The endpoint is generate-jitconfig. This example uses the GitHub CLI against a repository.
gh api --method POST /repos/OWNER/REPO/actions/runners/generate-jitconfig \
-f name="jit-runner-1" \
-F runner_group_id=1 \
-f 'labels[]=self-hosted' \
-f 'labels[]=linux' \
-f 'labels[]=x64' \
-f 'labels[]=ephemeral' \
--jq .encoded_jit_config > jitconfig.txt
./run.sh --jitconfig "$(cat jitconfig.txt)"The request has four parts:
nameis the runner name. It must be unique, so generate one per call.runner_group_idselects the runner group. In a repository, group1is the default group.labels[]lists the labels. The API requires theself-hostedlabel, and you normally add the operating system and architecture labels too.--jq .encoded_jit_configextracts the encoded value from the JSON response.
The response also describes the runner object, but only encoded_jit_config is needed to start it.
The flow end to end
The caller that holds API credentials is separate from the machine that runs the job. That separation is the main benefit.
The controller needs a credential that is allowed to create runners. The runner itself never holds it. It holds only the encoded configuration, which is valid for that one registration.
Common errors
- Reusing a name. A second request with a name already in use fails. Add a timestamp or random suffix.
- Starting the runner too late. A JIT runner that never connects stays registered but idle. Start it right after the call, and clean up unused registrations.
- Missing
self-hostedin labels. The request is rejected without it. - Wrong group ID. The group must exist and must allow the repository. See the chapter on runner groups.
- Insufficient permissions on the caller. The credential needs admin access to the repository, or the matching organization permission. Do not give the runner's own host this credential.
Where JIT fits
JIT configuration builds on the ephemeral runner from the previous chapter. It does not replace isolation. A JIT runner on a reused machine still shares that machine's state with earlier jobs unless the machine is recreated. The next chapter shows how autoscaling creates a fresh machine and a fresh JIT configuration for each job.
Exercise
A team's startup script calls config.sh with a registration token stored on every runner host. Name two things JIT configuration changes about this setup.
Show answer
The long-lived registration token no longer needs to be stored on the runner hosts, because a controller calls generate-jitconfig and passes only the encoded configuration to run.sh. The separate config.sh step is also gone, and the runner is automatically ephemeral, so it takes one job and is removed.
Exercise
Your request to generate-jitconfig fails with a conflict error on the second run of your script. What is the most likely cause, and how do you fix it?
Show answer
The runner name is already in use. Generate a unique name per request, for example by adding a timestamp or random suffix.