---
title: prefect-github
---

Prefect-github makes it easy to interact with GitHub repositories and use GitHub credentials.

## Getting started

### Prerequisites

- A [GitHub account](https://github.com/).

### Install `prefect-github`

The following command will install a version of `prefect-github` compatible with your installed version of `prefect`. 
If you don't already have `prefect` installed, it will install the newest version of `prefect` as well.

```bash
pip install "prefect[github]"
```

Upgrade to the latest versions of `prefect` and `prefect-github`:

```bash
pip install -U "prefect[github]"
```

### Register newly installed block types

Register the block types in the `prefect-github` module to make them available for use.

```bash
prefect block register -m prefect_github
```

## Examples

In the examples below, you create blocks with Python code.
Alternatively, blocks can be created through the Prefect UI.

To create a deployment and run a deployment where the flow code is stored in a private GitHub repository, you can use the `GitHubCredentials` block.

A deployment can use flow code stored in a GitHub repository without using this library in either of the following cases:

- The repository is public
- The deployment uses a [Secret block](https://docs.prefect.io/latest/develop/blocks/) to store the token

Code to create a GitHub Credentials block:

```python
from prefect_github import GitHubCredentials


github_credentials_block = GitHubCredentials(token="my_token")
github_credentials_block.save(name="my-github-credentials-block")
```

### Access flow code stored in a private GitHub repository in a deployment

Use the credentials block you created above to pass the GitHub access token during deployment creation. The code below assumes there's flow code stored in a private GitHub repository.

```python
from prefect import flow
from prefect.runner.storage import GitRepository
from prefect_github import GitHubCredentials


if __name__ == "__main__":

    source = GitRepository(
        url="https://github.com/org/private-repo.git",
        credentials=GitHubCredentials.load("my-github-credentials-block")
    )

    flow.from_source(source=source, entrypoint="my_file.py:my_flow").deploy(
        name="private-github-deploy",
        work_pool_name="my_pool",
    )
```

Alternatively, if you use a `prefect.yaml` file to create the deployment, reference the GitHub Credentials block in the `pull` step:

```yaml
pull:
    - prefect.deployments.steps.git_clone:
        repository: https://github.com/org/repo.git
        credentials: "{{ prefect.blocks.github-credentials.my-github-credentials-block }}"
```

### Interact with a GitHub repository

You can use prefect-github to create and retrieve issues and PRs from a repository.
Here's an example of adding a star to a GitHub repository:

```python
from prefect import flow
from prefect_github import GitHubCredentials
from prefect_github.repository import query_repository
from prefect_github.mutations import add_star_starrable


@flow()
def github_add_star_flow():
    github_credentials = GitHubCredentials.load("github-token")
    repository_id = query_repository(
        "PrefectHQ",
        "Prefect",
        github_credentials=github_credentials,
        return_fields="id"
    )["id"]
    starrable = add_star_starrable(
        repository_id,
        github_credentials
    )
    return starrable


if __name__ == "__main__":
    github_add_star_flow()
```

## Resources

For assistance using GitHub, consult the [GitHub documentation](https://docs.github.com).

Refer to the `prefect-github` [SDK documentation](https://reference.prefect.io/prefect_github/) to explore all the capabilities of the `prefect-github` library.
