Sub topic 7
Requiring a shared workflow to pass before merging
This chapter assumes you know how to write a GitHub Actions workflow and how status checks appear on a pull request. It also assumes you can read an organisation ruleset, including its target, conditions and rules. Earlier chapters covered those parts. Here you add one rule type, workflows, to an organisation ruleset.
Why require a workflow instead of a status check
A required status check only names a check. Each repository still has to define a workflow that produces it. A team can delete that workflow, rename the job, or skip it with path filters. The check then never reports, or reports under a different name.
A required workflow moves the definition to one place. The organisation keeps a single workflow file in one repository. The ruleset tells GitHub to run that file on pull requests in every targeted repository. Repository owners cannot edit or remove it from their own repositories.
Typical uses:
- Run a security or licence scan in every repository.
- Run a common lint or build step.
- Check that every repository contains required files, such as a
CODEOWNERSfile.
How the pieces fit together
Two repositories are involved. The source repository holds the workflow file. The target repositories are the ones the ruleset selects through its repository_name or repository property conditions.
The workflow runs in the target repository's context. Inside the workflow, github.repository is the target repository, and the checkout step fetches the target's code. The source repository only supplies the YAML.
Writing the shared workflow
The workflow must listen for the events that a merge goes through. Use pull_request, and add merge_group if any targeted repository uses a merge queue. If the workflow has no matching trigger, the required check never reports and the pull request waits indefinitely.
name: Org CI
on:
pull_request:
merge_group:
permissions:
contents: read
jobs:
baseline:
runs-on: ubuntu-latest
steps:
- name: Check out the target repository
uses: actions/checkout@v4
- name: Require a CODEOWNERS file
run: |
if [ ! -f .github/CODEOWNERS ] && [ ! -f CODEOWNERS ]; then
echo "::error::No CODEOWNERS file found"
exit 1
fiSave this as .github/workflows/org-ci.yml in the source repository, for example YOUR_ORG/org-workflows.
Keep these limits in mind:
- The source repository must allow other repositories in the organisation to use its workflows. In the source repository's Settings > Actions > General, set Access to allow repositories in the organisation. A private source repository fails with a not-found style error otherwise.
- Keep the workflow general. It runs on every targeted repository, including ones with different languages and layouts.
- Avoid secrets that only some repositories have. Use organisation-level secrets, or the workflow will fail where they are missing.
Adding the rule to a ruleset
The rule needs the numeric ID of the source repository, not its name. Look it up with the gh CLI:
gh api /repos/YOUR_ORG/org-workflows --jq .idThen add a workflows entry to the rules array of an organisation ruleset:
{
"type": "workflows",
"parameters": {
"do_not_enforce_on_create": false,
"workflows": [
{
"repository_id": 123456,
"path": ".github/workflows/org-ci.yml",
"ref": "refs/heads/main"
}
]
}
}The parameters:
| Field | Meaning |
|---|---|
| `repository_id` | ID of the source repository that stores the workflow. |
| `path` | Path to the workflow file inside the source repository. |
| `ref` | Branch, tag or commit in the source repository to read the workflow from. |
| `do_not_enforce_on_create` | When `true`, creating a new matching branch does not require the workflow to have passed first. |
| `workflows` | An array, so one rule can require several workflows. |
The workflows rule is available only in organisation rulesets, not in a ruleset on a single repository. Availability can depend on your GitHub plan, so check the current GitHub documentation for your plan.
Choosing the ref
The ref value decides which version of the workflow runs. You have three options.
- A branch such as
refs/heads/main. Updating the workflow means merging to that branch. The change reaches every repository at once. This is convenient and also risky, because a bad merge breaks every pull request in the organisation. - A tag such as
refs/tags/v1. You publish a new tag and then update the ruleset to point at it. Rollout is deliberate and can be reverted by pointing back to the old tag. - A commit SHA. This is the most predictable choice, and it needs a ruleset update for every change.
Protect the source repository's branch with its own ruleset. Anyone who can push to the ref that the organisation ruleset points at can run code on every targeted repository's pull requests. Restrict write access to a small team and require reviews there.
Rolling it out
Start by creating the ruleset with enforcement set to evaluate. In evaluate mode, GitHub runs the workflow and records the result without blocking merges. The rule insights page shows which repositories would have been blocked. Fix the shared workflow, or exclude repositories that cannot comply, and then switch to active.
Here is a full ruleset body that applies the workflow to the default branch of repositories matching a name pattern:
{
"name": "require-org-ci",
"target": "branch",
"enforcement": "evaluate",
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
},
"repository_name": {
"include": ["~ALL"],
"exclude": ["org-workflows", "sandbox-*"]
}
},
"rules": [
{
"type": "workflows",
"parameters": {
"do_not_enforce_on_create": false,
"workflows": [
{
"repository_id": 123456,
"path": ".github/workflows/org-ci.yml",
"ref": "refs/heads/main"
}
]
}
}
]
}Excluding the source repository itself is a common choice. Otherwise a broken change to org-ci.yml blocks the very pull request that would fix it.
Create the ruleset from the file:
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/orgs/YOUR_ORG/rulesets \
--input require-org-ci.jsonWhen insights look clean, change "enforcement" to "active" in the file and update the ruleset. The PUT request replaces the whole ruleset body, so send the complete JSON and not only the changed field. Find the ruleset ID with gh api /orgs/YOUR_ORG/rulesets.
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
/orgs/YOUR_ORG/rulesets/RULESET_ID \
--input require-org-ci.jsonCommon errors
- The check stays "Expected" forever. The workflow has no
pull_requesttrigger, or the path orrepository_idis wrong, so no run starts. Check the workflow file and the numbers in the rule. - The workflow cannot be found. The source repository does not share its Actions with the organisation, or the
refdoes not exist there. - Everyone is blocked after a merge to the source repository. The
refpoints at a branch that anyone can change. Pin a tag or SHA. - Required checks defined elsewhere conflict. The same job might also be listed as a required status check in another ruleset or in a branch protection rule. Rules from several rulesets combine, and every one of them must be satisfied, so a duplicate or mismatched name can block merges.
- A new repository cannot create its first branch. Set
do_not_enforce_on_createtotrueif the workflow cannot pass before the branch exists. - The workflow depends on repository content that some repositories lack. Exclude those repositories, or make the workflow skip cleanly with a passing result. A skipped job caused by a
pathsfilter at the workflow level means no run is created, which can leave the check pending, so avoid workflow-level path filters here.
Exercise
You add a workflows rule to an organisation ruleset in active mode. The rule points at org-ci.yml in the repository org-workflows. Pull requests in a targeted repository now show the required check as "Expected" and never start. The workflow file starts with on: push. What is wrong, and what do you change? Also name one reason to exclude org-workflows from the ruleset.
Show answer
The workflow does not listen for pull_request (or merge_group when a merge queue is used), so no run starts and the required check never reports. Change the trigger to on: pull_request (add merge_group for merge queues) and update the file at the ref the rule uses. Excluding org-workflows prevents a broken change to the shared workflow from blocking the pull request that would fix it.