1. 概述

本文档主要记录当从芯片原厂(比如RK)获取到repo形式的SDK源码之后怎么一步步搞到自己内部服务器的gitlab服务器上。

2. 以mirror方式获取SDK源码

2.1 配置ssh-key

(按照官方指导进行配置即可)

2.2 获取repo

# 切换到用户根目录
$ cd /home/ubuntu
# 获取repo
$ git clone ssh://git@www.rockchip.com.cn/repo/rk/tools/repo

于是在/home/ubuntu下会有repo,后面使用直接指定绝对路径或直接将其加入系统环境变量都可以。

2.3 mirror方式获取官方的SDK源代码

# 切换到工作目录
$ cd $SDK_Mirror_BaseDir
# 新建目录用于存放SDK代码
$ mkdir SDK_RK3288_Linux4_4
$ cd SDK_RK3288_Linux4_4
# 初始化代码仓库
$ /home/ubuntu/repo/repo init --repo-url ssh://git@www.rockchip.com.cn/repo/rk/tools/repo -u ssh://git@www.rockchip.com.cn/linux/rockchip/platform/manifests -b linux -m rk3288_linux_release.xml --mirror
# 同步代码
$ .repo/repo/repo sync

2. Gitlab端操作

2.1 创建Gitlab端group

在gitlab服务端创建一个group,后面SDK中所有的代码工程都放置到Gitlab的这个新建的Group上。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IVqK7SFN-1678932639945)(:/7a7065aa011d4b0a8b28a6e44e8bd18b)]
填写对应的group信息:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MlbCRlWS-1678932639946)(:/f6817738d6d34e65bb7f01507a405288)]

2.2 获取新建group的id编号

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rF82QG7N-1678932639947)(:/5c1a41c04df44d1fae00824302b7db6a)]
可见新创建的group的ID号为2260。

2.3 创建并获取目标group的API Access Token

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iIC1Ha83-1678932639947)(:/e3cc9a2b9a9f4f92abbb323f5ac6c206)]
简单填入相关信息:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bsE5ngCB-1678932639947)(:/a54931623c42436087243eabc9bfc609)]
此时会生成一个令牌,先保存下来。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VXSNQXhr-1678932639947)(:/29ca20f20d3b41708c50cc7d96d076e6)]
可见所生成的令牌为:Pc6BZKP-zhPfRn7vUaxM

4. 将mirror下来的代码上传到gitlab里面

4.1 构建自动操作脚本

#!/usr/bin/python
#encoding:utf-8
import gitlab
import os
import re
import time
import sys

# RK3288_Linux4-4
BASE_GROUP_ID = 2260																														# group ID修改成自己的
LOCAL_MIRROR_DIR = '/home/ubuntu/Work/00_SDK_Mirror/SDK_RK3288_Linux4_4/'				  # mirror仓库路径修改成自己的
REMOTE_REPO_BASE_DIR = 'git@172.30.131.71:rockchip/sdk/rk3288-linux4-4/'					# 仓库地址修改成自己的
gl = gitlab.Gitlab('http://172.30.131.71/', private_token='Pc6BZKP-zhPfRn7vUaxM')	# token修改为自己的

project_paths = []

def parse_paths():
    with open(os.path.join(os.getcwd(), "path.txt"), "rb") as strReader:
        for line in strReader:
            if line is not None and len(line) != 0:
                project_paths.append(line.replace("\r\n" , ""))

    # print("project_paths=" + str(project_paths))
    # print("project_paths len=" + str(len(project_paths)))

def parse_repo_dir_paths(baseDir):
    cmdstr = "cd "+baseDir+";"+"find ./* -name \"*.git\" -type d"
    # print("cmdstr = " + cmdstr)
    result = os.popen(cmdstr).read()
    # print("result = " + result)
    path_list = result.split("./")
    # print( path_list)
    for path in path_list:
        if path != '':
            project_paths.append(path.replace(".git\n", ""))
    # print(project_paths, len(project_paths))

def parse_paths():
    with open(os.path.join(os.getcwd(), "path.txt"), "rb") as strReader:
        for line in strReader:
            if line is not None and len(line) != 0:
                project_paths.append(line.replace("\r\n" , ""))

    # print("project_paths=" + str(project_paths))
    # print("project_paths len=" + str(len(project_paths)))

def create_group_and_project():
    group_base = gl.groups.get(BASE_GROUP_ID)
    print("grout_base=" + str(group_base))
    for path in project_paths:
        print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        print("path=" + path)
        paths = path.split("/")
        print("paths=" + str(paths))
        last_path_index = len(paths) - 1
        print("last_path_index=", last_path_index)
        group = group_base
        for index in range(0, last_path_index):
            p = paths[index]
            print("p=" + p)
            # is the group exist
            print("parent group=" + group.name)
            try:
                all_groups = group.subgroups.list(all=True)
            except AttributeError:
                all_groups = []
                print("AttributeError: clear all subgroups")

            is_group_exist = False
            for g in all_groups:
                if g.name == p:
                    is_group_exist = True
                    group = g
                    print("group exist=" + g.name)
                    break
            if is_group_exist:
                continue
            # create subgroup
            data = {
                "name": p,
                "path": p,
                "parent_id": group.id
            }

            try:
                group = gl.groups.create(data)
                print("group create success name=" + p)
                time.sleep(1)
            except gitlab.exceptions.GitlabCreateError as e:
                if e.response_code == 400:
                    print("group:" + p + " has already been created")

                    query_groups = gl.groups.list(all=True)
                    print("query_groups:" + str(query_groups))
                    for query_group in query_groups:
                        if query_group.name == p and query_group.parent_id == group.id:
                            group = query_group
                            print("update exit group:" + group.name)
                            break

        project = paths[last_path_index]
        print("group project list group=" + group.name)
        real_group = gl.groups.get(group.id, lazy=True)
        all_projects = real_group.projects.list(all=True)
        print("group all projects=" + str(all_projects))
        is_project_exist = False
        for p in all_projects:
            if p.name == project:
                is_project_exist = True
                print("project exist=" + p.name)
                break
        if not is_project_exist:
            print("create project=" + project)
            gl.projects.create({'name': project, 'path': project, 'namespace_id': group.id})
            print("project create success name=" + project)
            time.sleep(1)


def test_create_project_with_dot_name():
    # need use path field, if don't use path, GitLab url will replace "." to "_"
    gl.projects.create({'name': "xxx.yy.xy", 'path': "xxx.yy.xy"})

def mirror_push_local_to_gitlab():
    for path in project_paths:
        print("ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp");
        gitrepo_dir = str(LOCAL_MIRROR_DIR)+ path + ".git"
        cmdstr = "cd "+ gitrepo_dir + ";" + "git push --mirror " + REMOTE_REPO_BASE_DIR + path + ".git"
        print("cmdstr="+cmdstr)
        os.popen(cmdstr)
        # return

if __name__ == '__main__':
    # 解析仓库路径信息
    parse_repo_dir_paths(LOCAL_MIRROR_DIR)
    # 在Gitlab端批量创建groups以及projects
    create_group_and_project()
    # 将本地路径下的mirror仓库批量以mirror方式push到gitlab的各个仓库
    mirror_push_local_to_gitlab()

4.2 执行脚本自动部署