hugo 搭建一个多语言帮助网页 并托管到github和国内cdn

# 本地测试

# 获取hugo

win环境 https://github.com/gohugoio/hugo/releases 下载 hugo_extended_0.111.3_windows-amd64.zip 解压到 D:/hugo

1
2
3
hugo new site  D:\hugo\site1
cp hugo.exe D:\hugo\site1\hugo.exe
cd  D:\hugo\site1

# 选一个模板

我这里用 https://github.com/alex-shpak/hugo-book 这个,因为支持左右两侧导航 更适合文档类的。

1
2
3
4
5
git init
git submodule add https://github.com/alex-shpak/hugo-book themes/hugo-book
::  从  \themes\hugo-book\exampleSite 复制一个文件_index.md  到 \content\_index.md
hugo server --minify --theme hugo-book
:: 打开测试

# 通用型优化

准备把这个目录 所谓所有文档类型的模板。需要处理一点 新建一个 文件" .gitignore " 把 hugo.exe 添加进去

# 推送到私有git

测试没有问题后,文件cp到 linux开发机

1
2
3
4
5
6
7
8
rm -rf .git
rm -rf .gitmodules 
git init
git checkout -b main
git add .
git commit -m "first commit"
git remote add origin https://gitXXXXX
git push -u origin main

# 私有git配置和github的同步

在gitea 的仓库的设置里面,Git 远程仓库链接 添加github的 地址 用户名 和 有action权限的密码
这个github仓库为私有仓库,另外新建一个公开仓库作为page 公开仓库设置 sshkey 私有仓库设置 secrets 这个不在多说。

# 新建actione文件

.github\workflows\hugo-to-pub.yml 内容如下,其中 secrets.FOR_PAGE_SSHKEY 是 公开仓库的ssh 私key

 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
name: hugo-to-pub
on:
  push:
    branches:
      - main

jobs:
  hugo-to-pub:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0
    - name: 安装 hugo
      run: |
        mkdir hugo
        curl -sSL https://github.com/gohugoio/hugo/releases/download/v0.111.3/hugo_extended_0.111.3_linux-amd64.tar.gz | tar -xz --directory=./hugo
        echo `pwd`/hugo >> $GITHUB_PATH
    - name: 编译 hugo
      run: |
        echo "编译"
        hugo --theme=hugo-book --baseUrl="https://doc.XXXX/"
    - name: 处理独立域名的绑定
      run: |
        echo "doc.XXXX" >> ./public/CNAME
    - name: 处理ssh权限
      env:
        FOR_PAGE_SSHKEY: ${{ secrets.FOR_PAGE_SSHKEY }}
      run: |
        mkdir -p ~/.ssh/
        echo "$FOR_PAGE_SSHKEY" > ~/.ssh/id_rsa
        chmod 600 ~/.ssh/id_rsa
        ssh-keyscan github.com >> ~/.ssh/known_hosts
    - name: 初始化git并推送到github公开库
      run: |
        cd public
        git config --global init.defaultBranch main
        echo "init"
        git init
        git checkout -b main
        git add -A
        echo "config"
        git config user.name "joyanhui"
        git config user.email "joyanhui@qq.com"
        echo "commit"
        git commit -m "自动生成  $GITHUB_SHA "
        echo "推送到公开库"
        git remote add origin git@github.com:joyanhui/XXXXX.git
        git push -u origin main -f -q

# 最后配置dns和cdn

国内解析到国内cdn回源到github
国外解析到github.io 完毕

# 补充

在codeserver 里面开发 hugo的话,注意调试的时候的ip绑定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
echo "http://10.1.1.99:1313/"

./hugo server  --theme hugo-book --bind=10.1.1.99  


test(){

    ./hugo server --minify --theme hugo-book   --baseURL=http://yoursite.org/  \
              --port=131 \
              --appendPort=false \
              --bind=87.245.198.50
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计