GitBlit hooks 多仓库单分支全自动文件部署

本文介绍了一个基于Git的文件自动更新方案。通过定义特定的脚本,实现不同分支的克隆和拉取操作,确保文件始终保持最新状态。适用于多种场景下的自动化部署需求。
//声明类
import com.gitblit.GitBlit
import com.gitblit.Keys
import com.gitblit.models.RepositoryModel
import com.gitblit.models.TeamModel
import com.gitblit.models.UserModel
import com.gitblit.utils.JGitUtils
import com.gitblit.utils.StringUtils
import java.text.SimpleDateFormat
import org.eclipse.jgit.api.CloneCommand
import org.eclipse.jgit.api.PullCommand
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.lib.Config
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.transport.ReceiveCommand
import org.eclipse.jgit.transport.ReceiveCommand.Result
import org.eclipse.jgit.util.FileUtils
import org.slf4j.Logger

def updatedRef
for (ReceiveCommand command : commands) {
    updatedRef = command.refName
}

// RootFolderUrl 'D:/git' 文件夹目录
// BranchName 'refs/heads/master' 目标分支名字
def AutoClone(RootFolderUrl,BranchName){
    clientLogger.info("准备开始文件更新")
    // 文件夹目录
    def rootFolder = RootFolderUrl
    def bare = false
    //是否全部分支
    def allBranches = false
    //目标分支
    def branch = BranchName
    def includeSubmodules = true
    
    //版本库名称
    def repoName = repository.name
    //文件夹对象
    def destinationFolder = new File(rootFolder, StringUtils.stripDotGit(repoName))
    //版本库路径
    def srcUrl = 'file://' + new File(gitblit.getRepositoriesFolder(), repoName).absolutePath
    
    //判断是否全部分支更新
    if(allBranches){
        logger.info("用户 ${user.username} 请求从 ${repository.name} 版本库下[所有分支]克隆文件")
        //不终止,继续执行
    }else{
        logger.info("用户 ${user.username} 请求从 ${repository.name} 版本库下 ${branch} 分支更新文件")
        //单线分支
    }
    
    clientLogger.info("开始检查目录")
    // 检查目标文件目录是否存在
    if (destinationFolder.exists()) {
        //已存在,使用 pull 拉取推送的不同文件
        logger.info("正在把 ${srcUrl} 版本库 ${branch} 分支文件更新至 ${destinationFolder}")
        clientLogger.info("文件目录已存在,准备更新文件")
        //git 获取文件夹路径
        Git git = Git.open(destinationFolder)
        //调用 pull 类下的 pull 方法
        PullCommand cmd = git.pull()
        //设置对象分支
        cmd.setRemoteBranchName(branch)
        //执行
        cmd.call()
        //关闭
        git.close()
        logger.info("文件更新成功")
        clientLogger.info("文件已更新完成")
    } else {
        //不存在,使用 clone 克隆对应分支 
        clientLogger.info("准备新建文件目录,正在确认版本库")
        //调用 clone 类 下的 方法
        CloneCommand cmd = Git.cloneRepository();
        cmd.setBare(bare)
        //判断是否判断全部分区
        if (allBranches){
            logger.info("正在把 ${srcUrl} 版本库所有分支克隆到 ${destinationFolder}")
            clientLogger.info("开始克隆全部分支")
            cmd.setCloneAllBranches(true)
        }else{
            logger.info("正在把 ${srcUrl} 版本库 ${branch} 分支克隆到 ${destinationFolder}")
            clientLogger.info("开始克隆 ${branch} 分支")
            cmd.setBranch(branch)       
        }
        cmd.setCloneSubmodules(includeSubmodules)
        //设置路径
        cmd.setURI(srcUrl)
        //设置文件夹路径
        cmd.setDirectory(destinationFolder)
        //执行
        Git git = cmd.call();
        git.repository.close()
        logger.info("克隆已完成")
        clientLogger.info("克隆已完成")
    }
     
    logger.info("文件已同步")
    
}


// 判断哪个仓库
switch (repository.name) {
        // 仓库名 1
        case 'His.git':
            
            // 自定义分支名称
            switch (updatedRef) {
                case 'refs/heads/master':
                    AutoClone('C:/inetpub/m','refs/heads/master')
                    break
                case 'refs/heads/bug':
                    AutoClone('C:/inetpub/d','refs/heads/bug')
                    break
                default:
                    clientLogger.info("该分支文件未自动部署")
                    break
            }
            
            break
        // 仓库名 2
        case 'test.git':
            
            // 自定义分支名称
            switch (updatedRef) {
                case 'refs/heads/master':
                    AutoClone('C:/inetpub/wwwroot','refs/heads/master')
                    break
                default:
                    clientLogger.info("该分支文件未自动部署")
                    break
            }
            
            break

        default:
            clientLogger.info("所在仓库未设置推送")
            break
}

转载于:https://my.oschina.net/u/3756690/blog/1612105

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值