该存储库包含一个 GitHub Actions 工作流,用于自动从存储库中同步包。以下是设置和使用此工作流的说明。
- 脚本会读取
packages文件中的同步规则,并根据规则从远程仓库克隆指定的内容。 - 支持以下格式的同步规则:
https://github.com/user/repo.git;- 默认将整个仓库同步到主仓库的根目录。
https://github.com/user/repo.git,src/folder;- 同步仓库中的
src/folder子目录。
- 同步仓库中的
https://github.com/user/repo.git,path=custom_path;- 将整个仓库同步到主仓库的
custom_path目录。
- 将整个仓库同步到主仓库的
https://github.com/user/repo.git,themes/theme1,path=my_themes;- 将仓库中的
themes/theme1子目录同步到主仓库的my_themes目录。
- 将仓库中的
- 配置文件:
packages- 每行定义一条同步规则。
- 以
#开头的行会被视为注释并忽略。
- GitHub 默认保留所有工作流运行记录,这可能会导致存储空间浪费和界面混乱。
- 该工作流会:
- 获取
main分支上的所有工作流运行记录。 - 仅保留最新的 3 条记录。
- 删除较旧的运行记录。
- 获取
- 手动验证清理效果:
- 前往 Actions 页面,检查是否只有最新的 3 条记录。
- 创建一个具有以下作用域的 PAT:
repo:对私有存储库的完全控制。admin:org:对组织资源的完全控制。
- 将 PAT 存储在存储库的 Secrets 中,名称为
SYNC_TOKEN。
-
进入存储库的 Settings > Secrets and variables > Actions。
-
点击 New repository secret。
-
设置名称为
SYNC_TOKEN,并粘贴您的 PAT 值。 -
保存 Secret。
-
进入存储库的 Settings > Actions > General。
-
在 Workflow permissions 下,选择 Read and write permissions。
- 这允许工作流推送更改并删除工作流运行记录。
-
启用 Allow GitHub Actions to create and approve pull requests。
- 这确保工作流有足够的权限执行提交更改和删除运行记录等操作。
- 触发工作流:
- 可通过 workflow_dispatch 事件手动触发,或在推送至
main分支时自动触发。
- 可通过 workflow_dispatch 事件手动触发,或在推送至
- 同步包:
- 工作流会检出存储库,设置 Python 3.10,并运行
sync_packages.py脚本。 - 脚本所做的任何更改都会通过
SYNC_TOKEN提交并推回存储库。
- 工作流会检出存储库,设置 Python 3.10,并运行
- 清理工作流运行记录:
- 工作流会获取
main分支上的所有运行记录,并仅保留最新的 3 条。 - 较旧的运行记录会被删除以减少混乱并节省存储空间。
- 工作流会获取
- 如果遇到
403 Forbidden错误,请按照以下步骤排查:- 检查
SYNC_TOKEN是否具有以下范围:repo:对私有存储库的完全控制。admin:org:对组织资源的完全控制。
- 确保存储库的 Workflow permissions 设置为 Read and write permissions。
- 确保启用了 Allow GitHub Actions to create and approve pull requests。
- 如果问题仍然存在,请尝试重新生成
SYNC_TOKEN并更新存储库 Secrets。
- 检查
- 如果清理步骤跳过了某些运行 ID:
- 检查 API 响应,确保提取了正确的 ID。
- 添加调试日志以打印 API 响应:
echo "API Response: $RESPONSE"
我们欢迎任何形式的贡献!如果您发现任何问题或有改进建议,请按照以下步骤操作:
- 提交问题:
- 描述问题的具体现象。
- 提供相关日志或截图。
- 提交拉取请求:
- 确保代码通过所有测试。
- 在 PR 描述中说明更改的内容和原因。
- 加入我们的讨论组(如果有的话)以获取更多帮助。
本项目采用 MIT 许可证。有关详细信息,请参阅 LICENSE 文件。
This repository contains a GitHub Actions workflow that automates the process of syncing packages from repositories and cleaning up old workflow runs. Below are the instructions for setting up and using this workflow.
-
Sync Packages:
- Automatically run a Python script (
sync_packages.py) to sync packages from repositories. - Commit and push changes back to the main branch.
- Automatically run a Python script (
-
Clean Up Old Workflow Runs:
- Retain only the latest 3 workflow runs on the
mainbranch. - Delete older workflow runs to reduce clutter and save storage space.
- Retain only the latest 3 workflow runs on the
To ensure the workflow functions correctly, you need to configure the following permissions:
- Create a PAT with the following scopes:
repo: Full control of private repositories.admin:org: Full control of organization resources.
- Store the PAT in your repository's secrets as
SYNC_TOKEN.
-
Go to Settings > Secrets and variables > Actions in your repository.
-
Click New repository secret.
-
Set the name as
SYNC_TOKENand paste the value of your PAT. -
Save the secret.
-
Go to Settings > Actions > General in your repository.
-
Under Workflow permissions, select Read and write permissions.
- This allows the workflow to push changes and delete workflow runs.
-
Enable Allow GitHub Actions to create and approve pull requests.
- This ensures the workflow has sufficient permissions to perform actions like committing changes and deleting runs.
-
Triggering the Workflow:
- The workflow can be triggered manually via the workflow_dispatch event or automatically when pushing to the
mainbranch.
- The workflow can be triggered manually via the workflow_dispatch event or automatically when pushing to the
-
Syncing Packages:
- The workflow checks out the repository, sets up Python 3.10, and runs the
sync_packages.pyscript. - Any changes made by the script are committed and pushed back to the repository using the
SYNC_TOKEN.
- The workflow checks out the repository, sets up Python 3.10, and runs the
-
Cleaning Up Workflow Runs:
- The workflow fetches all runs on the
mainbranch and retains only the latest 3 runs. - Older runs are deleted to reduce clutter and save storage space.
- The workflow fetches all runs on the
- If you encounter
403 Forbiddenerrors during the cleanup step:- Verify that the
SYNC_TOKENhas the correct scopes (repoandadmin:org). - Ensure Read and write permissions is selected under Workflow permissions in your repository settings.
- Ensure Allow GitHub Actions to create and approve pull requests is enabled.
- Verify that the
- If the cleanup step skips Run IDs:
- Check the API response to ensure the correct IDs are being extracted.
- Add debug logs to print the API response:
echo "API Response: $RESPONSE"
If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.