Sub topic 15
Custom protection rules with GitHub Apps
This chapter assumes you know how required reviewers and wait timers gate a job that targets an environment. It also assumes you know what a webhook is. Built-in rules cover human approval and timing. A custom protection rule lets an external system decide, such as a monitoring service, a change-management tool, or your own code.
How a custom rule works
A custom rule is a GitHub App that you enable on an environment. When a job reaches that environment, GitHub pauses the deployment and sends the app a deployment_protection_rule webhook event. The deployment stays pending until the app answers through the REST API.
Setting it up
- Create or install a GitHub App that subscribes to the Deployment protection rule event and has the repository permissions it needs, such as Deployments read and write.
- Install the app on the repository.
- Open Settings, then Environments, then the environment. Under Deployment protection rules, tick the app.
An environment can have a limited number of custom rules (currently up to six). Every enabled rule must approve before the job runs, together with any built-in rules.
Responding to a request
The webhook payload includes the environment, the deployment, the repository, and a deployment_callback_url. The app authenticates as an installation and posts its decision to that URL.
# INSTALLATION_TOKEN is an installation access token for your app.
# CALLBACK_URL is the deployment_callback_url from the webhook payload.
GH_TOKEN="$INSTALLATION_TOKEN" gh api --method POST "$CALLBACK_URL" \
-f environment_name=production \
-f state=approved \
-f comment="No open incidents in the monitoring system"Use state=rejected to fail the deployment. The comment appears in the deployment's review history. The app may also post more than once, for example to wait while it polls an external system, so long as it answers before the deployment times out.
Common errors
- The deployment waits forever. The app is not subscribed to the event, is not installed on the repository, or its webhook URL is unreachable. Check the app's recent deliveries in its settings.
- The callback returns 403 or 404. The request used the wrong credential. Use an installation token for this repository, not a personal token or the app's JSON Web Token.
- The rule does not appear in the environment settings. Custom rules need a public repository, or a private or internal repository on a plan that supports them. See the availability chapter.
- The app approves for the wrong environment. Always send the
environment_namefrom the payload, because one app can guard several environments.
Exercise
Your app receives a deploymentprotectionrule event for production but never responds. What happens to the deployment, and what call would approve it?
Show answer
The deployment stays pending, and the job does not run, until the app answers or the deployment times out. To approve it, the app sends a POST request to the payload's deploymentcallbackurl, authenticated with an installation token, with environment_name set to production and state set to approved.