ai-要約を取得 文章摘要

hexo + butterfly 个人博客 & webstack 主题个人导航搭建全过程系列

  1. hexo_blog の誕生 - 基础环境配置(一)
  2. 博客魔改(二)
  3. gitactions 实现自动化部署(三)⇦当前位置🪂
  4. 独立开发tooltip遇到的实际问题(四)
  5. 实现 github & aliyun 双端部署(五)
  6. hexo_blog 多语言改造(六)

附上经过我调整的工作流逻辑

# 当有改动推送到master分支时,启动Action
name: 自动部署

on:
push:
branches:
- main #2020年10月后github新建仓库默认分支改为main,注意更改

release:
types:
- published

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 检查分支
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: main #2020年10月后github新建仓库默认分支改为main,注意更改

- name: Restore file modification time 🕒
run: find source/_posts -name '*.md' | while read file; do touch -d "$(git log -1 --format="@%ct" "$file")" "$file"; done

- name: 安装 Node
uses: actions/setup-node@v1
with:
node-version: "18.x" #action使用的node版本,建议大版本和本地保持一致。可以在本地用node -v查询版本号。

- name: 安装 Hexo
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g

- name: 缓存 Hexo
uses: actions/cache@v3
id: cache
with:
path: node_modules
key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

- name: 安装依赖
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install --save

- name: 生成静态文件
run: |
hexo clean
hexo generate

- name: 部署 #此处main:main 指从本地的main分支提交到远程仓库的main分支,若远程仓库没有对应分支则新建一个 ' git checkout -b main ' 如有其他需要,可以根据自己的需求更改。
run: |
cd ./public
git init
git checkout -b main # 创建并切换到 main 分支 这一行非常重要, 因为静态托管的仓库默认分支是 main
git config --global user.name '${{ secrets.GITHUBUSERNAME }}'
git config --global user.email '${{ secrets.GITHUBEMAIL }}'
git add .
git commit -m "${{ github.event.head_commit.message }} $(date +"%Z %Y-%m-%d %A %H:%M:%S") Updated By Github Actions"
git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITHUBTOKEN }}@github.com/${{ secrets.GITHUBUSERNAME }}/${{ secrets.GITHUBUSERNAME }}.github.io.git" main:main

直到 2025 年了,发现 github actions 执行自动部署后,导致 updated 时间戳出现异常,尝试了ai大模型给的各种无效建议之后,找到了下面的实战教程,领教了。

其中,新增了

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 检查分支
uses: actions/checkout@v2
with:
fetch-depth: 0 # 新增了这一行
ref: main
# 以及,下面这两行
- name: Restore file modification time 🕒
run: find source/_posts -name '*.md' | while read file; do touch -d "$(git log -1 --format="@%ct" "$file")" "$file"; done

这段工作流逻辑的目的是 恢复文件的修改时间,具体来看它是通过 git 的提交历史来为指定的文件设置修改时间。以下是逐步的分析:

1. 目标目录和文件过滤

find source/_posts -name '*.md'
  • find 命令:在目录 source/_posts 中查找所有扩展名为 .md 的 Markdown 文件。
  • 这一步的作用是收集需要处理的文件列表。

2. 循环处理每个文件

| while read file; do ...; done
  • while read file:逐行读取 find 的输出,每次将一个文件路径存入 file 变量中。
  • 目的是对找到的每个 .md 文件进行后续操作。

3. 获取文件最后一次提交时间

git log -1 --format="@%ct" "$file"
  • git log 命令
    • -1:仅获取最新的一条提交记录。
    • --format="@%ct":使用自定义格式输出提交时间,%ct 是提交的 UNIX 时间戳,@ 只是为了方便直接使用。
    • "$file":指定要查询的文件。
  • 作用:提取该文件在 Git 仓库中最后一次被修改的时间戳。

4. 更新文件的修改时间

touch -d "$(git log -1 --format="@%ct" "$file")" "$file"
  • touch -d 命令
    • -d 参数用于指定一个时间值,将文件的修改时间设置为该值。
    • $(git log -1 ...):通过子命令获取最后一次提交的时间戳。
    • "$file":更新对应文件的修改时间。

综合逻辑

  1. 遍历 source/_posts 目录下所有的 .md 文件。
  2. 对每个文件,查询其在 Git 仓库中最后一次提交的时间。
  3. 使用 touch 命令将文件的修改时间更新为该提交时间。

用途

这种做法常用于 Hexo 博客或者静态博客生成工具中,因为:

  • 文件的修改时间可能影响文章的排序(比如基于时间的文章列表)。
  • 有时会因文件同步工具(如 Git 克隆或拷贝)丢失原始文件的修改时间,此时通过 Git 提交记录恢复可以确保正确的时间顺序。