jenkins cli和使用groovy脚本的checkout Subversion

jenkins cli和使用groovy脚本的checkout Subversion,jenkins,jenkins-plugins,jenkins-cli,jenkins-scriptler,Jenkins,Jenkins Plugins,Jenkins Cli,Jenkins Scriptler,有没有办法通过在主机上执行groovy脚本来使用Jenkins Cli签出任何Subversion项目?我可以创建SVN客户机管理器[org.tmatesoft.SVN.core.wc.SVNClientManager],但无法真正理解如何在从URL签出SVN项目时使用它 经过大量的尝试和尝试,我得出了以下结论,可能对其他人有用: import jenkins.*; import jenkins.model.*; import hudson.*;

有没有办法通过在主机上执行groovy脚本来使用Jenkins Cli签出任何Subversion项目?我可以创建SVN客户机管理器[org.tmatesoft.SVN.core.wc.SVNClientManager],但无法真正理解如何在从URL签出SVN项目时使用它

经过大量的尝试和尝试,我得出了以下结论,可能对其他人有用:

        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  
        }
import jenkins.*;
导入jenkins.model.*;
进口哈德逊*;
导入hudson.model.*;
导入hudson.slaves.slave计算机;
导入hudson.scm.SubversionSCM;
导入hudson.remoting.Channel;
导入hudson.FilePath;
导入org.tmatesoft.svn.core.SVNURL;
导入org.tmatesoft.svn.core.io.SVNRepository;
导入org.tmatesoft.svn.core.io.SVNRepositoryFactory;
导入org.tmatesoft.svn.core.SVNException;
导入org.tmatesoft.svn.core.wc.SVNClientManager;
导入org.tmatesoft.svn.core.auth.ISVNAuthenticationProvider;
导入org.tmatesoft.svn.core.wc.SVNLogClient;
导入org.tmatesoft.svn.core.SVNDirEntry;
导入org.tmatesoft.svn.core.wc.SVNRevision;
导入org.tmatesoft.svn.core.SVNDepth;
导入org.tmatesoft.svn.core.SVNDirEntry;
导入org.tmatesoft.svn.core.ISVNDirEntryHandler;
导入org.tmatesoft.svn.core.wc.SVNUpdateClient;
导入java.lang.*;
导入java.util.ArrayList;
导入java.util.List;
私有布尔checkNodeExist(字符串节点名称){
if(Jenkins.getInstance().slaves.find({it.name==node\u name})==null)
返回false;
其他的
返回true;
}
专用ISVNAuthenticationProvider createAuthenticationProvider(抽象项目上下文){
返回Jenkins.getInstance().getDescriptorByType(SubversionSCM.DescriptorImpl.class)
.createAuthenticationProvider(上下文);
}
公共类SimpleSVNDirEntryHandler实现ISVNDirEntryHandler{
private final List dirs=new ArrayList();
公共列表getDirs(){
列表排序器=新的ArrayList();
for(SVNDirEntry目录项:目录){
add(dirEntry.getName());
}
返回分拣机;
}
public void handleDirEntry(SVNDirEntry dirEntry)抛出SVNException{
直接添加(直接输入);
}
}           
public void perfromsvnlistocationmaster(SVNURL SVNURL){
试一试{
SVNRepository repo=SVNRepositoryFactory.create(svnUrl);
SVNClientManager clientManager=SubversionSCM.createSvnClientManager(createAuthenticationProvider())
SVNLogClient logClient=clientManager.getLogClient();
SimpleSVNDirEntryHandler-dirEntryHandler=新SimpleSVNDirEntryHandler();
List dirs=new ArrayList();
logClient.doList(repo.getLocation(),SVNRevision.HEAD,SVNRevision.HEAD,false,SVNDepth.INFINITY,SVNDirEntry.DIRENT\u KIND,dirEntryHandler)
dirs=dirEntryHandler.getDirs();
println(dirs)
}
捕获(svnEx异常svnEx){
println“#错误:+svnEx;
抛出svnEx
}   
}
public void PerfromSVNCheckOutOperation(SVNURL SVNURL、boolean isMaster、字符串追加器、SlaveComputer computer=null){
试一试{
SVNRepository repo=SVNRepositoryFactory.create(svnUrl);
SVNClientManager clientManager=SubversionSCM.createSvnClientManager(createAuthenticationProvider());
SVNUpdateClient updateClient=clientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
字符串destDir=svnUrl.getPath().substring(svnUrl.getPath().lastIndexOf('/')+1);
如果(isMaster==true){
updateClient.doCheckout(repo.getLocation(),新的java.io.File(System.getProperty(“java.io.tmpdir”),destDir+''.'+'附录),SVNRevision.HEAD,SVNRevision.HEAD,SVNDepth.INFINITY,false);
}否则{
如果(计算机==null){
抛出新的IllegalArgumentException(“错误:参数:当我们需要在从机中签出时,计算机不能为null”);
}否则{
updateClient.doCheckout(repo.getLocation(),新的java.io.File(System.getProperty(“java.io.tmpdir”),destDir+''.'+'附录),SVNRevision.HEAD,SVNRevision.HEAD,SVNDepth.INFINITY,false);
通道slaveChannel=computer.getChannel();
FilePath fpSrc=new hudson.FilePath(new java.io.File(System.getProperty(“java.io.tmpdir”)、destDir+''+appender));
//println新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“将文件从主目录中的临时目录递归复制到从目录”;
int files_copied=fpSrc.copyrecuriveto(fpDestination);
println文件\u已复制
fpSrc.deleteRecursive();
}
}
}
捕获(例外情况除外){
抛出新异常(“#错误:”,ex);
}
}
如果(args.length==4){
字符串url=新字符串(args[0]);
SVNURL svn_url=null;
试一试{
svn_url=SVNURL.parseURIDecoded(url);
}
捕获(svnEX异常svnEX){