WRY

Where Are You?
You are on the brave land,
To experience, to remember...

0%

Fix the Modification Time of Blogs

Troubleshooting Process

Where is the modification time information of the blog obtained when hexo compiles the blog?

After consulting the hexo document, the modification time information is based on the file modification time.

Why the file modification time obtained by Github Action is different from the local one?

The modification time of files will not be synchronized to the deploy machine when Action checkouts the branch from remote repository. So the modification time is determined by the file system. All files are just created for deploy machine, and their atime(access time), mtime(modify time) and ctime(change time) are all at this time.

How to solve the problem mentioned above?

As we know, the modification record of files will be carried in the git repository, so the commit record of the git will also carry the modification time of files.

We can find the last modified commit time of the file in git by the following git command.

1
mtime=$(git log -1 --format="@%ct" "$file")

We can change the mtime of file by the following command.

1
touch -d "$mtime" "$file"

So, we can restore modification time of files by the following command.

1
git ls-files | while read file; do touch -d $(git log -1 --format="@%ct" "$file") "$file"; done

Testing on Action still cannot fix this problem, although there is no problem with local testing.

I spent a lot of time on this problem, which also exposed my lack of clear ideas to solve problem.

By chance, I realized whether the checkout operation did not fetch all the commits. By reading the checkout instructions, I noticed a parameter fetch-depth, the introduction about it is as follows.

# Number of commits to fetch. 0 indicates all history for all branches and tags.
# Default: 1

Modify this parameter to 0, I successfully solves my problem.

End to spread flowers

Keep your thinking clear and no afraid to learn new knowledge!

Stay hungry. Stay foolish.

Appendix

The complete action yaml file is as follows.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
name: Node.js CI

on:
push:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14]
steps:
# 拉取代码
- uses: actions/checkout@v2
with:
fetch-depth: '0' # 取回所有的历史commits

# 配置git
- name: config git
run: |
git config --global core.quotepath false
git config --global user.name xxx
git config --global user.email xxx

# 修复文件的修改时间
- name: restore timestamps
run: |
git log -1 --format="@%ct" build.sh
git ls-files | while read file; do touch -d $(git log -1 --format="@%ct" "$file") "$file"; done
stat source/_posts/目录.md

# 删除敏感数据
- name: delete sensitive data
run: |
git checkout --orphan deploy
rm -rf xxx
git add -A
git commit -am "deploy"
git branch -D master
git branch -m master
ls -lah

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
# 安装环境
- run: npm install -g hexo-cli && npm install --save
- run: wget https://github.com/jgm/pandoc/releases/download/2.10.1/pandoc-2.10.1-1-amd64.deb && sudo dpkg -i pandoc-2.10.1-1-amd64.deb
# 编译
- run: export HEXO_ALGOLIA_INDEXING_KEY=${{ secrets.HEXO_ALGOLIA_INDEXING_KEY }} hexo clean && touch db.json && hexo algolia && hexo generate
# 同步到azure服务器
- run: printf "%s" "${{ secrets.DEPLOY_KEY }}" > ${SSH_PRIVATE} && chmod 600 ${SSH_PRIVATE} && rsync -avzW --delete --exclude-from='exclude-file.txt' public/ ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/data/hexo --rsh "ssh -p ${{ secrets.DEPLOY_PORT }} -i ${SSH_PRIVATE} -o StrictHostKeyChecking=no"
env:
SSH_PRIVATE: ../id_rsa.pem