使用Overleaf写论文很方便,但其GitHub仓库同步功能需要付费,如果习惯于使用Git作为版本控制系统或是使用GitHub做备份,可以使用GitHub Actions在一定程度上平替Overleaf自带的同步功能。

配置Overleaf参数

参考链接中的readme配置OVERLEAF_COOKIEOVERLEAF_PROJECT_ID两个GitHub secrets。

配置GitHub Actions

在仓库的.github/workflows/目录中新建yml文件:

name: overleaf2git

on:
workflow_dispatch:

permissions:
contents: write
pull-requests: write

jobs:
main:
runs-on: ubuntu-latest
steps:
- name: Fetch the latest version from overleaf server
uses: subhamx/overleaf_sync_with_git@v1.0.0
with:
OVERLEAF_PROJECT_ID: ${{ secrets.OVERLEAF_PROJECT_ID }}
OVERLEAF_COOKIE: ${{ secrets.OVERLEAF_COOKIE }}

- name: Get GMT+8 time
id: gmt8-time
run: |
GMT8_TIME=$(date -u -d '+8 hours' +'%Y-%m-%d %H:%M:%S')
echo "::set-output name=time::$GMT8_TIME"

- uses: actions/checkout@v2
with:
path: repo/

- name: Update workspace
run: |
cd repo/
cp -r ../artifacts/* ./
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .
if [[ $(git diff HEAD --stat) == '' ]]; then (echo 'Working tree is clean')
else (git commit -m "sync from overleaf") fi

- name: Create Pull Request
uses: peter-evans/create-pull-request@v7
with:
path: repo/
title: 'sync from overleaf at ${{ steps.gmt8-time.outputs.time }}'

进行同步

该Actions只能从Overleaf单向同步至GitHub,因此首次使用或在Git中对论文更新后需要将仓库内的文件先上传至Overleaf。每当需要执行同步时,只需在仓库中的Actions页面内找到与yml同名的workflow,点击Run workflow即可。如果Overleaf内的文件相较于Git仓库中有变化,该Actions会生成一个包含Overleaf中最新改动的PR。该Actions也可以按需设置为定时运行,详见GitHub Actions相关文档。