1. 安装 Hugo
以 debian 系列系统为例,先安装 hugo
2. 创建新项目
3. 安装 MemE 主题
1
2
3
|
cd blog
git init
git submodule add --depth 1 https://github.com/reuixiy/hugo-theme-meme.git themes/meme
|
使用 MemE 主题的配置文件
1
2
|
rm hugo.toml
cp themes/meme/config-examples/zh-cn/config.toml hugo.toml
|
4. 启动站点并测试
开发时执行如下命令。
1
|
hugo server --bind=0.0.0.0
|
部署时执行如下命令。
1
|
hugo build --environment production --theme meme --cleanDestinationDir --baseURL https://xxx.xxx/
|
5. 发布新博文
1
|
hugo new post/hugo安装和部署.md
|
6. 自动部署
创建 git 的 hook
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
|
#!/usr/bin/env bash
# blog.git/hooks/post-receive
# 设置环境变量
REPO_NAME="blog"
HUGO_BUILD_DIR="/home/git/hugo-build"
PUBLIC_WWW_DIR="/var/www/html/"
HUGO_PATH="/usr/local/bin/hugo"
# 创建临时工作目录
TMP_GIT_CLONE="/home/git/hugo-build-${REPO_NAME}"
rm -rf "${TMP_GIT_CLONE}"
mkdir -p "${TMP_GIT_CLONE}"
# 克隆最新代码到临时目录
git clone /var/lib/gitea/data/gitea-repositories/notnow/hugo-blog.git "${TMP_GIT_CLONE}"
git -c safe.directory=/tmp/hugo-build-blog checkout main
# 进入目录并构建Hugo站点
cd "${TMP_GIT_CLONE}" || exit
echo "开始构建Hugo站点..."
# 检查是否安装hugo
if ! command -v hugo &> /dev/null; then
echo "错误:Hugo未安装!"
exit 1
fi
# 执行hugo构建
${HUGO_PATH} build --environment production --cleanDestinationDir --theme meme --baseURL https://notnow.ink/
# 同步到网站目录
rm -rf /var/www/html/*
ls -l /var/www/html/
cp -r "${TMP_GIT_CLONE}/public/." "${PUBLIC_WWW_DIR}"
# 清理临时文件
# rm -rf "${TMP_GIT_CLONE}"
echo "Hugo博客部署完成!"
|
保存后,当再有代码推送上来的时候,就会自动触发部署脚本,实现博客的自动部署。