- From Website: GitHub CLI
- From Homebrew:
brew install gh - Others: GitHub CLI Installation Docs
- Either login using
gh auth loginor setGITHUB_TOKEN - For Enterprise Server add
--hostname <hostname> - Troubleshooting:
gh auth status
gh api repos/aatmmr/github-training-api/issues --paginate --jq length## Usage
Get issue count of a repository:
The query string can be added inline
gh api graphql -F login='octokit' -f query='
query($login: String!) {
organization(login: $login) {
repositories(first: 20) {
nodes { name }
}
}
}
'or as file
gh api graphql -F login=octokit -F query=@src/graphql/list-repos.graphql --paginateBe aware that to add a @ in front of the file name and that any parameter that is used in the query in the file must be added as -F parameter in the CLI command to be passed in to the query.
The CLI has a long list pf build in commands that wrap API calls. For example, you can list issues of a repository:
gh issue list --repo octokit/octokit.js This call is independent of the context you are in as the desired repository is defined via --repo.
The CLI is aware of the context you are in. For example, if you are in a repository folder, the CLI will use the repository as the default context.
Listing the issues of the repository gets as simple as
gh issue listListing the secrets of the repository gets as simple as
gh secret listThe GitHub CLI can be used in GitHub Action Workflows as it is preinstalled on GitHub hosted runners. Simply use the gh command in the workflow in a run step.
An example can be found in CLI Demo Workflow.
Important
Make sure to checkout the repository before using the gh command as the CLI needs the repository to be present to know the context.
Important
The GITHUB_TOKEN has to be passed to the gh command as an environment variable (env) to authenticate the command. The token is automatically provided in the workflow with the restricted scope of the repository the workflow is executed from.
- List installed extensions:
gh extension list - Browse catalogue:
gh extension browse - Install an extension:
gh extension install <extension>