修复Vercel部署hexo导致文章的更新时间错误

一个爱折腾的人 Lv1

修复 Vercel/Github Actions 部署 hexo 导致文章的更新时间错误

在使用 VercelGithub Actions 自动化部署 hexo 博客时会发现所有文章的更新时间都变成了此次提交修改的时间,但实际上这些文章是没有任何修改的。

这是因为 git 在推送更新的时候不会保存文件的访问时间、修改时间等元信息。

Hexo 中的文章默认是通过使用 文件的最后修改时间 作为文章的更新时间,只有在 font-matter 中指定了 updated 才会使用 updated 的值作为文章的更新时间,所以才会出现 CI 构建后所有文章的更新时间都变成了此次构建的时间,因为推送上去的文件的元信息不会保存。

使用 Github Action 部署到 Vercel

本站使用的就是这种方法,由于本站使用的是 Vercel 作为托管平台,每次推送到仓库后,Vercel 会自动构建并部署。

我们需要在 Vercel 构建之前使文件的最后修改时间与文章的更新时间一致,这样 Vercel 构建后的文件的最后修改时间才会与文章的更新时间一致。

因为 Vercel 绑定仓库后,每次推送会自动进行部署,不能修改 Vercel 的构建流程,所以我们需要在 Github 仓库中添加 Github Action。

原理

hexo-theme-next issue # 893 中提到了这个问题,@sli1989 提出了一个解决方案:

hi, the updated time with CI deployment can be fixed using this. please check Continuous Integration configurations carefully.

1
2
3
YAML
# Restore last modified time
- "git ls-files -z | while read -d '' path; do touch -d \"$(git log -1 --format=\"@%ct\" \"$path\")\" \"$path\"; done"

该条命令会将文件的最后修改时间修改为 Git 仓库中文件的最后提交时间,即文章的更新时间。

同样使用 find 命令也可以实现这个功能:

1
2
BASH
find source/_posts -name '*.md' | while read file; do touch -d "$(git log -1 --format="@%ct" "$file")" "$file"; done

准备工作

我们可以在博客根目录中创建一个 vercel.json 文件,添加以下内容:

1
2
3
4
5
6
7
8
JSON
{
"git": {
"deploymentEnabled": {
"main": false
}
}
}

这样 Vercel 就不会在 Github 仓库更新时自动部署了。

在 Github Action 中需要使用以下两个环境变量、一个秘钥:

  • VERCEL_ORG_ID
  • VERCEL_PROJECT_ID
  • VERCEL_TOKEN

获取 Vercel Access Token

我们需要在 Vercel 中获取一段 Vercel Access Token 用于 Github Action 部署到 Vercel。快速前往 Access Token 页面

img

获取 VERCEL_ORG_ID 和 VERCEL_PROJECT_ID

在本地安装 Vercel CLI 后,使用 vercel login 登录 Vercel

1
2
3
4
5
6
BASH
# npm
npm i -g vercel

# yarn
yarn global add vercel

在博客根目录下执行 vercel link,创建一个 Vercel 项目,此操作会在博客根目录下生成一个 .vercel 文件夹,.vercel/project.json 里面包含了 VERCEL_ORG_IDVERCEL_PROJECT_ID

在 Github 仓库中添加 secrets

在 Github 仓库中的 Settings -> Secrets and Variables -> Actions 中添加以下秘钥和环境变量:

  • VERCEL_TOKEN
  • VERCEL_ORG_ID
  • VERCEL_PROJECT_ID

img

创建 Github Action

在 Github 仓库中创建一个名为 .github/workflows/deploy.yml 的文件,添加以下内容:

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
name: Deploy Blog to Vercel Production Deployment
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
# 0 indicates all history for all branches and tags.
fetch-depth: 0
- 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
# run: "git ls-files -z | while read -d '' path; do touch -d \"$(git log -1 --format=\"@%ct\" \"$path\")\" \"$path\"; done"
- name: Install Vercel-cli🔧
run: npm install --global vercel@latest
- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
- name: Build Project Artifacts
run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}
- name: Deploy Project Artifacts to Vercel
run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }}

如果 github actions 中使用 actions/checkout@v2,请设定它的参数 fetch-depth: 0,因为 0 表示获取所有分支和标签的所有历史记录。默认值为 1

推送代码到 Github

在本地执行 git push 推送代码到 Github,Github Action 会自动部署到 Vercel。

参考链接

修改转自原文

  • 标题: 修复Vercel部署hexo导致文章的更新时间错误
  • 作者: 一个爱折腾的人
  • 创建于 : 2023-08-18 22:45:19
  • 更新于 : 2023-08-21 06:11:09
  • 链接: https://www.izeka.eu.org/2023/08/18/修复Vercel部署hexo导致文章的更新时间错误/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。