零成本从0到1搭建个人博客

测试环境:Windows 11 专业版 + Git 2.45.1

下载Hugo

点击进入以下网页:github.com/gohugoio/hu...

可按需选择版本,如v0.154.5。

创建个人博客

解压这个压缩包,进入到解压后的文件夹,右键进入命令行界面,输入以下命令【hugo new site CodeEdge】,其中【CodeEdge】名字可任意取。

将【hugo.exe】文件复制到【CodeEdge】文件夹中,再cd到CodeEdge目录,执行以下命令启动服务【hugo server -D】

配置个人博客的主题

点击进入以下网页github.com/CaiJimmy/hu...,这里选择的是【Stack】主题,可按需选择版本,如v3.33.0。

将这个压缩包解压到CodeEdge的themes文件夹下:

将【exampleSite】文件夹下的【content】文件夹和【hugo.yaml】文件复制到【CodeEdge】文件夹下:

接下来,将【CodeEdge】文件夹下的【hugo.toml】文件以及【content/post】路径下的【rich-content】文件夹一并删除:

编辑【hugo.yaml】文件,将theme修改为themes文件夹下的主题文件夹同名:

在命令行中再次执行【hugo server -D】

访问【http://localhost:1313】就能加载添加的主题:

新建Github博客网站仓库

在Github上新建一个博客网站仓库,如【CodeEdge.github.io

为了连接的稳定性,github上最好选择ssh:

在github的博客网站仓库依次执行以下步骤:

编辑【hugo.yaml】文件,将baseurl修改为如下网站名称,与上面的步骤⑤保持一致:

之后即可通过访问这个baseurl来查看部署的博客了。

在命令行cd到public文件夹:

依次执行以下命令:

bash 复制代码
echo "# CodeEdge.github.io" >> README.md
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:github用户名/CodeEdge.github.io.git
git push -u origin main

新建Github博客源代码仓库

在Github上新建一个博客源代码仓库,如【CodeEdge.github.io

在命令行cd到CodeEdge文件夹:

依次执行以下命令:

bash 复制代码
echo "# CodeEdgeSource" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:github用户名/源代码仓库.git
git push -u origin main

实现Github Action自动部署

在github上依次进行以下操作,创建token,需要勾选【repo】和【workflow】:

将这个token保存到源代码仓库【CodeEdgeSource】中:

在【CodeEdge】文件夹下新建以下文件:

hugo_deploy文件的主要内容如下:

yaml 复制代码
name: Build and Deploy Hugo Site

on:
  push:
    branches:
      - main
    paths:
      - "content/**/*.md"
      - "static/**"
      - "layouts/**"
      - "assets/**"
  workflow_dispatch: # 支持手动触发

concurrency:
  group: deploy-pages
  cancel-in-progress: true

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout source repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.DEPLOYCODEEDGE_TOKEN }} # 用于图片上传需要推送权限

      - name: Git Configuration
        run: |
          git config --global core.quotePath false
          git config --global core.autocrlf false
          git config --global core.safecrlf true
          git config --global core.ignorecase false

      # ============ 构建 Hugo 站点 ============
      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v3
        with:
          hugo-version: "latest"
          extended: true

      - name: Build site
        run: hugo -D # 包含 draft 文章

      # ============ 部署到 GitHub Pages ============
      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          personal_token: ${{ secrets.DEPLOYCODEEDGE_TOKEN }}
          external_repository: github用户名/博客网站仓库
          publish_branch: main
          publish_dir: ./public
          force_orphan: true
          commit_message: "auto deploy: ${{ github.event.head_commit.message }}"
          user_name: "github-actions[bot]"
          user_email: "github-actions[bot]@users.noreply.github.com"

在【CodeEdge】文件夹下新建【.gitignore】文件,避免提交以下非必要的文件:

bash 复制代码
public
resources
.hugo_build.lock
hugo.exe

*.lock
.DS_Store
Thumbs.db
*.tmp
*.log
.idea/
.vscode/

博客部署步骤

markdown 复制代码
#新建博客
hugo new content post/XXX.md

#本地查看
hugo server -D

#部署
git add .

git commit -m "xxx"

git push origin main

当新增的博客推送到源代码仓库后,GitHub Actions 会自动执行以下步骤:

  1. 检测到推送
  2. 触发 .github/workflows/hugo_deploy.yaml 工作流
  3. 运行 hugo -D 构建网站
  4. 将生成的静态文件部署到博客网站仓库
  5. 等待一定的时间后,博客网站会自动更新

小贴士

1.Hugo 需要 Front Matter 才能识别文章。也就是在文件最开头需要有以下这些代码:

markdown 复制代码
+++
title = '配置ssh解决https不稳定的问题'
date = '2026-01-13T14:20:00+08:00'
draft = false
+++

2.github账号现在有一个输入验证码的步骤,可在浏览器上安装【2FA Authenticator】插件获取。

3.一些搭建博客可能用到的网站

免费图标网站 icon-icons.com/
emoji词典 www.emojiall.com/
在线PS工具 ps.pic.net/
ANI 鼠标样式转 PNG 文件 在线工具 ezgif.com/ani-to-apng
PNG转ICO在线工具 www.png2ico.com/
免费的商用字体 www.100font.com/
相关推荐
雨夜之寂2 小时前
大模型 ai coding 比较
后端·面试
RoyLin2 小时前
Rust 编写的 40MB 大小 MicroVM 运行时,完美替代 Docker 作为 AI Agent Sandbox
后端·架构·rust
风象南4 小时前
无文档遗留系统的逆向梳理:利用 AI 重建架构视图
后端
金銀銅鐵5 小时前
浅解 Junit 4 第六篇:AnnotatedBuilder 和 RunnerBuilder
后端·junit·单元测试
钟智强5 小时前
Erlang 从零写一个 HTTP REST API 服务
后端
王德印5 小时前
工作踩坑之导入数据库报错:Got a packet bigger than ‘max_allowed_packet‘ bytes
java·数据库·后端·mysql·云原生·运维开发
Cache技术分享5 小时前
327. Java Stream API - 实现 joining() 收集器:从简单到进阶
前端·后端
颜酱5 小时前
滑动窗口算法通关指南:从模板到实战,搞定LeetCode高频题
javascript·后端·算法
重生之后端学习5 小时前
994. 腐烂的橘子
java·开发语言·数据结构·后端·算法·深度优先