GitHub access for deploys: token, repo, host app
Three separate kinds of GitHub access sit behind a deploy. The fine-grained token permissions, the API calls that fill a repo, and the host app.
Three credentials, three jobs
Getting from a repository to a site that deploys on every push involves three separate kinds of GitHub access, and they fail independently. You push code with your own login. A setup tool creates the repository and commits files with a personal access token. Your host reads the code and receives push notifications through a GitHub App installed on your account. When a push does not deploy, the token is almost never the cause: the host's app cannot see the repository.
| Who | Credential | Used for | Stops working when |
|---|---|---|---|
| You | Your GitHub login, over SSH or HTTPS | Pushing commits | You lose access to the repository |
| A setup tool | Fine-grained personal access token | Creating the repository, committing the first files | The token expires, or an organisation has not approved it |
| The host (Railway, Vercel) | A GitHub App installation | Reading code, reacting to pushes | The repository is not in the app's access list |
The token: two permissions you choose, one GitHub adds
GitHub has two kinds of personal access token. A classic token carries broad scopes, and its repo scope covers every repository you can reach. A fine-grained token is tied to one owner, can be limited to chosen repositories, and takes individual permissions. GitHub recommends fine-grained wherever it works, and for creating a repository and committing to it, it does.
- Leave everything else at No access, in particular Secrets, Workflows and Members. A setup tool has no use for them.
- When a call is refused, GitHub's answer includes a header named
X-Accepted-GitHub-Permissionslisting what the endpoint wanted. That is quicker than guessing.
| Permission | Level | Needed for |
|---|---|---|
| Administration | Read and write | Creating a repository, and deleting it if you ever ask a tool to. |
| Contents | Read and write | Committing files, and reading a file such as .env.example. |
| Metadata | Read-only | Required on every fine-grained token. |
Creating it, with the form filled in for you
GitHub lets a link pre-fill the token form through query parameters, which removes most of the room for mistakes. name and description set the text fields, target_name picks the owner, expires_in takes a number of days from 1 to 366 or none, and each permission is its own parameter set to read or write. This is the kind of link Shipyard shows you.
- Open the link, or go to Settings, Developer settings, Personal access tokens, Fine-grained tokens, Generate new token.
- Resource owner: your own account, or the organisation the repositories should live in. A fine-grained token covers one owner only.
- Expiration: choose a date you will notice. GitHub allows tokens without expiry, though an organisation can forbid them.
- Repository access: for a token that creates repositories, choose All repositories. The repository does not exist yet, so it cannot be picked from the Only select repositories list.
- Permissions: confirm Administration and Contents are both Read and write.
- Press Generate token and copy the value. It is shown once.
https://github.com/settings/personal-access-tokens/new?name=Shipyard&administration=write&contents=writeIf the repositories belong to an organisation
Organisations have their own policy for fine-grained tokens, under the organisation's Settings, Personal access tokens, Settings, on the Fine-grained tokens tab. By default they are allowed, and by default each one needs an administrator's approval. That second default explains a confusing state: a member creates the token, it looks normal, and every call to a private repository fails. Until an owner approves it the token is pending and can only read public resources. Tokens created by an organisation owner are approved automatically.
The same page sets a maximum lifetime, 366 days unless changed, and a token with a longer life is blocked from the organisation without being revoked.
Filling a repository through the API, without git
A setup tool does not need git on your machine to put a starter into a new repository. Shipyard does it with six REST calls, and two details in the sequence took us a while to get right.
The first is auto_init. The low-level Git endpoints answer 409 Conflict on a repository that is empty, because there is no commit to build on. Creating the repository with auto_init set to true makes GitHub add a first commit, and the rest works. The second is timing. Straight after creation the branch reference can still be missing for a moment, so the read of heads/main is retried a few times, a second and a half apart, before giving up.
From there it is ordinary Git, spelled out. Read the commit the branch points at, create a tree that starts from that commit's tree and adds your files, create a commit with the old one as its parent, and move the branch to it. All files land in one commit. There is a useful side effect: if the new tree has the same hash as the old one, nothing changed, and the tool can stop without creating an empty commit. That is what makes running it twice harmless.
POST /user/repos {"name":"acme","private":true,"auto_init":true}
GET /repos/{owner}/{repo}/git/ref/heads/main
GET /repos/{owner}/{repo}/git/commits/{sha}
POST /repos/{owner}/{repo}/git/trees {"base_tree":"<tree sha>","tree":[{"path":"README.md","mode":"100644","type":"blob","content":"..."}]}
POST /repos/{owner}/{repo}/git/commits {"message":"Add the starter","tree":"<new tree sha>","parents":["<sha>"]}
PATCH /repos/{owner}/{repo}/git/refs/heads/main {"sha":"<new commit sha>"}How Shipyard uses GitHub
Shipyard asks for a fine-grained token with exactly the permissions above and keeps it in the keychain on your computer. With it, Shipyard creates the repository, private unless you pass --public, commits the Next.js starter in one commit using the calls shown, and hands the repository name to the host so pushes to the default branch deploy. If you name an organisation, it checks first that the token can see it.
With --repo owner/name it uses a repository you already have. In that mode it writes nothing to it: it only reads .env.example (or .env.sample) to learn which variable names your app expects. Teardown leaves repositories alone unless you add --include-repo, and it never deletes one it did not create.
Installing the host's GitHub App is yours to do, because it happens in your GitHub account, in the browser. Shipyard tells you before the launch starts, and if the host still cannot see the repository it stops at that step with the link to fix it.
The host's app is where deploys actually break
Railway and Vercel each install a GitHub App on your account or organisation. During installation GitHub asks one question that decides everything later: All repositories or Only select repositories. With the second choice, a repository created afterwards is not on the list, the host cannot see it, and pushes to it do not start a deploy.
To check or change it for a personal account, go to Settings, then Applications under Integrations, then Installed GitHub Apps, and press Configure next to the app. For an organisation it is the organisation's Settings, then GitHub Apps under Third-party Access. Under Repository access add the repository, or switch to All repositories, and save. If you create products often, All repositories on a dedicated organisation is the arrangement that never surprises you.
Each host adds a rule of its own. Vercel only lets the owner of a personal repository connect it; a collaborator cannot. Railway needs a project member whose connected GitHub account has contributor access, and its troubleshooting list tells you to check that the app has no pending permission update waiting in GitHub.
Symptoms and where to look
| Symptom | Usual cause | Check |
|---|---|---|
| 404 from the API for a repository you can see in the browser | GitHub answers 404, not 403, when a token lacks access to a private repository. | The token's resource owner and repository access. |
| "Resource not accessible by personal access token" | A permission is missing. | The X-Accepted-GitHub-Permissions header on the failed response. |
| Token works on your own repositories, not the organisation's | Pending approval, or the resource owner is your personal account. | The organisation's personal access token requests. |
| 409 when committing through the API | The repository is empty. | Create it with auto_init, or add a first file. |
| A classic token is rejected by a tool | It lacks the repo scope. | The scopes listed next to the token. |
| Push succeeds, no deploy | The host's app cannot see the repository, or the host watches another branch. | Installed GitHub Apps, then the host's branch setting. |
| The token vanished from your settings | GitHub removes tokens that have not been used for a year. | Create a new one. |
What expires, and what keeps running
A setup token and a deploy pipeline have different lifetimes, which is easy to forget on the day the token expires. Deploys run through the host's GitHub App, not through your token, so an expired or deleted token changes nothing for a product that is already live. Only the tool that creates repositories stops, until you give it a new token. That also means you can keep the token short-lived without risk to production.
Sources
- GitHub Docs: Managing your personal access tokensdocs.github.com
- GitHub Docs: Setting a personal access token policy for your organizationdocs.github.com
- GitHub Docs: Using the REST API to interact with your Git databasedocs.github.com
- GitHub Docs: Reviewing and modifying installed GitHub Appsdocs.github.com
- GitHub Docs: Troubleshooting the REST APIdocs.github.com
- Vercel docs: Deploying GitHub projects with Vercelvercel.com
Launch in an hour, not a weekend
Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.
See how it works