4

Use GitHub cli (gh), how do I get a current repo name?

I can get a listing of the repos I have but how do I get the current repo?

DFBerry
  • 1,818
  • 1
  • 19
  • 37

2 Answers2

9

To get the name of the repo in the curernt working directory, run

gh repo view --json name -q ".name"

This extracts the name of the repo from the command that lists information about the repo in the current directory, and extracts the actual name from the JSON output. For instance, if you were in the working directory for the repo with a URL of http://github.com/rust-lang/rust, the output would be rust.

smitop
  • 4,770
  • 2
  • 20
  • 53
  • 1
    I just stumbled across the same problem. While your solution works, it does an API request that should not be required. However, I didn't find a way to do it without the API request either. – Andreas Rogge Dec 16 '22 at 15:19
  • 3
    If you'd like the fullname you can also use `gh repo view --json nameWithOwner -q ".nameWithOwner"` – Kyle James Walker Jan 11 '23 at 19:19
0

In github actions, there are environment variables that provide this information. In particular:

  • GITHUB_REPOSITORY The owner and repository name. For example, octocat/Hello-World.
  • GITHUB_REPOSITORY_OWNER The repository owner's name. For example, octocat.

From this you can get the name without the owner:

  • ${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/} The repository name. For example, Hello-World

For a full list of variables, see: https://docs.github.com/en/actions/learn-github-actions/variables

Max Murphy
  • 1,701
  • 1
  • 19
  • 29