
搭建hugo博客
安装 Hugo 博客
安装 hugo:
Windows:
1 | choco install hugo --confirm |
macOS:
1 | brew install hugo |
创建新站点:
1 | hugo new site blog |
切换到 blog
1 | cd blog |
在当前目录中初始化一个空的 Git 存储库
1 | git init |
安装博客主题:
1 | git clone https://github.com/HEIGE-PCloud/DoIt.git themes/DoIt |
创建第一篇博文:
1 | hugo new posts/first_post.md |
开启本地服务:
1 | hugo server |
访问 http://localhost:1313 本地查看
发布站点到 GitHub 上
- 生成 public 文件夹
1 | hugo --theme=DoIt --baseURL="https://tsaoshuang.github.io/" --buildDrafts |
或者不加参数只用 hugo 命令 这个命令将生成一个 public 文件夹,其中包含您网站的所有静态内容,可以将其部署在任何 Web 服务器上。
- git 操作(切换至 public 目录进行操作)
1 | git init |
遇到的问题:
运行 git remote add origin 时出现错误: failed to push some refs to
如何修复此问题 运行以下命令:
1 | git pull --rebase origin master |
再运行:
1 | git push -u origin master |
更新博客内容的命令
新建博文
1 | hugo new post/FileName.md |
更新配置文件之后重新用 hugo 命令生成新文件
1 | hugo --theme=DoIt --baseURL="https://tsaoshuang.github.io/ |
或更新后只需要在 博客根目录运行
1 | hogo |
命令
然后切换到 public 文件夹
1 | cd public |
提交:
1 | git add . |
自动化部署
利用 Github Actions 实现将 hugo 站源文件自动化部署到 GitHub Pages 上
创建 Github Actions 文件
在站点源文件根目录,创建 .github/workflows/deploy.yml 文件:
1 | # This is a basic workflow to help you get started with Actions |
另一个自动化脚本:
1 | name: deploy |
其中,on: push 表示提交时触发 Action,自动编译并部署到 GitHub Pages。 以后提交个人站点内容时,会自动编译并部署。
绑定域名
除了在域名管理中绑定域名 cname 之外,还要在 GitHub setting 中的 pages 设置中绑定并验证域名 txt,这样域名才会生效 还有要在 Hugo 仓库的根目录新建一个 CNAME 文件里面写上你要绑定的域名或者子域名不带协议头的那种!
本文是原创文章,采用CC BY-NC-SA 4.0协议,完整转载请注明来自Readfere
评论