步骤
安装软件
- git
1 2 3
| git config --global user.name "your username" git config --global user.email "your email"
|
- hugo
hugo 创建本地 website
参考:https://gohugo.io/getting-started/quick-start/
- hugo new site blog # blog 根据需要修改
- cd blog
- git init
- git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke # theme 根据需要修改
- 把 content/posts 目录修改为 content/post
- hugo new post/my-first-post.md
- hugo server -D
- 根据选择的主题,修改主题相关的配置
github 上创建两个公开仓库
username.github.io
, 部署静态博客的仓库,username 修改为对应的 github 账号 ID。blog
,这个保存本地的 blog
的仓库。- 在
blog
中执行以下命令:1 2 3
| git remote add origin git@github.com:username/blog.git git branch -M main git push -u origin main
|
配置 github actions
参考:https://github.com/peaceiris/actions-gh-pages#%EF%B8%8F-first-deployment-with-github_token
使用 actions-hugo 和 actions-gh-pages 自动化部署 blog
生成的密钥对本地保存就可以。
- 生成一对 public key(gh-pages.pub) 和 private key(gh-pages)
1 2 3
| ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f gh-pages -N ""
|
- 添加公钥
gh-pages.pub
添加到 username.github.io 这个仓库的 deploy key 中,勾选 Allow write access
https://github.com/username/username.github.io/settings/keys/new
- 添加私钥
gh-pages
到 blog
中的 secret
https://github.com/username/blog/settings/secrets/actions/new
Name
ACTIONS_DEPLOY_KEYValue
gh-pages 私钥文件内容
本地 blog
添加 deploy.yml
- mkdir -p .github/workfows/
- touch .github/workflows/deploy.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
| name: GitHub Pages on: push: branches: - main pull_request: jobs: deploy: runs-on: ubuntu-20.04 concurrency: group: ${{ github.workflow }}-${{ github.ref }} steps: - uses: actions/checkout@v3 with: submodules: true fetch-depth: 0 - name: Setup Hugo uses: peaceiris/actions-hugo@v2 with: hugo-version: '0.91.2' - name: Build run: hugo --minify - name: Deploy uses: peaceiris/actions-gh-pages@v3 with: deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} external_repository: username/username.github.io publish_branch: master publish_dir: ./public
|
提交 本地 blog 到 github blog 仓库
1 2 3
| git add . git commit -m "commit info" git push
|
Reference
- github pages
- hugo quickstart
- actions-hugo
- actions-gh-pages
- deploy-to-external-repository-external_repository
Author
stardust
LastMod
2022-03-19
Markdown
The Markdown version »