Back to posts

Git over HTTPS with a GitHub App

A job that clones owner/private-repo needs read access to that repository. I want to give that access to the job without tying it to my GitHub account.

A GitHub App lets me do that, but git clone cannot turn an App’s private key into a token. Rather than add a token step to every script, I wrote git-credential-github-app. The scripts still call git clone. Git calls the helper when it needs credentials.

What Git sends to the helper

For an HTTPS clone, Git sends the helper a request like this. The two examples show the same clone, with useHttpPath off and on:

Default                    With useHttpPath = true
protocol=https             protocol=https
host=github.com            host=github.com
                           path=owner/private-repo.git

Without the path, the helper knows the host but not the repository. With the path, the exchange looks like this:

How Git gets a GitHub App token

Git sends the repository path to the credential helper. The helper asks GitHub for a token for that repository. GitHub returns the token, and the helper passes it back to Git as the HTTPS password.

GitHelperGitHubRepository pathRequest repo tokenShort-lived tokenHTTPS password

An installation gives the App access to a set of repositories. If the App can also read owner/another-repo, the token for owner/private-repo still cannot read it. The token expires after an hour. The private key does not, and anyone who can read it can request tokens for the other repositories where the App is installed.

Put the helper in Git’s configuration

For a first run, give the App Contents: read and install it on one private test repository. Save the private key outside the checkout with file mode 0600. Download the helper and put git-credential-github-app on your PATH.

If the job has its own user account, add this block to that user’s ~/.gitconfig. Replace the App ID and key path:

[credential "https://github.com"]
	helper =
	helper = github-app --app-id 123456 --private-key ~/.config/github-app/key.pem
	useHttpPath = true

The empty helper = clears the helpers Git read before this block. Without it, a helper that holds your personal credentials can answer first. The next line selects the App helper. useHttpPath gives it the repository path shown above.

If your version includes setup, let it fill in the block for you:

git-credential-github-app setup --app-id 123456 --private-key ~/.config/github-app/key.pem

setup prints the block without saving it. To apply it from an existing checkout, add --write --local. That leaves ~/.gitconfig alone. If the repository already sets a credential helper or useHttpPath, setup refuses to write.

On a shared user account, prefer the repository’s .git/config over ~/.gitconfig so the change does not apply to your other repositories. Local configuration is only available after the first clone. For that first clone, use a dedicated user or the per-command configuration.

Test it without cloning

Run this as the user that runs the job. If you used local configuration, run it from that checkout. Replace the URL with your private test repository:

git ls-remote https://github.com/OWNER/PRIVATE-REPO.git

For a repository with a main branch, expect a line like this. The first column is the commit ID:

8afe89ad2be071e6c250703ae0f8b53be1a6d460	refs/heads/main

git ls-remote reads the branch and tag references without changing the repository or printing the token. Use a private repository because a public one can answer without credentials. If Git asks for a username or password, stop there. Entering your personal credentials would test a different login.

If a job already supplies an App token to every Git command that needs one, I would leave it alone. The helper is useful when existing scripts already use HTTPS and I want to change how Git signs in without changing those scripts.