Sub topic 8
Just-in-time runner configuration through the API
This chapter covers just-in-time (JIT) runner configuration: what it replaces, how to request it, and what can go wrong. It assumes you can read GitHub Actions workflow syntax, use a Linux shell, and understand pull requests. It also assumes you have read the previous chapter on ephemeral runners and know what --ephemeral does.
The problem JIT configuration solves
The standard way to register a runner is config.sh with a registration token. That works, but it has three weaknesses in an automated fleet.
- A registration token is a credential that lets its holder add runners to a repository, organization, or enterprise. Tokens expire after one hour, but your provisioning system still has to fetch one and hand it to every new machine.
config.shwrites state to disk: a.runnerfile, a.credentialsfile, and an RSA key. Your image or startup script must handle those files.- Registration and job assignment are separate steps. A runner can register, sit idle, and then pick up a job that was meant for something else.
JIT configuration removes the separate registration step. You call a REST API endpoint, and GitHub creates one runner record and returns an encoded configuration blob. You pass that blob to run.sh. The runner starts, connects, takes one job, and is removed.
What the endpoint does
The endpoint is generate-jitconfig. It exists at three scopes.
| Scope | Endpoint |
|---|---|
| Repository | `POST /repos/OWNER/REPO/actions/runners/generate-jitconfig` |
| Organization | `POST /orgs/ORG/actions/runners/generate-jitconfig` |
| Enterprise | `POST /enterprises/ENTERPRISE/actions/runners/generate-jitconfig` |
The request body takes these fields:
name: the runner name. It must be unique within the scope, because the call creates a runner record.runner_group_id: the ID of the runner group the runner joins. Repository-level runners use the default group, ID1.labels: the labels for this runner. The list must include at least one label.work_folder: optional. The directory where job files are placed. The default is_work.
The response contains two things: a runner object with the new runner's ID and name, and encoded_jit_config, a base64-encoded string holding the settings and credentials the runner needs.
Treat encoded_jit_config as a secret. Anyone who holds it can start a runner that claims your runner's identity and receives its job. It is single-use, and it stops working once a runner has connected with it. It is still sensitive until then.
Requesting a configuration
This example uses the GitHub CLI. It is the same call you would make from a provisioning script.
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)"Walk through it:
-fsends a string field.-Fsends a typed field, sorunner_group_idgoes out as a number rather than a string.- The
labels[]form builds a JSON array. Repeating the flag adds items. --jq .encoded_jit_configextracts only the blob from the response. The redirect saves it tojitconfig.txt.run.sh --jitconfigstarts the runner using that blob. There is noconfig.shstep and no registration token.
Note that the example writes the blob to a file. On a shared machine, that file is readable by whoever can read the directory. Prefer passing the value through an environment variable or a pipe, and delete the file if you must create one.
Credentials the caller needs
The call that creates a JIT runner needs a credential with permission to manage runners. For a repository, a fine-grained personal access token or GitHub App installation token needs the repository permission "Administration" with write access. For an organization, it needs the organization permission "Self-hosted runners" with write access. A classic personal access token needs the repo scope for repositories or admin:org for organizations.
Do not use the workflow's GITHUB_TOKEN for this. It cannot create runners. The call is made by your provisioning service, outside any job.
This credential is more powerful than the blob it produces. A GitHub App is the better choice for production, because installation tokens are short-lived and the app's permissions can be limited to one task.
How JIT differs from `--ephemeral`
JIT runners are always ephemeral. The difference is in how they get registered, not in how many jobs they take.
| Property | `config.sh --ephemeral` | `generate-jitconfig` |
|---|---|---|
| Registration credential | Registration token, valid one hour | API call with an admin credential |
| Local registration step | `config.sh` required | None |
| Files written at configuration time | `.runner`, `.credentials`, RSA key | None from `config.sh`; the blob carries the settings |
| Jobs accepted | One | One |
| Runner record created | When `config.sh` runs | When the API call returns |
| Where you set labels and group | `config.sh` flags | API request body |
Because the runner record is created by the API call, you know the runner's ID and name before the machine boots. That is useful for cleanup. If the machine never starts, you can delete the record yourself.
Lifecycle of a JIT runner
Two points deserve attention.
First, GitHub removes the runner record after the job. Your system still has to destroy the host. A runner that finished its job is gone from GitHub's side, but the virtual machine or container is still running until you remove it. If you reuse that host for another JIT runner without cleaning it, you are back to the persistent-runner problems from earlier chapters.
Second, the runner is matched to a job by labels. Once connected, it takes the first queued job whose runs-on labels it satisfies. The job is not tied to the specific request that caused you to create the runner.
Matching jobs with the workflow
The workflow side is unchanged. It uses the labels you passed in the API call.
name: test
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: [self-hosted, linux, x64, ephemeral]
steps:
- uses: actions/checkout@v4
- run: make testThe job runs on a runner that carries all four labels. The runner in the API example above has exactly those four, so it qualifies. The permissions block limits the GITHUB_TOKEN to read access on repository contents.
A common provisioning design listens for the workflow_job webhook event. When the action is queued, the service reads the job's labels, creates a JIT runner with matching labels, and starts a host. When the action is completed, the service cleans up. This is how autoscaling controllers work, and the next chapter covers it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| HTTP 404 from the endpoint | The credential lacks permission, or the repository path is wrong. GitHub returns 404 rather than 403 for some hidden resources. | Check the token's permission and the owner and repository names. |
| HTTP 409 or 422 about the name | A runner with that name already exists in the scope. | Include a unique suffix such as a timestamp or random ID. |
| HTTP 422 about the group | The `runner_group_id` does not exist, or the repository is not allowed to use that group. | List groups and confirm the ID and repository access. |
| Runner starts, then exits with an authentication error | The config was already used, or it expired. | Request a new one. Do not retry with the same blob. |
| Job stays queued | No runner has all the labels in `runs-on`, or the runner is in a group the repository cannot use. | Compare the job's labels with the runner's. |
| Runner record remains after the host died | The host failed before it connected. | Delete the runner by ID with the API. |
Cleaning up orphaned records is part of running JIT well. A request that succeeds but is never followed by a started host leaves an idle runner entry. Deleting it is a single call:
gh api --method DELETE /repos/OWNER/REPO/actions/runners/RUNNER_IDTake RUNNER_ID from the runner.id field of the generate response. Store it with your provisioning record so you can find it later.
Security notes
JIT configuration improves the registration path. It does not make an unsafe job safe.
- Labels are not a security boundary. Any workflow in an allowed repository can write the same
runs-onlist. Restrict access with runner groups, which the later chapter on groups covers. - A job running on a JIT runner still executes arbitrary code from the workflow. If the trigger accepts fork pull requests, that code can be hostile. JIT limits how long the runner lives. It does not limit what the job does while it lives.
- The blob is a secret in transit. Do not log it, and do not put it in a command line visible to other users on the host. Process arguments are readable through
ps. For that reason, a real provisioner often passes the value through an environment variable or a file with restricted permissions. - Keep the credential that calls the API off the runner host. The host needs only the blob. If the host also held the admin credential, a compromised job could register further runners.
Putting it together
A minimal provisioning loop for a single host looks like this.
#!/usr/bin/env bash
set -euo pipefail
REPO="OWNER/REPO"
NAME="jit-$(hostname)-$(date +%s)"
RESPONSE="$(gh api --method POST "/repos/${REPO}/actions/runners/generate-jitconfig" \
-f name="${NAME}" \
-F runner_group_id=1 \
-f 'labels[]=self-hosted' \
-f 'labels[]=linux' \
-f 'labels[]=x64' \
-f 'labels[]=ephemeral')"
RUNNER_ID="$(jq -r .runner.id <<<"${RESPONSE}")"
JIT_CONFIG="$(jq -r .encoded_jit_config <<<"${RESPONSE}")"
echo "Created runner ${RUNNER_ID} named ${NAME}"
./run.sh --jitconfig "${JIT_CONFIG}"This script is a teaching sketch. In production, the process holding the admin credential runs on a separate control plane, and only the JIT_CONFIG value reaches the runner host. The script also does not destroy the host afterward. That is the job of whatever created the host, such as a VM lifecycle hook or the container's restart policy set to never restart.
Exercise
A provisioner calls generate-jitconfig and gets a runner ID, but the virtual machine fails to boot, so run.sh never runs. What is left behind on the GitHub side, and what should the provisioner do?
Show answer
An idle runner record remains registered under that name, because the API call creates the record before any host connects. The provisioner should delete it with DELETE /repos/OWNER/REPO/actions/runners/RUNNER_ID, using the ID it saved from the response, and then retry with a new name and a new configuration.
Exercise
Why is it a mistake to store the admin token used for generate-jitconfig on the runner host, even though the runner itself is ephemeral?
Show answer
A job on that host can read the token. With it, the job can create more runners, delete runners, or request configurations that route other jobs to an attacker's machine. The token outlives the single job, so the ephemeral lifetime of the runner does not contain the damage.