GitHub Checkout Action Preserve File Modification Time

By # Deploy Hexo From Private Repository to GitHub Pages, we can leverage GitHub Actions to automatically deploy the Hexo website. However, for each deployment commit, the post's edit time will be changed to the current time instead of actual modification time. It may mislead the search engine to regard the website as a frequently modified site.

By default, Hexo uses the post file modification time as its edit time. By design, git doesn't preserve the file modification time (refer to this). After checkout action, the file modification time will be the current time.

Therefore, we need to restore the modification time by third party tools: git-restore-mtime.

git-restore-mtime is a GitHub Workflow Action which restores timestamps of files in the current tree based on their last commit times.

Although the sample usage yaml looks complicated, using git-restore-mtime action is easy. Just add a new step after checkout action:

- name: Restore Timestamps
  uses: chetan/git-restore-mtime-action@v1

Unfortunately, it doesn't work. Because git-restore-mtime leverages the git reflog to find the correct timestamp for each file. The default checkout action only checkout the last commit instead of full history. So we need to add another line fetch-depth: 0 in the checkout step to pull the full history:

...
- name: Checkout
  uses: actions/checkout@v3.0.0
  with:
	token: ${{ secrets.MYPAT }}
	submodules: 'true'
	persist-credentials: false
	fetch-depth: 0

- name: Restore Timestamps
  uses: chetan/git-restore-mtime-action@v1
...

Now it works! Every Hexo post's edit time is correct.