一、前期工作
1
2
3
4
5
6
7
8
9
|
adduser hugo
passwd hugo
su hugo
cd ~
mkdir hugo.git
cd hugo.git
git --bare init
touch hooks/post-receive
mkdir /data/www/hugo
|
添加下面的代码到 hooks/post-receive :
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
|
#!/bin/bash
echo "Post-receive hook executed."
# 设置主仓库和 Hugo 站点路径
MAIN_REPO=$HOME/hugo.git
TMP_HUGO=$HOME/hugo_site
PUBLIC_WWW=/data/www/hugo
HUGO_DOUBAN=$TMP_HUGO/data/douban
MOD_DOUBAN=$HOME/hugo_mod/douban
MOD_THEME=$HOME/hugo_mod/hugo-PaperMod
# 设置 Hugo 主题路径(假设主题是作为子模块)
THEME_DIR=$TMP_HUGO/themes/
# 克隆或更新主仓库
git clone -b main $MAIN_REPO $TMP_HUGO || (cd $TMP_HUGO && git pull)
#更新Theme
echo "update theme"
cp -r $MOD_THEME $THEME_DIR/
#更新DOUBAN信息
echo "douban ..."
cp -r $MOD_DOUBAN/ $TMP_HUGO/data/
# 更新DOUBAN封面
cp -r $MOD_DOUBAN/images/* $TMP_HUGO/static/images/
# 重新构建 Hugo 站点
hugo -F --cleanDestinationDir -s $TMP_HUGO -d $PUBLIC_WWW
rm -rf $TMP_HUGO
exit
|
1
2
3
|
chmod a+x hooks/post-receive
chown hugo /data/www/hugo/
chgrp hugo /data/www/hugo/
|
定时更新DOUBAN数据和Theme
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
|
"update_repo.sh" 33L, 891C
#!/bin/bash
HUGO_HOOK=/home/hugo/hugo.git/hooks/post-receive
# 进入目录
MOD_HOME=/home/hugo/hugo_mod
cd $MOD_HOME || exit
# 更新每个仓库
for repo in $(ls -d */); do
echo "Updating $repo"
if [ "$repo" = "douban/" ]; then
cd "$repo"
# 获取当前本地分支的最新提交哈希
current_commit=$(git rev-parse HEAD)
# 拉取远程仓库的最新变化
git pull
# 获取拉取后本地分支的最新提交哈希
latest_commit=$(git rev-parse HEAD)
# 检查是否有新的提交
if [ "$current_commit" != "$latest_commit" ]; then
echo "There are new commits. Performing additional steps..."
# 在这里执行你想要的流程或命令
# 例如,重新构建应用、更新子模块等
# ./your_additional_process.sh
. $HUGO_HOOK
fi
else
(cd "$MOD_HOME" && cd "$repo" && git pull)
fi
done
|
执行定时任务
1
2
|
crontab -e
0 Hugo final_push.sh hugo_head.sh posts posts-new Hugo final_push.sh hugo_head.sh posts posts-new Hugo final_push.sh hugo_head.sh posts posts-new Hugo final_push.sh hugo_head.sh posts posts-new update_repo.sh
|
\