Custom Repository Sync #18
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: Custom Repository Sync | |
on: | |
schedule: | |
- cron: '0 3 * * *' # 每天UTC时间3点执行 | |
workflow_dispatch: # 允许手动触发 | |
jobs: | |
sync: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write # 显式声明需要写入权限 | |
steps: | |
- name: Checkout your repository | |
uses: actions/checkout@v3 | |
- name: Configure git | |
run: | | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
- name: Sync multiple repositories with custom folder structure | |
run: | | |
# 定义同步源数组 - 格式: [目标路径]="源仓库URL 分支 源路径 是否为文件夹(true/false)" | |
declare -A sources=( | |
# 同步整个文件夹 - 保留原始文件夹名称 | |
["luci-app-openclash"]="https://github.com/vernesong/OpenClash.git dev luci-app-openclash true" | |
# 同步多个文件 - 放入指定文件夹 | |
["luci-theme-argon"]="https://github.com/jerrykuku/luci-theme-argon.git master . false" | |
["luci-app-argon-config"]="https://github.com/jerrykuku/luci-app-argon-config.git master . false" | |
) | |
# 遍历并同步每个来源 | |
for dest in "${!sources[@]}"; do | |
IFS=' ' read -r repo_url branch src_path is_folder <<< "${sources[$dest]}" | |
echo "Syncing from $repo_url ($branch:$src_path) to $dest (is folder: $is_folder)" | |
# 创建临时目录 | |
temp_dir=$(mktemp -d) | |
# 克隆源仓库的特定分支 | |
git clone --depth 1 --single-branch --branch "$branch" "$repo_url" "$temp_dir" | |
# 获取源仓库最新提交信息 | |
cd "$temp_dir" | |
latest_commit_hash=$(git log -1 --format="%H") | |
latest_commit_date=$(git log -1 --format="%ad" --date=iso) | |
latest_commit_msg=$(git log -1 --format="%s") | |
cd - > /dev/null | |
echo "Latest commit in $repo_url ($branch):" | |
echo " Hash: $latest_commit_hash" | |
echo " Date: $latest_commit_date" | |
echo " Message: $latest_commit_msg" | |
# 确保目标目录存在 | |
mkdir -p "$dest" | |
if [ "$is_folder" = "true" ]; then | |
# 如果是文件夹,保留原始结构 | |
if [ -d "$temp_dir/$src_path" ]; then | |
# 清空目标目录,然后复制 | |
rm -rf "$dest"/* 2>/dev/null || true | |
cp -rf "$temp_dir/$src_path"/* "$dest"/ 2>/dev/null || true | |
fi | |
else | |
# 如果是多个文件,放入指定文件夹 | |
if [ "$src_path" = "." ]; then | |
# 源是根目录,复制所有文件 | |
cp -rf "$temp_dir"/* "$dest"/ 2>/dev/null || true | |
else | |
# 源是特定路径,复制该路径下的所有内容 | |
cp -rf "$temp_dir/$src_path"/* "$dest"/ 2>/dev/null || true | |
fi | |
fi | |
# 清理临时目录 | |
rm -rf "$temp_dir" | |
# 如果有变更,添加到 git | |
if [[ $(git status "$dest" --porcelain) ]]; then | |
git add "$dest" | |
# 使用正确的多行字符串格式 | |
git commit -m "Update $dest from $repo_url ($branch:$src_path)" \ | |
-m "Source commit: $latest_commit_hash" \ | |
-m "Update time: $latest_commit_date" \ | |
-m "Commit message: $latest_commit_msg" \ | |
-m "Synced at: $(date -u "+%Y-%m-%d %H:%M:%S UTC")" || true | |
echo "Changes committed for $dest" | |
else | |
echo "No changes for $dest" | |
fi | |
done | |
- name: Push changes | |
run: | | |
# 使用正确的引用方式 | |
git remote set-url origin "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" | |
git push origin HEAD || echo "No changes to push" |