gitblit自动化部署

//声明类
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
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

//logger.info() 服务器日志信息
//clientLogger.info() 客户端日志信息

CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider("user", "password");
 
// 文件夹目录
def rootFolder = 'D:/htdocs'
def bare = false
//是否全部分支
def allBranches = false
//目标分支
def branch = 'refs/heads/master'
def includeSubmodules = true

//版本库名称
def repoName = repository.name
//文件夹对象
def destinationFolder = new File(rootFolder, StringUtils.stripDotGit(repoName))
//版本库路径
def srcUrl = 'file://' + new File(gitblit.getRepositoriesFolder(), repoName).absolutePath

def updatedRef
for (ReceiveCommand command : commands) {
    updatedRef = command.refName
    clientLogger.info("${updatedRef.equals(branch)}")
}

//判断是否全部分支更新
if(allBranches){
    logger.info("User ${user.username} requests to clone files from [all branches] under the ${repository.name} repository")
    //不终止,继续执行
}else{
    logger.info("User ${user.username} requests to update the file from ${updatedRef} branch under the ${repository.name} repository")
    //单线分支
    //判断 推送分支 是否与 设置分支相同
    if (updatedRef.equals(branch)){
        //不终止,继续执行
        clientLogger.info("Prepare to get the file from the target branch")
    }else{
        //终止后续操作
        logger.info("Push branch ${updatedRef} is not in the update scope, end the operation")
        clientLogger.info("Push branch ${updatedRef} is not in the update scope, end the operation")
        return false
    }
}

clientLogger.info("Checking the file directory")
// 检查目标文件目录是否存在
if (destinationFolder.exists()) {
    //已存在,使用 pull 拉取推送的不同文件
    logger.info("Updating ${srcUrl} repository file to  ${destinationFolder}")
    clientLogger.info("File directory already exists, ready to update file")
    //git 获取文件夹路径
    Git git = Git.open(destinationFolder)
    //调用 pull 类下的 pull 方法
    PullCommand cmd = git.pull().setCredentialsProvider(credentialsProvider);
    //设置对象分支
    cmd.setRemoteBranchName(branch)
    //执行
    cmd.call()
    //关闭
    git.close()
    logger.info("File update succeeded")
    clientLogger.info("File has been updated")
} else {
    //不存在,使用 clone 克隆对应分支 
    logger.info("Cloning ${srcUrl} repository to ${destinationFolder}")
    clientLogger.info("Prepare to create a new file directory, verifying the repository")
    
    //调用 clone 类 下的 方法
    CloneCommand cmd = Git.cloneRepository().setCredentialsProvider(credentialsProvider);
    cmd.setBare(bare)
    //判断是否判断全部分区
    if (allBranches){
        logger.info("Start cloning all branches")
        clientLogger.info("Start cloning all branches")
        cmd.setCloneAllBranches(true)
    }else{
        logger.info("Start cloning branch ${branch}")
        clientLogger.info("Start cloning branch ${branch}")
        cmd.setBranch(branch)       
    }
    cmd.setCloneSubmodules(includeSubmodules)
    //设置路径
    cmd.setURI(srcUrl)
    //设置文件夹路径
    cmd.setDirectory(destinationFolder)
    //执行
    Git git = cmd.call();
    git.repository.close()
    logger.info("clone is complete")
    clientLogger.info("clone is complete")
}
 
logger.info("Operation completed")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值