Featured image of post hugo搭建遇到问题解决方案

hugo搭建遇到问题解决方案

hugo搭建过程中踩过的坑

本文用于记录将hugo博客部署至github托管过程中遇到的问题及解决办法,希望能帮助和我一样的萌新避坑

托管过程(github action教程)

创建仓库

首先在github创建两个仓库,一个用于存储博客源文件,文件名随意,这里用blog-file,这个设置为private私密;另一个仓库用github官方托管域名username.github.io来命名,设置为公开,如下图所示:

仓库

本地获取ssh密钥

终端输入命令:

git config --global user.email "you@example.com"   #you@example.com替换为你的邮箱并回车
git config --global user.name "Your Name"          #Your Name替换为你的名字并回车

生成ssh key,在git bash中输入以下命令:

ssh-keygen -t rsa

mac生成的密钥将存储在~/.ssh路径下

打开公钥文件 id_rsa.pub/id_rsa_work.pub, 复制所有内容,在GitHub上打开Setting -> SSH and GPG keys -> add SSH key,将复制的内容粘贴在里边,保存。

创建github token

在 GitHub 账户下 Setting - Developer setting - Personal access tokens(classic) 下创建一个不过期(no expiration)的 Token,权限需要开启 repoworkflow

token1

(注意:token只会显示一次,请及时保存)

token

私密源仓库设置token

在博客源仓库(blog-file)的 Settings - Secrets - Actions 中添加 PERSONAL_TOKEN 环境变量为刚才的 Token,这样 GitHub Action 就可以获取到 Token 了。

personal-token

创建workflows发布文件

在本地博客主目录下创建 .github/workflows目录,然后创建deploy.yml文件。我的 GitHub Action 配置为,自动发布示例配置如下:

name: deploy

on:
    push:
    workflow_dispatch:
    # schedule:
        # Runs everyday at 8:00 AM
        # - cron: "0 0 * * *"

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2
              with:
                  submodules: true
                  fetch-depth: 0

            - name: Setup Hugo
              uses: peaceiris/actions-hugo@v2
              with:
                  hugo-version: "latest"
                  extended: true
                  # 这里最好与本机hugo版本一致,例如:hugo-version: "0.121.2"
                  # 有时github部署会自动更新hugo版本导致部署失败
                  # extended来控制是否为扩展版本

            - name: Build Web
              run: hugo

            - name: Deploy Web
              uses: peaceiris/actions-gh-pages@v3
              with:
                  personal_token: ${{ secrets.PERSONAL_TOKEN }}
                  external_repository: Bla1n/Bla1n.github.io
                  publish_branch: main
                  publish_dir: ./public
                  commit_message: ${{ github.event.head_commit.message }}

external_repository替换为自己的公开博客git

本地快捷push文件

在本地博客根目录文件夹下新建一个名为deploy.sh的文件用于一键部署博客,在deploy.sh中填入如下内容,其中最后一行代码里的git@github.com:Bla1n/blog-file.git需要更改为你自己仓库的地址:

hugo #构造你的blog

git init #初始化git

git add -A

git commit -m 'deploy'

git push -f git@github.com:Bla1n/blog-file.git main #向存储库推送

初次使用

之后再本地调试完博客后,即可在终端运行如下命令则可以一键部署到github

bash deploy.sh

配置自己的域名

等待Github Action完成后,我们需要开启GitHub page,首先进入公开博客仓库,然后打开设置,在如图示位置进行设置(不会的可以自行搜索)

domain

生效结果

action

push仓库会重置域名问题

每次push后会发现公开博客仓库设置的自己的域名会被重置为默认域名

解决方法:在content目录下创建一个名为CNAME的文件,内容填入自己的域名即可

CNAME

本地push失败彻底解决办法

在本地push几次后,突然有一天push不成功了,报错类似如下(忘记截图了)

ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from remote repo

解决办法:在~/.ssh路径下,创建一个名为config的文件,内容如下:

Host github.com
User xxxxqq.com(这里换成自己的邮箱)
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443

这样默认push使用https协议,即可成功。

deploy突然失败

平常更新都能deploy成功,但有一次上传文章突然就失败了。起初以为是文章的原因,结果回退commit版本依然是部署失败,报错如下:

ERROR TOCSS: failed to transform "/scss/style.scss" (text/x-scss). Check your Hugo installation; you need the extended version to build SCSS/SASS with transpiler set to 'libsass'.: this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information
Total in 539 ms
Error: error building site: TOCSS: failed to transform "/scss/style.scss" (text/x-scss). Check your Hugo installation; you need the extended version to build SCSS/SASS with transpiler set to 'libsass'.: this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information
Error: Process completed with exit code 1.

error

按照报错信息把网上的教程都试了一遍,都不行,然后仔细看了一眼报错,说我没使用扩展版本的hugo,但是我本地是用的扩展版本,于是检查配置文件。发现wordflows的配置文件关于hugo版本是这样写的:

- name: Setup Hugo
    uses: peaceiris/actions-hugo@v2
    with:
      hugo-version: "lastest"

日志上的hugo版本果然不对:

vision

随即查看hugo action官方使用指南,正确写法应该如下:

- name: Setup Hugo
  uses: peaceiris/actions-hugo@v2
  with:
    hugo-version: "0.121.2"
    # 这里最好与本机hugo版本一致,例如:hugo-version: "0.121.2"
    # 有时github部署会自动更新hugo版本导致部署失败
    extended: true
    # extended来控制是否为扩展版本
本博客已稳定运行
发表了13篇文章 · 总计29.11k字
森 ICP备2024001234
Built with Hugo
主题 StackJimmy 设计