利用python迁移gitlab中项目
思路
- 利用python 调用gitlab api 获取项目信息,创建信息,并同步项目
- 由于python 调用同步项目代码较多,可以通过python 生成同步的脚本
手工同步gitlab 的方式
## 这种方式适合单向同步,git迁移;由于该种方式会完全替换目标的工程,不适合双向同步的
## 从源gitlab克隆镜像到本地
git clone --mirror git@gitlab1.com:group-name/project-name.git
## 进入克隆的镜像目录
cd project-name.git
## 将克隆的镜像推送至目标
git push --mirror git@gitlab2.com:group-name/project-name.git
利用python 同步项目
由于精力有限,工作较多,实现成了利用python 同步创建空工程,并生成上述手工脚本。
前置技能要求
- 了解Git工作方式,能理解上述手工同步的原理
- 了解gitlab,并了解gitlab api的原理机制
- 同步项目利用了python-gitlab这个库(v2.5.0,支持gitlab v4 版本api, gitlab 10.0 以上版本支持(具体翻gitlab官方文档))
- python 版本3.7.X 需要使用pip安装python-gitlab
代码
- main.py
import gitlab
import time
import os
def main():
# 主要业务逻辑
#print('begin')
# 配置
# 目标仓库
tempDir = '/d/temp'
whGitlab = gitlab.Gitlab.from_config('wh', 'python-gitlab.cfg')
# 源仓库
kfbGitlab = gitlab