Sub topic 10
Limiting which repositories and workflows can use a runner
This chapter assumes you know how labels route jobs to runners (covered earlier) and that you have admin access to a GitHub organization. It also assumes you understand why a persistent runner is risky and why untrusted code must stay away from it. Here you will restrict who can reach a runner at all.
Why labels are not access control
Labels decide which runner is eligible for a job. They do not decide who may ask for it. Any workflow author can write runs-on: [self-hosted, linux, x64, ephemeral], and if the labels match, the job is queued for that runner.
Anyone with write access to a repository can edit a workflow file on a branch and run arbitrary commands on whatever runner it selects. Labels give no protection against that. Runner groups and workflow restrictions close the gap.
Runner groups
A runner group is a named collection of self-hosted runners that sits at the organization or enterprise level. The group, not the individual runner, carries the access policy. A group controls three things:
- Repository access. The group can be open to all repositories in the organization, or to a selected list.
- Public repository access. This is a separate toggle. It is off by default, and you should leave it off. Public repositories accept pull requests from strangers, and a self-hosted runner reachable from a public repository is an invitation to run their code.
- Workflow access. The group can be limited to specific workflow files at specific refs.
Every organization has a Default group. New runners land there unless you pick another group, and its default policy is permissive. A reasonable first step is to stop using it for anything sensitive. Create purpose-specific groups, such as ci-untrusted-ok, deploy-prod, and release-signing, and give each the narrowest policy that works.
Repository-level runners (registered to a single repo) cannot be placed in groups. If you need group policies, register runners at the organization level.
| Control | Scope | What it stops |
|---|---|---|
| Labels in `runs-on` | Per job | Jobs landing on a runner with the wrong OS or hardware |
| Repository access on a group | Per group | Repositories outside the list from using the runners |
| Allow public repositories (off) | Per group | Public repositories using the runners |
| Workflow restriction | Per group | Unlisted workflow files from using the runners |
| `permissions:` in the workflow | Per workflow or job | A compromised job from using a broad `GITHUB_TOKEN` |
How a job is checked
When a job requests a runner, GitHub evaluates the group policy before it assigns the job. A job that fails any check is not given a runner and waits in the queue. It does not fall through to another group.
A job stuck in the queue with no error is the usual symptom of a policy mismatch. The log shows only "Waiting for a runner to pick up this job". Check the group's repository list and workflow restriction before you suspect the runner is offline.
Targeting a group in a workflow
Workflows can name the group directly. Combine group with labels so the job is limited to the group and to matching hardware.
name: deploy
on:
push:
branches: [main]
permissions:
contents: read
jobs:
deploy:
runs-on:
group: deploy-prod
labels: [self-hosted, linux, x64, ephemeral]
steps:
- uses: actions/checkout@v4
- run: ./scripts/deploy.shNaming the group does not grant access. If the repository is not allowed in deploy-prod, the job never starts. The name only makes the intended target explicit and keeps the job off the Default group.
Creating a restricted group with the REST API
You can build groups in the web interface, but the REST API makes the policy reviewable and repeatable. The following request creates a group visible only to two repositories, closed to public repositories, and restricted to a single workflow file on main.
First find the repository IDs:
gh api /repos/OWNER/app --jq .id
gh api /repos/OWNER/infra --jq .idThen create the group. Replace ORG, the repository IDs, and the workflow path with your own values.
cat > group.json <<'EOF'
{
"name": "deploy-prod",
"visibility": "selected",
"selected_repository_ids": [111111111, 222222222],
"allows_public_repositories": false,
"restricted_to_workflows": true,
"selected_workflows": [
"ORG/app/.github/workflows/deploy.yml@refs/heads/main"
]
}
EOF
gh api --method POST /orgs/ORG/actions/runner-groups --input group.jsonThe response includes the new group's id. That number is the runner_group_id you pass to the just-in-time configuration endpoint from the previous chapter, so each fresh runner registers straight into the restricted group instead of landing in Default.
Check the result at any time:
gh api /orgs/ORG/actions/runner-groups --jq '.runner_groups[] | {id, name, visibility, allows_public_repositories, restricted_to_workflows}'Workflow restrictions in detail
A workflow restriction names the allowed workflow by repository, path, and ref, in the form OWNER/REPO/.github/workflows/FILE.yml@REF. The ref can be a branch, a tag, or a full commit SHA.
Consider what this does and does not give you:
- A developer who edits
deploy.ymlon a feature branch gets no access to the group. The refrefs/heads/feature-xis not on the list. - A developer who creates a different file,
evil.yml, on any branch also gets no access. - A developer who can push directly to
maincan still changedeploy.ymland the restriction will allow it. The restriction is only as strong as the protection on the ref it names.
Pair the restriction with branch protection on main: require pull request review, require review from code owners, and block force pushes. Add a CODEOWNERS entry for .github/workflows/ so workflow changes need a security reviewer.
# .github/CODEOWNERS
/.github/workflows/ @ORG/platform-securityA tag or SHA ref is harder to move than a branch, but it means you must update the restriction on every release. Many teams accept the branch ref plus branch protection as the practical middle.
Test reusable workflows before you rely on a restriction. When a job runs from a workflow called by another workflow, confirm in a throwaway group which file ref GitHub matches against the list.
Minimal permissions inside the job
Group policy decides who gets onto the runner. The permissions: key decides what the job can do once it is there. Set it explicitly, because the default token permissions depend on organization and repository settings and may be broader than you expect.
permissions:
contents: read
jobs:
test:
runs-on:
group: ci
labels: [self-hosted, linux, x64, ephemeral]
steps:
- uses: actions/checkout@v4
- run: make testSet organization-wide default token permissions to read-only as well, so a workflow that omits the key still gets the smaller token. Grant extras such as id-token: write only on the jobs that need them.
Combining groups for different trust levels
A workable layout separates runners by how much you trust the code that reaches them:
ci-internal: ephemeral runners with no cloud credentials, open to private repositories, used for tests and builds of reviewed code.deploy-prod: ephemeral runners that can request OIDC credentials, open to two repositories, restricted to one workflow on a protectedmain.- No self-hosted group for public repositories. Use GitHub-hosted runners for them. If you must self-host for a public repository, use a dedicated group of ephemeral runners with no network path to internal systems and no credentials.
Because each group is a separate set of ephemeral runners, a compromised job in ci-internal cannot find leftover state from a deploy job. The groups hold the boundary between trust levels, and ephemerality stops state from crossing it over time.
Common mistakes
- Leaving runners in
Default. New runners inherit a permissive policy. Register into a named group, or use JIT configuration with an explicitrunner_group_id. - Enabling public repository access "just to test". The setting is easy to forget. Audit it with the list command above.
- Restricting repositories but not workflows. Everyone with write access to an allowed repository can still run any workflow on the group's runners.
- Naming a branch in a workflow restriction without protecting it. The restriction then trusts every person who can push to that branch.
- Treating the group name in
runs-onas a security boundary. The name is a request. The policy on the group is the boundary.
Exercise
Your organization has a group deploy-prod open to selected repositories, with public repositories blocked and no workflow restriction. A developer with write access to an allowed repository adds a new workflow file on a feature branch that uses runs-on: { group: deploy-prod, labels: [self-hosted, linux] } and runs env. What happens, and which setting would change it?
Show answer
The job runs on a deploy-prod runner, because the repository is allowed and nothing limits which workflow files may use the group. Setting restricted_to_workflows to true and listing only ORG/REPO/.github/workflows/deploy.yml@refs/heads/main would make the job wait in the queue, since the feature-branch file is not on the list. Branch protection and CODEOWNERS on main keep that list meaningful.
Exercise
A job targeting a restricted group stays queued with "Waiting for a runner to pick up this job", and the runners show as idle and online. Name three policy checks you would inspect.
Show answer
Check that the repository is in the group's allowed repository list, that the repository is not public while the group blocks public repositories, and that the workflow file path and ref match an entry in the group's selected workflows. Also confirm that every label in runs-on is carried by at least one runner in the group.