From 41decf4299f52ff2143eb435491cb286d90e340b Mon Sep 17 00:00:00 2001 From: zzzhizhi Date: Mon, 6 Jan 2025 14:55:24 +0800 Subject: [PATCH] feat: workflow & blog --- .github/workflows/deploy-public.yml | 45 +++++++++++++++++++ .gitignore | 2 + .gitmodules | 3 -- content/en/posts/hugo-blog-setup-tips.md | 24 +++++++++- content/{zh-cn => zh}/archive.md | 2 +- .../posts/hugo-blog-setup-tips.md | 28 ++++++++++-- .../search.zh-cn.md => zh/search.zh.md} | 0 hugo.yaml | 14 +++--- public | 1 - themes/PaperMod | 2 +- 10 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/deploy-public.yml delete mode 100644 .gitmodules rename content/{zh-cn => zh}/archive.md (72%) rename content/{zh-cn => zh}/posts/hugo-blog-setup-tips.md (88%) rename content/{zh-cn/search.zh-cn.md => zh/search.zh.md} (100%) delete mode 160000 public diff --git a/.github/workflows/deploy-public.yml b/.github/workflows/deploy-public.yml new file mode 100644 index 0000000..3e2d95f --- /dev/null +++ b/.github/workflows/deploy-public.yml @@ -0,0 +1,45 @@ +name: Deploy public + +on: + push: + branches: ["main"] + workflow_dispatch: + inputs: + hugoVersion: + description: 'Hugo version to use' + required: false + default: '0.128.0' + type: string + +permissions: + contents: write + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + env: + HUGO_VERSION: ${{ inputs.hugoVersion || '0.128.0' }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Hugo + run: | + wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${{ env.HUGO_VERSION }}/hugo_extended_${{ env.HUGO_VERSION }}_linux-amd64.deb + sudo dpkg -i ${{ runner.temp }}/hugo.deb + + - name: Build + run: hugo --minify --gc + + - name: Deploy to public branch + uses: s0/git-publish-subdir-action@develop + env: + REPO: self + BRANCH: public + FOLDER: public + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + MESSAGE: "Build: ({sha}) {msg}" + SQUASH_HISTORY: true diff --git a/.gitignore b/.gitignore index e073e8a..432d554 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Folders _obj _test +public # Architecture specific extensions/prefixes *.[568vq] @@ -21,6 +22,7 @@ _testmain.go *.exe *.test +*.cmd .vscode .DS_Store diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 89af1b0..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "themes/PaperMod"] - path = themes/PaperMod - url = https://github.com/adityatelange/hugo-PaperMod.git diff --git a/content/en/posts/hugo-blog-setup-tips.md b/content/en/posts/hugo-blog-setup-tips.md index 39bd29f..2f33aa2 100644 --- a/content/en/posts/hugo-blog-setup-tips.md +++ b/content/en/posts/hugo-blog-setup-tips.md @@ -37,7 +37,7 @@ defaultContentLanguageInSubdir: true - Affects the URL structure generated by Hugo - Determines the language identifier for default content -It is recommended not to modify this configuration. +The above configuration should not be modified unless you specifically need to change the default language. `defaultContentLanguageInSubdir: true` @@ -239,6 +239,28 @@ git push cd .. ``` +Due to maintenance difficulties, I switched to using separate source and deployment branches within the same repository. Therefore: + +```shell +# Create public branch in main repository +git checkout --orphan public +git reset --hard +git commit --allow-empty -m "Init public branch" +git push origin public +git checkout main + +# Link public directory to public branch +git worktree add -B public public origin/public + +# Generate site and push +hugo -D +cd public +git add -A +git commit -m "Push %date%" +git push origin public +cd .. +``` + With Cloudflare Pages CI/CD, a single script can push updates with one command. Do not configure `baseURL` in `hugo.yaml`; otherwise, all unique preview URLs will be redirected to `baseURL`. diff --git a/content/zh-cn/archive.md b/content/zh/archive.md similarity index 72% rename from content/zh-cn/archive.md rename to content/zh/archive.md index 54966a8..b3bb5d0 100644 --- a/content/zh-cn/archive.md +++ b/content/zh/archive.md @@ -1,6 +1,6 @@ --- title: "归档" layout: "archives" -url: "/zh-cn/archives" +url: "/zh/archives" summary: "archives" --- \ No newline at end of file diff --git a/content/zh-cn/posts/hugo-blog-setup-tips.md b/content/zh/posts/hugo-blog-setup-tips.md similarity index 88% rename from content/zh-cn/posts/hugo-blog-setup-tips.md rename to content/zh/posts/hugo-blog-setup-tips.md index d4cf511..6e08c2c 100644 --- a/content/zh-cn/posts/hugo-blog-setup-tips.md +++ b/content/zh/posts/hugo-blog-setup-tips.md @@ -24,7 +24,7 @@ params: ### 多语言 -这里配置很容易出问题。如果你像我一样喜欢搞多语言博客,请关注以下配置: +这里的配置很容易出问题。如果你像我一样喜欢搞多语言博客,请关注以下配置: ```yaml defaultContentLanguage: zh-cn @@ -37,7 +37,7 @@ defaultContentLanguageInSubdir: true - 影响 Hugo 生成的 URL 结构 - 决定默认内容的语言标识 -以上配置建议不要修改。 +以上配置不要修改,除非的确要修改默认语言。 `defaultContentLanguageInSubdir: true` @@ -221,7 +221,7 @@ params: 敲打 `hugo -D` 命令后,Hugo 会在 `/public` 目录下构建静态博客。每次构建前一般会将 `/public` 删掉来确保新构建的内容不出问题。 -在这种情况下,我希望只部署静态博客而不是整个 Hugo 框架。于是写了一个 Shell 脚本: +在这种情况下,我希望只部署静态博客到仓库中,而不是整个 Hugo 框架。于是写了一个 Shell 脚本: ```shell @echo off @@ -239,6 +239,28 @@ git push cd .. ``` +因为不方便维护,我又改用同一仓库中单独创建源码分支和部署分支的方法。于是乎: + +```shell +# 在主仓库创建 public 分支 +git checkout --orphan public +git reset --hard +git commit --allow-empty -m "Init public branch" +git push origin public +git checkout main + +# 将 public 目录关联到 public 分支 +git worktree add -B public public origin/public + +# 生成网站并推送 +hugo -D +cd public +git add -A +git commit -m "Push %date%" +git push origin public +cd .. +``` + 结合 Cloudflare Pages 自带 CI/CD,只需一个脚本即可实现一键推送更新。 不要在 `hugo.yaml` 中配置 `baseURL`,这会导致所有部署唯一预览 URL 被重定向到 `baseURL`。 diff --git a/content/zh-cn/search.zh-cn.md b/content/zh/search.zh.md similarity index 100% rename from content/zh-cn/search.zh-cn.md rename to content/zh/search.zh.md diff --git a/hugo.yaml b/hugo.yaml index 2895291..040404f 100644 --- a/hugo.yaml +++ b/hugo.yaml @@ -1,4 +1,4 @@ -languageCode: zh-cn +languageCode: zh theme: PaperMod pagination: @@ -76,13 +76,13 @@ params: limit: 10 keys: ["title", "permalink", "summary", "content"] -defaultContentLanguage: zh-cn +defaultContentLanguage: zh defaultContentLanguageInSubdir: true languages: - zh-cn: + zh: languageName: "简体中文" - contentDir: "/content/zh-cn" + contentDir: "/content/zh" weight: 1 title: "致之 | zzzhizhi" description: "致之的博客" @@ -116,10 +116,10 @@ languages: - name: email url: "mailto:mail@zzzhizhi.top" - name: rss - url: "/zh-cn/index.xml" + url: "/zh/index.xml" editPost: - URL: "https://github.com/zzzhizhiw/zzzhizhi-blog/content/zh-cn" + URL: "https://github.com/zzzhizhiw/zzzhizhi-blog/tree/main/content/zh" Text: "编辑" appendFilePath: true @@ -181,7 +181,7 @@ languages: url: "/en/index.xml" editPost: - URL: "https://github.com/zzzhizhiw/zzzhizhi-blog/content/en" + URL: "https://github.com/zzzhizhiw/zzzhizhi-blog/tree/main/content/en" Text: "Edit" appendFilePath: true diff --git a/public b/public deleted file mode 160000 index 90b3f2b..0000000 --- a/public +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 90b3f2b82366c95151880af901870b6a7bc946ed diff --git a/themes/PaperMod b/themes/PaperMod index 3f4fb58..9d16e78 160000 --- a/themes/PaperMod +++ b/themes/PaperMod @@ -1 +1 @@ -Subproject commit 3f4fb58a86b1e277df4953d6c79b967c28070136 +Subproject commit 9d16e786987542a41a674342d833b016633aea58