You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Action自动创建仓库并复制模板目录技术实现问询

完整实现方案

1. 调整后的GitHub Action工作流文件 .github/workflows/create_repo.yaml

这个工作流仅在master分支的org.json文件变更时触发,执行仓库创建与模板同步操作,同时支持手动触发调试:

name: 自动创建仓库并同步模板
on:
  push:
    branches: [master]
    paths:
      - "org.json" # 仅监听该文件的变更
  workflow_dispatch: # 支持手动触发调试

jobs:
  create-and-sync-repos:
    runs-on: ubuntu-latest
    steps:
      - name: 拉取当前代码仓库
        uses: actions/checkout@v4

      - name: 设置Python环境
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: 安装依赖包
        run: |
          python -m pip install --upgrade pip
          pip install PyGitHub gitpython

      - name: 执行仓库创建与同步脚本
        run: python scripts/create_repo.py
        env:
          GITHUB_TOKEN: ${{ secrets.REPO_CREATION_TOKEN }} # 自定义PAT令牌
          ORG_NAME: "ORG1" # 替换为你的组织名称
          TEMPLATE_DIR: "template_dir"

2. 完整的Python脚本 scripts/create_repo.py

脚本负责读取org.json内容,检查仓库是否存在,不存在则创建,最后同步模板目录内容到目标仓库:

import os
import json
import shutil
from github import Github
from git import Repo

def main():
    # 从环境变量读取配置
    github_token = os.getenv("GITHUB_TOKEN")
    org_name = os.getenv("ORG_NAME")
    template_dir = os.getenv("TEMPLATE_DIR")

    # 初始化GitHub客户端
    g = Github(github_token)
    org = g.get_organization(org_name)

    # 读取org.json配置文件
    with open("org.json", "r") as f:
        org_data = json.load(f)

    # 遍历所有待处理仓库
    for project in org_data:
        for repo_list in project.values():
            for repo_item in repo_list:
                repo_name = repo_item["repo_name"]
                print(f"开始处理仓库: {repo_name}")

                # 检查仓库是否已存在
                try:
                    org.get_repo(repo_name)
                    print(f"仓库 {repo_name} 已存在,跳过创建步骤")
                except:
                    # 创建新仓库(私有,自动初始化)
                    print(f"创建仓库 {repo_name}")
                    org.create_repo(
                        name=repo_name,
                        private=True,
                        auto_init=True
                    )

                # 同步模板文件到目标仓库
                print(f"同步模板内容到 {repo_name}")
                sync_template(repo_name, github_token, org_name, template_dir)

def sync_template(repo_name, token, org_name, template_dir):
    # 构建带令牌的仓库克隆地址
    repo_url = f"https://{token}@github.com/{org_name}/{repo_name}.git"
    temp_dir = f"/tmp/{repo_name}"

    # 清理临时目录
    if os.path.exists(temp_dir):
        shutil.rmtree(temp_dir)

    # 克隆仓库到临时目录
    repo = Repo.clone_from(repo_url, temp_dir)

    # 复制模板文件到仓库目录
    for item in os.listdir(template_dir):
        src = os.path.join(template_dir, item)
        dest = os.path.join(temp_dir, item)
        if os.path.isdir(src):
            shutil.copytree(src, dest, dirs_exist_ok=True)
        else:
            shutil.copy(src, dest)

    # 提交并推送变更
    repo.index.add(["."])
    repo.index.commit("同步模板初始文件")
    repo.remote("origin").push()

if __name__ == "__main__":
    main()

3. 关键配置步骤

  • 创建个人访问令牌(PAT):
    在GitHub账号的Settings > Developer settings > Personal access tokens中生成一个拥有repo权限的令牌,然后添加到仓库的Settings > Secrets and variables > Actions中,命名为REPO_CREATION_TOKEN。
  • 替换组织名称:
    将工作流文件中的ORG_NAME: "ORG1"替换为你的实际组织名称。
  • 模板目录确认:
    确保template_dir目录下的文件结构符合需求,脚本会完整复制该目录下的所有内容到新仓库根目录。

内容的提问来源于stack exchange,提问作者Rio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 04:37:36