
Let's say you have a situation where certain things need to be checked before a pull request can be merged into the main or master or default branch of your repository. First let's create a GitHub Action workflow that will either pass (exit 0) or fail (exit 1) based on some condition. In this trivial example, if the base directory of the branch for the pull request does not contain a file named example.txt the GitHub Action will fail (exit 1).
name: checking files in this pull request
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
pull_request:
types: [opened]
jobs:
on-pull-request-job:
runs-on: [self-hosted, linux]
steps:
- name: Checking out the repository code in this pull request
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Fail if example.txt is NOT in this pull request (it should be)
run: if [[ ${{ github.workspace }} != *"example.txt"* ]]; then echo "this pull request does NOT contain example.txt"; exit 1; fi
Then let's go to the repository Settings > Rules > Rulesets and let's create a rule set. The main thing you want the rule set to do is to Require status checks to pass and to then use the GitHub Action workflow you created.

You probably want to apply this ruleset to only the main/master/default branch.

Then for proof of concept, assuming your repository does not contain a file named example.txt in the base directory of the repository, the pull request should fail the status check and the pull request will not be allowed to be merged into the main/master/default branch of the repository.

Did you find this article helpful?
If so, consider buying me a coffee over at