GH cli - how to get current repo?
Asked Answered
W

2

5

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?

Whatnot answered 22/3, 2022 at 13:27 Comment(1)
What do you mean with "Current repo"?Arevalo
A
11

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.

Arnica answered 22/3, 2022 at 15:37 Comment(2)
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.Psychosocial
If you'd like the fullname you can also use gh repo view --json nameWithOwner -q ".nameWithOwner"Threshold
S
1

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

Salomie answered 11/7, 2023 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.