From 1bc223feea2d35400fb01611a18a1a0f05480f77 Mon Sep 17 00:00:00 2001 From: leafee98 Date: Fri, 29 Apr 2022 09:52:15 +0800 Subject: [PATCH] modify: use-git-hook... update the build-site code --- ...t-hook-to-build-hugo-site-automatically.md | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/content/posts/use-git-hook-to-build-hugo-site-automatically.md b/content/posts/use-git-hook-to-build-hugo-site-automatically.md index a685cef..bfab3ee 100644 --- a/content/posts/use-git-hook-to-build-hugo-site-automatically.md +++ b/content/posts/use-git-hook-to-build-hugo-site-automatically.md @@ -3,7 +3,7 @@ title: "使用 git hook 实现自动构建 Hugo 静态网站" date: 2022-04-28T20:39:01+08:00 tags: [] categories: [] -draft: true +draft: false --- Hugo 是一个优秀的静态网站生成器,并且结构对 Git 十分友好,所以一般会将 Hugo 搭配 Git 使用来提供较高的可操作性,很多人还会搭配 GitHub Pages 来实现免服务器建立个人博客。但是在不使用 GitHub Pages 的情况下,使用静态博客就会不可避免地要重复构建网站,每次都要手动构建再上传构建结果未免过于繁琐,这篇文章将介绍已有自建服务器的情况下,通过 Git Hook 实现在推送时自动重新构建网站内容的方式。 @@ -66,7 +66,7 @@ branch=main site_dir=/var/www/blog log_file=/var/log/build-site.log -exec >> $log_file +exec 1>> $log_file exec 2>&1 # both are real path @@ -76,6 +76,7 @@ git_dir=$(realpath .) echo ========= INFO ======== echo INFO: BUILD TIME: $(date -Iseconds) +echo INFO: SHELL: ${SHELL} echo INFO: new_repo=$new_repo echo INFO: git_dir=$git_dir echo INFO: banch=$branch @@ -83,10 +84,12 @@ echo INFO: site_dir=$site_dir echo ===== CLONE REPO ====== # use "file://" to let depth take effect +echo INFO:command=git clone --depth=1 --branch=$branch file://$git_dir $new_repo git clone --depth=1 --branch=$branch file://$git_dir $new_repo echo === INIT SUBMODULE ==== -git -C $new_repo submodule update --init +echo git --work-tree=$new_repo --git-dir=$new_repo/.git -C $new_repo submodule update --init +git --work-tree=$new_repo --git-dir=$new_repo/.git -C $new_repo submodule update --init echo ===== BUILD SITE ====== hugo --destination $site_dir --cleanDestinationDir --enableGitInfo -s $new_repo @@ -105,20 +108,31 @@ remote: fatal: /usr/libexec/git-core/git-submodule cannot be used without a work 此外,使用如上几个参数将裸仓库看作是普通仓库的 `$GIT_DIR` 会导致在裸仓库中创建一些额外的文件和目录,我觉得这是不好的,所以最后采用了克隆一个新的仓库的方法,将克隆深度设为 1 也是为了尽可能减少对性能的消耗,需要注意要使用 `file://` 格式的 URL 来指定原始仓库的路径,否则 `--depth` 参数将不会在本地文件系统中生效。 -实际的运行效果如下 +在初始化子模块的时候,`--work-tree` 和 `--git-dir` 参数是必须的,如果只保留 `-C` 参数则会出现下面的错误信息,但是奇怪的是如果在 bash 中手动运行该命令,又不会出现任何错误,只有在部署好,尝试推送仓库时,此错误才会在日志中显现,暂时认为是 git-shell 有我尚不知道的行为或者 Git 的 submodule 存在 BUG。 + +> 在实际尝试部署时,仅测试了单独 `-C` 和 `-C, --work-tree, --git-dir` 三个参数同时存在这两种情况,前者会出现上面描述的十分离奇且隐蔽的错误,后者正常工作。 + +``` +fatal: not a git repository: '.' +``` + +实际的运行效果如下(内容从日志文件中获取) ``` ========= INFO ======== -INFO: BUILD TIME: 2022-04-28T14:10:28+00:00 -INFO: new_repo=/tmp/tmp.4mn5x1c4v3 +INFO: BUILD TIME: 2022-04-29T01:41:15+00:00 +INFO: SHELL: /usr/bin/git-shell +INFO: new_repo=/tmp/tmp.IjbpWdGlA8 INFO: git_dir=/srv/git-repo/pub/leafee98-blog.git INFO: banch=main INFO: site_dir=/var/www/blog ===== CLONE REPO ====== -Cloning into '/tmp/tmp.4mn5x1c4v3'... +INFO:command=git clone --depth=1 --branch=main file:///srv/git-repo/pub/leafee98-blog.git /tmp/tmp.IjbpWdGlA8 +Cloning into '/tmp/tmp.IjbpWdGlA8'... === INIT SUBMODULE ==== +INFO:command=git --work-tree=/tmp/tmp.IjbpWdGlA8 --git-dir=/tmp/tmp.IjbpWdGlA8/.git -C /tmp/tmp.IjbpWdGlA8 submodule update --init Submodule 'themes/hugo-theme-flat' (https://cgit.leafee98.com/hugo-theme-flat.git/) registered for path 'themes/hugo-theme-flat' -Cloning into '/tmp/tmp.4mn5x1c4v3/themes/hugo-theme-flat'... +Cloning into '/tmp/tmp.IjbpWdGlA8/themes/hugo-theme-flat'... Submodule path 'themes/hugo-theme-flat': checked out '4e73d5d1eb6f2d0d0cd375b422e60fd22077a29d' ===== BUILD SITE ====== Start building sites _ @@ -135,9 +149,9 @@ hugo v0.97.3-078053a43d746a26aa3d48cf1ec7122ae78a9bb4 linux/amd64 BuildDate=2022 Sitemaps | 1 Cleaned | 0 -Total in 299 ms +Total in 312 ms ===== CLEAN TASK ====== -remove the cloned repo /tmp/tmp.4mn5x1c4v3 -INFO: FINISH TIME: 2022-04-28T14:10:29+00:00 +remove the cloned repo /tmp/tmp.IjbpWdGlA8 +INFO: FINISH TIME: 2022-04-29T01:41:17+00:00 ```