GitHub CLI 是一个命令行工具,可以将GitHub 网站上的大部分功能引入终端,使用户可以在一个地方完成所有工作,避免切换页面以达到节省时间提高效率的目的。具体功能见下表
gh <command> <subcommand> --help
可以获取详细帮助
命令 | 作用 |
---|---|
alias | 别名可以用来为 gh 命令创建快捷方式。包括delete、list 和set 三个子命令。 |
api | 向 GitHub API 发出经过身份验证的 HTTP 请求并打印响应。 |
auth | 使用 GitHub 授权gh 和 git。登录方式包括web 和token 两种。 |
browse | 在浏览器打开仓库 |
codespace | Github codespaces 提供线上的编程环境。这个环境可以视为一台带vscode 和jupyter 的虚拟机,还可以使用ssh 连接。 |
config | 显示或更改 gh 的配置设置。 |
extension | 提供 gh 命令的扩展。 |
gist | Gist 用来存储代码片段,如果设置权限为私有则只能通过url 来访问,用一行js 命令即可实现将内容嵌入网页。 |
gpg-key | 管理在 GitHub 帐户中注册的 GPG 密钥。 |
issue | 处理 GitHub 的issues。包括创建、查看、关闭、删除、编辑和添加评论等操作。 |
label | 管理仓库的标签 |
pr | 处理仓库的pull requests。包括创建、查看、关闭、评论和编辑等操作。 |
release | 管理仓库的releases。包括创建、查看、编辑、删除和下载等操作 |
repo | |
run | 列出、查看和监视从 GitHub Actions 运行的最近工作流。 |
search | 搜索范围包括issues、prs和仓库。 |
secret | 列出、设置和删除秘钥。该秘钥可用于GitHub Actions 和GitHub Codespaces。 |
ssh-key | 列出、设置和删除ssh秘钥。 |
status | 打印仓库的issues、pr和评论。 |
workflow | GitHub Actions可用于项目构建、测试、打包、发布和部署。Workflow 命令可以管理GitHub Actions。 |
完整的功能可以参考手册。这里我们尝试在开发一个静态网站中使用GitHub CLI。这个网站使用create-react-app 模板,利用workflow 打包,最后使用Github page 发布。
安装及登录
Github Cli 支持macOS, Windows 和Linux。命令行安装参考(https://github.com/cli/cli#installation)二进制安装(https://github.com/cli/cli/releases/tag/v2.21.2)
安装成功后,在终端
按回车后将打开浏览器页面,输入终端的验证码,点击继续,之后确定授权。这里有可能需要移动端的github app验证。
最后终端显示✓ Logged in as [username]
暨成功登录。
还有另一种方式登录,在github 网站的Settings\Developer settings\personal access tokens 创建token,然后使用命令 gh auth login token
登录。
创建前端工程并建立仓库
我使用的npm 版本为9.2.0,node 版本为19.4.0。可以使用nvm(https://blog.youkuaiyun.com/qq_30376375/article/details/115877446)管理多个nodejs版本。
# 使用npm 安装create-react-app
npm install -g create-react-app
# 使用模板创建项目
create-react-app hello-react
# 进入项目目录
cd hello-react
# 以本地文件为初始内容,在github 创建一个名为hello-react 的仓库,分支名设置为master。
gh repo create hello-react --public --source=. --remote=master --push
在上面命令执行完成后,可使用命令 gh repo list
查看所有仓库,其中便包含我们刚刚创建的 hello-react
。
这时我们可以使用命令 npm run start
运行工程,打开 http://localhost:3000/hello-react 后如下图显示
将项目发布
如果没有一台服务器,那么我们做的网站只能在局域网里自娱自乐。这里github pages 提供了一个存放静态网站的地方。
那我们自己给自己提一个issue 吧
gh issue create --title "Good Job" --body "Publish it in Github Cli"
gh issue list
查看开启的issue
gh issue view [number]
查看指定编号的issue
修改package.json,添加 “homepage”: “https://[username].github.io/hello-react”,
创建./github/workflows/deploy.yml 文件
name: GitHub Actions Build and Deploy Demo
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3
- name: Install and Build 🔧 # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built.
run: |
npm ci
npm run build
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: build # The folder the action should deploy.
gh secret set ACCESS_TOKEN
修改成Hello world 再次push
修改/src/App.js
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Hello world
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
git add .
git commit -m "change text"
git push master
gh workflow view
https://callmebg.github.io/hello-react/
其它
GitHub Codespaces 是云端的开发环境 , 它允许开发人员通过浏览器或从本地的 Visual Studio Code IDE 直接进行调试、维护、更改、部署 GitHub 上的代码。
gh codespace create
? Repository: [username]/hello-react
? Branch (leave blank for default branch):
? Choose Machine Type: 2 cores, 4 GB RAM, 32 GB storage
[username]-improved-space-palm-tree-r79w6qq7v69hxr9w
# 默认使用本地vscode打开
gh codespace code
? Choose codespace: callmebg/hello-react (master): improved space palm-tree
# -w 使用浏览器打开
gh codespace code -w
使用起来与本地一样。每个月免费额度不高,所以用完得记得关闭。使用命令gh codespace stop 选择运行的codespace 关闭。
如果不在仓库中定义配置,GitHub 将使用默认的 Linux 镜像创建一个codespace 。这个 Linux 映像包含许多流行语言的运行时,如 Python、 Node、 PHP、 Java、 Go、 C++ 、 Ruby 和.NET Core/C #。使用这些语言的最新版本或 LTS 版本。还有一些工具可以支持数据科学和机器学习,比如 JupyterLab 和 Conda。该镜像还包括其他开发工具和实用程序,如 Git、GitHub CLI、yarn、 openssh 和 vim。要查看包含的所有语言、运行时和工具,可以在codespace 的终端输入devcontainer-info content-url,得到一个url。我创建的环境配置 https://github.com/devcontainers/images/blob/main/src/universal/history/2.1.3.md