Gitlab批量创建同步项目和分支

效果

          创建一个组,并把一个模板组下面的项目全部同步到新创建的这个组中

           

 批量创建项目仓库

复制代码
#!/bin/bash

# GitLab API URL
GITLAB_URL="https://192.168.30.111:8090/api/v4"
 
# API Token
API_TOKEN="345345345345345345345"

#这是手动创建的group id
GROUP_ID=180

PROJECTS=("sa_backend_dev" "h5-new" "soc_asmp-service" "datacollect" "soar" "soar_plugin" "dataprocess" "backend-dataprocess-job" "network-security-leak" "SqlScrip" "soc_ai")

for project in "${PROJECTS[@]}"; do
    if [[ -n $GROUP_ID ]]; then
        GROUP_PARAM="group_id=$GROUP_ID"
    else
        GROUP_PARAM="path=$GROUP_PATH"
    fi
    
    curl -k --request POST "$GITLAB_URL/projects" \
         --header "PRIVATE-TOKEN: $API_TOKEN" \
         --header "Content-Type: application/json" \
         --data "{ \"name\": \"$project\", \"namespace_id\": $GROUP_ID }"
done
create_template_project.sh
复制代码

 

批量同步分支代码

     先创建完项目仓库后才能同步push分支代码

复制代码
import json
import shlex
import subprocess
import os
import requests
import time



def getProjects(group_id=None):
    url = f"https://192.168.30.111:8090/api/v4/groups/163/projects?private_token=士大夫撒旦飞洒地方&per_page=100000"
    allProjectsDict = requests.get(url=url,verify=False).json()
    for thisProject in allProjectsDict: 
        try:
            thisProjectURL = thisProject['http_url_to_repo']
            print(thisProjectURL)
            #target_path = full_path + '//' + thisProject['path']
            print(('git clone %s' % (thisProjectURL)))
            command = shlex.split('git clone %s' % (thisProjectURL))
            resultCode = subprocess.Popen(command)
            resultCode.wait()  # 等待子进程执行完毕
        except Exception as e:
            print("Error on {}: {}".format(thisProjectURL, str(e)))


def pushProjects(group_id=None):
    url = f"https://192.168.30.111:8090/api/v4/groups/163/projects?private_token=dfasdfsadfsadf&per_page=100000"
    allProjectsDict = requests.get(url=url,verify=False).json()
    for thisProject in allProjectsDict: 
        try:
            thisProjectURL = thisProject['http_url_to_repo']
            thisProjectURL = thisProjectURL.replace("base-repo", "smart-computer-room")
            #print(thisProjectURL)
            #target_path = full_path + '//' + thisProject['path']
            #首先要切到相应的项目目录
            print(('git push %s' % (thisProjectURL)))
            last_index = thisProjectURL.rfind('/')
            if last_index != -1:
               result = thisProjectURL[last_index + 1:]
            else:
               result = "No '/' found"
            # print(result)
            dirname =  result.replace(".git","")
            # print(dirname)
            os.chdir("D:\\笔记\\2025\\gitlab\\%s" %(dirname))
            
            branchs= ["dev-v1.1","base-dev-v1.1","base-dev"]
            for branch in branchs:
                command = shlex.split('git checkout %s' %(branch))
                resultCode = subprocess.Popen(command)
                resultCode.wait()  # 等待子进程执行完毕

                command = shlex.split('git push %s' % (thisProjectURL))
                resultCode = subprocess.Popen(command)
                resultCode.wait()  # 等待子进程执行完毕
        except Exception as e:
            print("Error on {}: {}".format(thisProjectURL, str(e)))


if __name__ == "__main__":
    #getProjects()
    # time.sleep(30)
    pushProjects()
clone_push_code.py
复制代码

      

   先克隆代码
      git clone https://192.168.30.111:8090/base-repo/h5-new.git

   提交多个分支
    先切换分支然后再push
     git checkout master
     git push https://192.168.30.111:8090/xiangan/h5-new.git

     git checkout test
     git push https://192.168.30.111:8090/xiangan/h5-new.git

     git checkout dev
     git push https://192.168.30.111:8090/xiangan/h5-new.git

     

      每次push只能推送一个分支,如果要同步多个分支必须采用循环推送的方式

posted @   不懂123  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示