GitHub CLI 命令行工具使用

标签: github   cli   devops  

github cli

GitHub 被巨软收购以后推出了一系列非常好用的开发者工具,比如前面我们使用过的 CI/CD 工具 GitHub Actions包管理工具 packages,今天我们要为大家介绍的是近来 GitHub 发布的又一个非常有用的工具: GitHub CLI,可以让开发者通过命令行于 GitHub 进行无缝的协同工作,也就是我们直接在命令行终端上就可以进行 pull requests、issues 等其他功能,现在已经发布 Beta 版本,我们可以在 macOS、Windows 或者 Linux 平台上安装 GitHub CLI。

安装

要安装 GitHub CLI 非常简单,比如我们这里在 macOS 下面依然可以用 Homebrew 工具进行安装:

$ brew install github/gh/gh
# 如果需要更新执行下面的命令即可
$ brew update && brew upgrade gh

安装完成后直接在命令行中执行 gh 命令,看到如下所示的信息就证明已经安装完成:

$ gh
> GET /repos/cli/cli/releases/latest
Work seamlessly with GitHub from the command line.

GitHub CLI is in early stages of development, and we'd love to hear your
feedback at <https://forms.gle/umxd3h31c7aMQFKG7>

Usage:
  gh [command]

Available Commands:
  help        Help about any command
  issue       Create and view issues
  pr          Create, view, and checkout pull requests

Flags:
      --help              Show help for command
  -R, --repo OWNER/REPO   Select another repository using the OWNER/REPO format
      --version           Show gh version

Use "gh [command] --help" for more information about a command.

其他平台的安装参考官方文档即可: https://cli.github.com/manual/installation

使用

下面我们以 issue 和 pull requests 两个开发者使用非常频繁的功能为例来介绍下 GitHub CLI 的基本使用。从 GitHub 上面 Clone 一个项目到本地,然后在项目目录下面执行 gh 相关的命令,比如我们这里就在博客文章的项目下面来进行演示,项目地址:https://github.com/cnych/qikqiak.com

列表过滤

我们可以使用 gh 命令来过滤 issue,比如过滤带有 gitment 标签的问题:

$ gh issue list  --label "gitment"
> GET /repos/cli/cli/releases/latest
Notice: authentication required
Press Enter to open github.com in your browser... < HTTP 200 OK

在第一次使用的时候需要我们进行一次授权,在命令行中输入回车键就会在浏览器中打开授权页面,点击授权即可:

github authorization

授权完成后回到终端中输入回车键即可得到结果:

$ gh issue list  --label "gitment"
[git remote -v]
> GET /repos/cli/cli/releases/latest
> POST /graphql
< HTTP 200 OK
< HTTP 200 OK

Issues for cnych/qikqiak.com

> POST /graphql
< HTTP 200 OK
#152  Kubernetes 零宕机滚动更新                                          (gitment, zero-downtime-rolling-update-k8s)
#151  在 Kubernetes 集群上部署 VSCode                                   (deploy-vscode-on-k8s, gitment)
#150  自定义 Traefik2 中间件                                            (custom-traefik2-middleware, gitment)
#149  基于 Jenkins 的 DevOps 流水线实践                                   (devops-base-on-jenkins, gitment)
#148  自定义 Kubernetes 调度器                                          (custom-kube-scheduler, gitment)
#146  一文搞懂 Traefik2.1 的使用                                         (gitment, traefik-2.1-101)
......

上面的命令即可将带有 gitment 标签的 issue 过滤出来。

快速查看详情

找到一个我们关心的 issue 过后,要想查看该 issue 的详细信息,可以使用如下命令在浏览器中快速将 issue 的详细信息页面打开:

$ gh issue view 152
[git remote -v]
> POST /graphql
< HTTP 200 OK
> POST /graphql
< HTTP 200 OK
Opening https://github.com/cnych/qikqiak.com/issues/152 in your browser.
[open https://github.com/cnych/qikqiak.com/issues/152]

创建 PR

创建一个分支,在提交几次代码后修复了 issue 中描述的 BUG 后,然后可以使用 gh 命令来创建一个 pull request 来提交我们贡献的代码:

gh pr create
[git remote -v]
> POST /graphql
< HTTP 200 OK
[git rev-parse --abbrev-ref HEAD]
[git status --porcelain]
[git push --set-upstream origin HEAD:gh-pages]

Creating pull request for gh-pages into master in cnych/qikqiak.com

[git rev-parse --show-toplevel]
? Title Update gitignore
? Body <Received>
? What's next?  [Use arrows to move, type to filter]
> Preview in browser
  Submit
  Cancel

我们还可以通过键盘上的上下方向键来移动进行动作选择,可以跳转到浏览器中进行操作,也可以直接提交或者取消。

状态查看

当我们创建了一个 pull request 过后要想快速了解其状态,同样可以使用 gh 命令来显示 pull requests 的 review 和状态:

$ gh pr status
[git remote -v]
[git rev-parse --abbrev-ref HEAD]
[git config --get-regexp ^branch\.gh-pages\.(remote|merge)$]
> POST /graphql
< HTTP 200 OK
> POST /graphql
< HTTP 200 OK

Relevant pull requests in cnych/qikqiak.com

Current branch
  #153  Update gitignore rules [gh-pages]
   - Checks passing

Created by you
  #153  Update gitignore rules [gh-pages]
   - Checks passing

Requesting a code review from you
  You have no pull requests to review

这里我们只是简单介绍了 issue 和 pull requests 的几个常用命令,更多的使用方式可以查看官方文档了解更多:https://cli.github.com/manual/examples

微信公众号

扫描下面的二维码关注我们的微信公众帐号,在微信公众帐号中回复◉加群◉即可加入到我们的 kubernetes 讨论群里面共同学习。

wechat-account-qrcode

「真诚赞赏,手留余香」

阳明

请我喝杯咖啡?

使用微信扫描二维码完成支付

相关文章