Fix Update Time Wrong in Hexo

Maple

Problem

It is convenient to use GitHub Actions to deploy GitHub Pages. As given by Hexo officially,

Official script
  1. Create a repo named username.github.io, where username is your username on GitHub. If you have already uploaded to other repo, rename the repo instead.
  2. Push the files of your Hexo folder to the default branch of your repository. The default branch is usually main, older repository may use master branch.
  • To push main branch to GitHub:
    $ git push -u origin main
  • The public/ folder is not (and should not be) uploaded by default, make sure the .gitignore file contains public/ line. The folder structure should be roughly similar to this repo, without the .gitmodules file.
  1. Check what version of Node.js you are using on your local machine with node --version. Make a note of the major version (e.g., v16.y.z)
  2. Create .github/workflows/pages.yml in your repo with the following contents (substituting 16 to the major version of Node.js that you noted in previous step):
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
35
36
37
38
39
.github/workflows/pages.yml
name: Pages

on:
push:
branches:
- main # default branch

jobs:
pages:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js 16.x
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
  1. Once the deployment is finished, the generated pages can be found in the gh-pages branch of your repository.
  2. In your GitHub repo’s setting, navigate to Settings > Pages > Source. Change the branch to gh-pages and save.
  3. Check the webpage at username.github.io.

However, This yaml script will cause the update time of your posts be the time script run. This is due to git clone which save latest push time rather than modification time.

Solution

In order to restore the modification time, just add /usr/bin/git ls-files -z | while read -d '' path; do touch -d "$(git log -1 --format="@%ct" "$path")" "$path"; done to yaml script. And as we use actions/checkout, the parameter fetch-depth: 0 is need because 0 means get all history for all branches and tags.
So, the complete script for me is:

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
35
36
37
38
39
name: Pages

on:
push:
branches:
- main # default branch

jobs:
pages:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Use Node.js 18.x
uses: actions/setup-node@v2
with:
node-version: "18"
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Restore file modification time
run: |
/usr/bin/git ls-files -z | while read -d '' path; do touch -d "$(git log -1 --format="@%ct" "$path")" "$path"; done
- name: Build
run: npm run build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public

Reference

  1. 修复 CI 构建博客造成的更新时间错误 - Sea’s Blog (mrseawave.github.io)
  • Title: Fix Update Time Wrong in Hexo
  • Author: Maple
  • Created at : 2023-05-15 14:25:58
  • Updated at : 2023-10-27 11:27:31
  • Link: https://www.maple367.eu.org/Tools/Website/fix-update-time-wrong-in-hexo/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Fix Update Time Wrong in Hexo