1. 快速上手
- 登录GitHub创建一个新项目
项目名为[username].github.io,其中[username]换成自己的GitHub用户名。
权限设为Public。
- 克隆到本地,并创建一个HTML
1
2
3
| git clone https://github.com/username/username.github.io.git
cd username.github.io
echo "Hello World" > index.html
|
- 将新内容提交到远程仓库
1
2
3
| git add .
git commit -m "Initial commit"
git push -u origin main
|
- 访问
https://username.github.io
2. Hugo集成
使用Hugo和GitHub Actions持续集成部署到GitHub Pages。
- 创建
.github/workflows/gh-pages.yml文件,配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| name: GitHub Pages
on:
push:
branches:
- main # Set a branch name to trigger deployment
pull_request:
jobs:
deploy:
runs-on: ubuntu-20.04
permissions:
contents: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
steps:
- uses: actions/checkout@v3
with:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.101.0'
extended: true
- name: Build
run: hugo --minify
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
if: ${{ github.ref == 'refs/heads/main' }}
with:
deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # secret 中设置好私钥
external_repository: username/username.github.io # Page 仓库
publish_branch: main # Page 仓库的分支
publish_dir: ./public # 静态网页路径
|
- 生成秘钥对,用于设置ACTIONS_DEPLOY_KEY。
1
| ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f gh-pages
|
将公钥添加到username/username.github.io仓库中Settings->Deploy Keys->Add depoly key,
命名为ACTIONS_DEPLOY_KEY_PUB。
将私钥添加到Hugo仓库中Settings->Secrets->Actions->New repository secret,
命名为ACTIONS_DEPLOY_KEY。
查看Hugo仓库->Actions持续集成是否成功,查看username/username.github.io仓库是否部署成功,
访问https://username.github.io/查看是否正常显示,正常则部署完毕。