Sub topic 7
What a GitHub App is
This chapter assumes you know how to call the GitHub REST API with a bearer token, and that you have seen fine-grained personal access tokens (PATs). It introduces the GitHub App model. Later chapters cover installing apps, permissions and events, and the token exchange in detail.
The problem a PAT does not solve
A fine-grained PAT limits access by repository and permission, but it still belongs to one person. That causes three problems for automation:
- Actions taken with the token appear as that person.
- If the person leaves the organization, the token stops working.
- The token acts as a stand-in for a human, not as a distinct piece of software.
A GitHub App fixes these problems. It is a first-class identity on GitHub, registered once and then installed wherever it is needed.
What an app is made of
A GitHub App is a registration that holds a few pieces of configuration:
- An identity. The app has an App ID and a name. Its actions show up as
your-app[bot]. - A set of permissions. The app declares the access it needs, such as read access to contents or write access to issues. These use the same model as fine-grained PATs.
- Webhook subscriptions. The app can ask GitHub to send it events, such as a new pull request.
- A private key. You generate this key on the app's settings page. The app uses it to prove who it is.
The registration is not tied to any repository. It is a template. Access only exists after someone installs the app.
How the pieces relate
An installation connects an app to an account and, optionally, to a specific list of repositories. The app can only reach what the installation covers, and only with the permissions the owner approved.
Each installation has its own ID. When your code acts, it acts as one specific installation, so a single app can serve many customers without mixing their data.
Two ways the app can authenticate
An app has two levels of identity:
- As itself. The app signs a JSON Web Token (JWT) with its private key. This credential can manage the app, for example list its installations. It cannot read repository contents.
- As an installation. The app exchanges its JWT for an installation access token. This token is what you use to read and write repository data.
The installation token is short-lived and limited to the repositories and permissions of that installation. A later chapter walks through the exchange.
Apps compared with fine-grained PATs
| Aspect | Fine-grained PAT | GitHub App |
|---|---|---|
| Owner | One user | The app, owned by a user or organization |
| Acts as | The user | `app[bot]` |
| Token lifetime | Chosen at creation, up to a policy limit | Installation tokens expire after one hour |
| Webhooks | None | Built in |
| Setup effort | A few clicks | Registration, key handling, token exchange |
| Survives staff changes | No | Yes |
Apps also have their own rate limits, which can scale with the number of repositories and users in an installation. A PAT shares the limits of its user.
Common mistakes
- Requesting too many permissions. Each installer sees the list. Broad requests slow approval and widen the damage if the key leaks.
- Treating the private key like a config value. Anyone holding it can mint tokens for every installation. Store it as a secret.
- Using the JWT for repository calls. The JWT is only for app-level endpoints. Repository calls need an installation token.
When to reach for an app
Choose an app when the software runs without a person present, serves several accounts, reacts to events, or should outlive any one employee. Choose a fine-grained PAT for a personal script or a quick one-off task.
Exercise
Your CI helper needs to comment on pull requests in 20 repositories across two organizations, and it must keep working when the developer who built it leaves. Why is a GitHub App a better fit than a fine-grained PAT, and which credential would the helper use to post the comments?
Show answer
A PAT is tied to one user, so it breaks when that person leaves, and a single PAT has one resource owner, so it cannot cover two organizations. A GitHub App is owned independently and can be installed on both organizations with access to the 20 repositories. To post comments, the helper authenticates with a JWT signed by the app's private key, exchanges it for an installation access token, and uses that token for the comment requests. The JWT alone cannot write to repositories.