如何让jenkins支持gitlab的lfs大文件编译

这个问题,是很多大问题中的一个小环节,今天终于差不多搞定。

作个线索记录:

一,gitlab的版本高于8.3?以后,就自然支持LFS(Large File System)功能了,反正6版本的gitlab是没有这个功能的。

二,甚至,这个gitlab的LFS存储位置,还可以分享的放置到nexus 3的库中。

三,默认的git客户端,是不支持git 的lfs功能的,要特别安装一个git-lfs,有了这个,才可以执行git lfs clone之类的命令。

四,在作gitlab的LFS的功能时,需要一个特殊的文件来识别LFS的文件类型( .gitattributes)。

五,git lfs功能,可以基于命令行使用,也可以基于脚本上传(我甚至写了个Python脚本,用于上传指定文件到指定的git lab项目中)。

六,在jenkins集成gitlab作构建时,如果存在lfs文件下载,作job配置和pipeline时,都需要特殊的配置。

附:

1, jenkins配置gitlab lfs参考url:

https://issues.jenkins-ci.org/browse/JENKINS-43158

https://issues.jenkins-ci.org/browse/JENKINS-35687

checkout([$class: 'GitSCM', branches: [[name: 'git-branch']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'GitLFSPull']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'GitLab', url: 'http://www.demo.com/test.git']]])

2,使用python脚本及三方库上传lfs文件:

import os.path
import sys
import time
import string
import base64
import random
# 此库需要pip安装,python_gitlab-1.13.0-py3-none-any.whl
import gitlab

# 定义gitlab的url,接入token及项目group及project,作唯一定位
gitlab_url = "http://www.git.demo"
private_token = "xxxxxxxxxxx"
path_with_namespace = "user/project"

# 需要上传的压缩文件的本地路径
file_path = 'D:\\software\\software.zip'

class PrismGitlab:
    def __init__(self, gitlab_url, private_token):
        self.gl = gitlab.Gitlab(gitlab_url, private_token=private_token)
        self.gl.auth()

    def get_project(self, path_with_namespace):
        # list返回List,只有一个返回,故而[0]
        self.project = self.gl.projects.list(path_with_namespace=path_with_namespace)[0]

    def create_branch(self):
        # 生成随机branch
        random_letter = ''.join(random.sample(string.ascii_letters, 2))
        new_branch = time.strftime("%Y%m%d%H%M%S", time.localtime()) + random_letter.upper()
        self.new_branch = new_branch
        self.branch = self.project.branches.create({'branch': new_branch, 'ref': 'master'})

    def delete_branch_with_ref(self, branch):
        self.project.branches.delete(branch)

    def delete_branch(self):
        self.branch.delete()

    def create_b64_file(self, file_path):
        # 使用 basename获取文件名,可以少一个参数
        git_file_path = os.path.basename(file_path)
        print(file_path, git_file_path)
        # base64编码,在上传大文件中,是必须的。
        b64_file = base64.b64encode(open(file_path, 'rb').read()).decode()
        data = {
            'branch': self.new_branch,
            'commit_message': self.new_branch,
            'actions': [
                {
                    # Binary files need to be base64 encoded
                    'action': 'create',
                    'file_path': git_file_path,
                    'content': b64_file,
                    'encoding': 'base64',
                },
            ]
        }
        return self.project.commits.create(data)
    def delete_file(self, branch, filename):
        f = self.project.files.get(file_path=filename, ref=branch)
        print(f)
        f.delete(branch=branch,
                 commit_message='Delete {} - {} From Python-GitLab API.'
                 .format(branch, filename))

if __name__ == '__main__':

    gl = PrismGitlab(gitlab_url, private_token)
    gl.get_project(path_with_namespace)
    # gl.delete_branch_with_ref("20191202094134IT")
    # gl.delete_file(branch="master", filename="git-2.23.0.tar.gz")

    gl.create_branch()

    try:
        commit = gl.create_b64_file(file_path)
        print(commit)
    except Exception as e:
        print("error, maybe this object already exists in project.", e)
        gl.delete_branch()
        sys.exit(1)

  

猜你喜欢

转载自www.cnblogs.com/aguncn/p/12177196.html