前言
搭建了hugo博客后,还需要一篇文章一篇文章的git更新,很麻烦,研究了一会,找到了可以利用github Action自动部署的方法
本地设置
文章设置
- 1.打开cmd命令行,使用hugo new content post/文章名/index.md创建基本文章文件,如果成功就会出现以下结果:
注意:
-
在blog主文件路径栏输入’cmd’,不要在其他路径下!
-
文章创建后在blog文件(如上图的blog)内的content/post/你创建的文章名内,可以把文章文件放到此文件夹中,再在markdown中引用文件或其他操作!
远程设置
Github 设置
-
本人推荐使用Github desktop软件进行最后一步push步骤(因为我也只能用这个才能push上,用git push不行)本文将介绍使用Github desktop的方法,部分引用letere的文章
-
1.前往
Setttings -> Developer Settings -> Personal access tokens
,创建一个token(classic) -
2.token选择永不过期,并勾选 repo 和 workflow 选项
-
3.为保证安全,将生成的token,保存的仓库的变量中,前往
Settings -> Secrets and variables -> Actions
中设置
-
4.在hugo主文件创建一个
.github/workflows/xxxx.yaml
文件,将以下内容复制进去,想具体了解更多,可查看【Github Action文档】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
name: deploy # 代码提交到main分支时触发github action on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Setup Hugo uses: peaceiris/actions-hugo@v3 with: hugo-version: "latest" extended: true - name: Build Web run: hugo -D - name: Deploy Web uses: peaceiris/actions-gh-pages@v4 with: PERSONAL_TOKEN: ${{ secrets.你的token变量名 }} EXTERNAL_REPOSITORY: 你的github名/你的仓库名 PUBLISH_BRANCH: main PUBLISH_DIR: ./public commit_message: auto deploy
git设置
-
1.在blog主文件右键新建文本文档,命名为.gitignore
-
2.在.gitignore中输入以下内容,来避免提交不必要的文件:
1 2 3 4 5 6 7
# 自动生成的文件 public resources .hugo_build.lock # hugo命令 hugo.exe
-
3.在blog主文件右键选择Open git bush here
-
4.然后在此页面分别输入:
|
|
-
5.然后打开"Github desktop"软件(推荐用地球魔法)
-
6.选择Add existing repository导入blog主文件
-
7.点击New branch创建新分支main(分析:代码内使用main分支,而desktop内默认使用master分支,导致上传后Action失败)

-
8.点击旁边或下面蓝色按钮“publish repository”类似字样

-
9.填写第一栏为“hugo-main”后点击“Publish repository”上传至远程仓库

- 10.如果一切正常在仓库的Actions中会显示一个workflows正常运行run(打勾)
- 这时文章就被上传了,此教程内步骤:文章设置和git设置1-5、8(点击Fetch origin)可重复操作以实现自动部署