Upstream File Sync #59
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Upstream File Sync | |
permissions: | |
contents: write | |
on: | |
schedule: | |
- cron: "0 0 * * *" # 每天执行 | |
workflow_dispatch: | |
jobs: | |
sync_latest_from_upstream: | |
name: Sync specific file from upstream repo | |
runs-on: ubuntu-latest | |
env: | |
UP_REPO: cmliu/edgetunnel # 上游仓库 | |
UP_BRANCH: main # 上游分支 | |
FILE_TO_SYNC: 明文源码.js # 需要同步的文件 | |
steps: | |
# 步骤 1: 设置 UTF-8 编码环境 和 git 编码 | |
- name: Set UTF-8 encoding | |
run: | | |
export LC_ALL=C.UTF-8 | |
export LANG=C.UTF-8 | |
git config --global core.quotepath false | |
git config --global i18n.logOutputEncoding utf-8 | |
# 步骤 2: 检出目标仓库 | |
- name: Checkout target repo | |
uses: actions/checkout@v3 | |
# 步骤 3: 添加上游仓库为远程仓库 | |
- name: Add upstream repo $UP_REPO | |
run: | | |
git remote add upstream https://github.com/$UP_REPO.git | |
git fetch upstream | |
# 步骤 4: 检查和更新 $FILE_TO_SYNC | |
- name: Check and update $FILE_TO_SYNC | |
run: | | |
# 确保文件名没有多余空格 | |
FILE_TO_SYNC=$(echo "$FILE_TO_SYNC" | xargs) | |
echo "Sanitized file name: '$FILE_TO_SYNC'" | |
if git ls-tree -r upstream/$UP_BRANCH --name-only | grep -q "^$FILE_TO_SYNC$"; then | |
echo "文件 $FILE_TO_SYNC 存在于 $UP_REPO,开始比较内容差异..." | |
mkdir -p temp | |
git show "upstream/$UP_BRANCH:$FILE_TO_SYNC" > temp/upstream_file | |
# 比较文件内容 | |
if [ -f "$FILE_TO_SYNC" ] && cmp -s temp/upstream_file "$FILE_TO_SYNC"; then | |
echo "文件 $FILE_TO_SYNC 与上游文件相同,跳过更新" | |
else | |
echo "文件 $FILE_TO_SYNC 与上游文件不同,更新本地文件" | |
mv temp/upstream_file "$FILE_TO_SYNC" | |
git add "$FILE_TO_SYNC" | |
fi | |
# 清理临时目录 | |
rm -rf temp | |
else | |
echo "错误:文件 $FILE_TO_SYNC 不存在于 $UP_REPO,跳过更新" | |
fi | |
# 步骤 5: 提交更改并推送 | |
- name: Commit and push changes | |
run: | | |
git config --global user.name "GitHub Actions Bot" | |
git config --global user.email "actions@github.com" | |
if git diff --cached --quiet; then | |
echo "没有检测到文件更改,跳过提交" | |
else | |
git commit -m "自动更新 $FILE_TO_SYNC" | |
git push | |
fi |