Search code examples
githubgithub-pages

How can I find the repository that has GitHub Pages enabled and is on a given domain?


I have 200+ GitHub repositories, and one of them has GitHub Pages enabled on it, with a domain name that belongs to me.

Now I wanna redo that website and hence assign that domain name to the GitHub Pages of a new repository.

Only issue is I cannot for the life of me figure out which of my myriad repositories is linked with that domain name.

How can I figure this out?


Solution

  • If you happened to have set the "website" setting in the "About" section of the repo, you could figure it out like this, using the GitHub CLI:

    gh repo list \
        --limit 500 --json name,homepageUrl --jq 'map(select(.homepageUrl != ""))'
    

    This shows you a list of all repos that have a homepage set.

    However, that's a setting enabled manually, and you can put any URL in there, so it might not return useful results.

    In that case, you could iterate over every repository, and try to request the Pages site via REST API. If that succeeds, there is a page associated with the repo.

    This snippet does that, suppressing errors from the call to the /pages endpoint; remove 2> /dev/null to see all the output.

    while IFS= read -r repo; do
        if url=$(gh api "repos/$repo/pages" --jq '.html_url' 2> /dev/null); then
            printf '%s uses %s\n' "$repo" "$url"
        fi
    done < <(gh repo list --limit 500 | awk '{print $1}')