jenkins cli,使用groovy脚本签出Subversion [英] jenkins cli with checkout Subversion using groovy script

查看:212
本文介绍了jenkins cli,使用groovy脚本签出Subversion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以通过在主服务器上执行常规脚本来使用Jenkins-Cli签出任何Subversion项目?我可以说是创建SVN客户端管理器[org.tmatesoft.svn.core.wc.SVNClientManager]的要点,但是真的无法理解如何从URL中签出SVN项目.

Is there a way to check out any Subversion project using Jenkins-Cli by executing a groovy script on the master? I can get to the point of creating SVN client manager[org.tmatesoft.svn.core.wc.SVNClientManager], but can't really understand how to employ that in checking out an SVN project from the URL.

推荐答案

经过反复尝试,我想出了这一点,可能对其他人有用:

After a lot of hit and trials I have come up with this, might be useful for someone else:

        import jenkins.*;
        import jenkins.model.*;
        import hudson.*;
        import hudson.model.*;
        import hudson.slaves.SlaveComputer;
        import hudson.scm.SubversionSCM;
        import hudson.remoting.Channel;
        import hudson.FilePath;

        import org.tmatesoft.svn.core.SVNURL;
        import org.tmatesoft.svn.core.io.SVNRepository;
        import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
        import org.tmatesoft.svn.core.SVNException;
        import org.tmatesoft.svn.core.wc.SVNClientManager;
        import org.tmatesoft.svn.core.auth.ISVNAuthenticationProvider; 
        import org.tmatesoft.svn.core.wc.SVNLogClient;
        import org.tmatesoft.svn.core.SVNDirEntry;
        import org.tmatesoft.svn.core.wc.SVNRevision;
        import org.tmatesoft.svn.core.SVNDepth;
        import org.tmatesoft.svn.core.SVNDirEntry;
        import org.tmatesoft.svn.core.ISVNDirEntryHandler;
        import org.tmatesoft.svn.core.wc.SVNUpdateClient;

        import java.lang.*;
        import java.util.ArrayList;
        import java.util.List;

        private boolean checkNodeExist(String node_Name){
            if (Jenkins.getInstance().slaves.find({it.name == node_Name}) == null)
                return false;
            else
                return true;
        }

        private ISVNAuthenticationProvider createAuthenticationProvider(AbstractProject context) {
            return Jenkins.getInstance().getDescriptorByType(SubversionSCM.DescriptorImpl.class)
                    .createAuthenticationProvider(context);
        }

        public class SimpleSVNDirEntryHandler implements ISVNDirEntryHandler {
            private final List<SVNDirEntry> dirs = new ArrayList<SVNDirEntry>();

            public List<String> getDirs() {
                List<String> sortedDirs = new ArrayList<String>();
                for (SVNDirEntry dirEntry : dirs) {
                    sortedDirs.add(dirEntry.getName());
                }
                return sortedDirs;
            }

            public void handleDirEntry(SVNDirEntry dirEntry) throws SVNException {      
                    dirs.add(dirEntry);     
            }

        }           
        public void PerfromSVNListOperationOnMaster(SVNURL svnUrl){
            try{
                SVNRepository repo = SVNRepositoryFactory.create(svnUrl);
                SVNClientManager clientManager = SubversionSCM.createSvnClientManager(createAuthenticationProvider())
                SVNLogClient logClient = clientManager.getLogClient();
                SimpleSVNDirEntryHandler dirEntryHandler = new SimpleSVNDirEntryHandler();
                List<String> dirs = new ArrayList<String>();
                logClient.doList(repo.getLocation(),SVNRevision.HEAD, SVNRevision.HEAD,false,SVNDepth.INFINITY,SVNDirEntry.DIRENT_KIND,dirEntryHandler)
                dirs = dirEntryHandler.getDirs();
                println (dirs)
            }
            catch(SVNException svnEx){
                println "#Error: " + svnEx;
                throw svnEx
            }   
        }

        public void PerfromSVNCheckOutOperation(SVNURL svnUrl,boolean isMaster,String appender,SlaveComputer computer = null){
            try{
                SVNRepository repo = SVNRepositoryFactory.create(svnUrl);
                SVNClientManager clientManager = SubversionSCM.createSvnClientManager(createAuthenticationProvider());
                SVNUpdateClient updateClient = clientManager.getUpdateClient();
                updateClient.setIgnoreExternals(false);
                String destDir = svnUrl.getPath().substring(svnUrl.getPath().lastIndexOf('/')+1);
                if (isMaster == true){
                    updateClient.doCheckout(repo.getLocation(),new java.io.File(System.getProperty("java.io.tmpdir"),destDir + '_' + appender),SVNRevision.HEAD,SVNRevision.HEAD,SVNDepth.INFINITY,false);
                }else{
                    if (computer == null){
                        throw new IllegalArgumentException("#Error: Argument:computer can't be null when we need to checkout in slave");
                    }else{              
                        updateClient.doCheckout(repo.getLocation(),new java.io.File(System.getProperty("java.io.tmpdir"),destDir + '_' + appender),SVNRevision.HEAD,SVNRevision.HEAD,SVNDepth.INFINITY,false);
                        Channel slaveChannel = computer.getChannel();               
                        FilePath fpSrc = new hudson.FilePath(new java.io.File(System.getProperty("java.io.tmpdir"),destDir + '_' + appender));
                        //println new java.io.File((slave.getWorkspaceRoot().toString()),destDir).toString().replace('\\','/')
                        FilePath fpDestination = new hudson.FilePath(slaveChannel,new java.io.File((slave.getWorkspaceRoot().toString()),destDir + '_' + appender).toString().replace('\\','/'));
                        println "Copying files recursively from Temp directory in master to slave";
                        int files_copied = fpSrc.copyRecursiveTo(fpDestination);
                        println files_copied                
                        fpSrc.deleteRecursive();
                    }
                }
            }
            catch (Exception ex){
                throw new Exception("#Error:",ex);
            }
        }

        if (args.length == 4){
            String url = new String(args[0]);
            SVNURL svn_url = null;
            try{
                svn_url = SVNURL.parseURIDecoded(url);      
            }
            catch(SVNException svnEX){
                println "#Error: Check SVN repository Location.";
                throw svnEX;
            }   
            String nodeName = new String(args[1]);
            String operation = new String(args[2]);
            String checkoutAppendString = new String(args[3]);
            println args    
            if (nodeName.equalsIgnoreCase("master")){
                println "Executing script on master"
                if (operation.equalsIgnoreCase("list")){
                    PerfromSVNListOperationOnMaster(svn_url);
                }else{
                    PerfromSVNCheckOutOperation(svn_url,true,checkoutAppendString);
                }       
            }else{
                if (checkNodeExist(nodeName)){
                    slave = Jenkins.getInstance().slaves.find({it.name == nodeName});
                    SlaveComputer computer = slave.getComputer();
                    if (computer.isOffline()){
                        println "#Error: $slave is offline."
                        return
                    }else{
                        if (operation.equalsIgnoreCase("list")){
                            PerfromSVNListOperationOnMaster(svn_url)
                        }else{
                            PerfromSVNCheckOutOperation(svn_url,false,checkoutAppendString,computer);
                        }           
                    }           
                }else{
                    println "#Error: $nodeName not found."
                    return      
                }           
            }
        }else{
            println "Invalid Usage, expecting 3 arguments : 1.RepositoryURL 2.NodeName 3.OperationType"
            return  
        }

这篇关于jenkins cli,使用groovy脚本签出Subversion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆