当我们在本地建立了我们的blog网站后,下一步就可以将blog内容分发到网络上,这样别人就可以看到了。

我调研了一下目前的托管网站,有以下选择:

这些平台各有特点,在后面的文章中我会一一介绍如何部署。在本篇文章中,我将介绍如何将blog网站部署到github page上。

什么是 Github Pages

GitHub Pages 是 GitHub 提供的免费静态网站托管服务,适合托管博客、项目主页、文档等。你只需要一个 GitHub 仓库,就可以将网站发布到 https://yourname.github.io 或自定义域名上。

如何部署blog网站到Github Pages

总体思路

项目结构

假设你有两个 GitHub 仓库:

项目结构如下:

PLAINTEXT
my-hugo-site/
├── content/            # 博客内容
├── layouts/            # Hugo 自定义布局
├── themes/             # Hugo 主题
├── config.toml        # Hugo 配置文件
└── .github/workflows/deploy.yml  # 自动部署配置

yourusername.github.io/
├── (发布的静态网站文件)   # 静态文件由 Hugo 构建后生成
点击展开查看更多

步骤一:创建两个仓库

  1. 在 GitHub 上创建两个仓库:
  1. 在 yourusername.github.io 仓库中,设置 GitHub Pages 的源分支为 main(或者 master)。

步骤二:设置 GitHub Actions 自动化部署

在 my-hugo-site 仓库中,创建 .github/workflows/deploy.yml 文件,设置自动构建并将静态文件推送到 yourusername.github.io 仓库。

YAML
name: GitHub Pages

on:
  push:
    branches:
      - main  # 博客根目录的默认分支,这里是main,有时也是master
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-24.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.147.0'  # 填写你的hugo版本,可用hugo version查看
          extended: true          # 如果你使用的不是extended版本的hugo,将true改为false

      - name: Build
        run: git submodule update --init --recursive && hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        if: ${{ github.ref == 'refs/heads/main' }}  # 注意填写main或者master
        with:
          personal_token: ${{ secrets.MY_PAT}} # 如果secret取了其他名称,将MY_PAT替换掉
          external_repository: lxb1226/lxb1226.github.io # 填写远程仓库,不一定是这个格式,按照自己的情况写 
          publish_dir: ./public
          #cname: www.example.com        # 填写你的自定义域名。如果没有用自定义域名,注释掉这行
点击展开查看更多

可以参考我的depoly.yaml

GitHub secrets 获取

在上面的yaml文件中,需要获取到 personal_token。你可以在my-hugo-site 仓库中获取。

你可以在仓库->Settings->Secrets and variables->Actions中获取。

personal_token

步骤三:配置Github Pages

  1. 在 yourusername.github.io 仓库中,进入 Settings → Pages。
  2. 设置 Source 为 main 分支,确保正确配置了 GitHub Pages。
  3. 保存后,静态网站会托管在 https://yourusername.github.io/

blog自动发布流程

  1. 在 my-hugo-site 仓库中编写文章、修改站点。
  2. 每次推送更新到 main 分支时,GitHub Actions 自动构建并将静态文件推送到 yourusername.github.io 仓库。
  3. GitHub Pages 自动更新并展示在 https://yourusername.github.io/。(这个一般要等一段时间)

参考

  1. https://docs.github.com/en/actions
  2. https://gohugo.io/documentation/
  3. https://lxb1226.github.io/
  4. https://github.com/lxb1226/heyjude-blog

版权声明

作者: heyjude

链接: http://heyjude.blog/zh-cn/posts/how-to-depoly-blog-to-github/

许可证: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

评论

开始搜索

输入关键词搜索文章内容

↑↓
ESC
⌘K 快捷键