如何通过Jenkins groovy脚本删除Jenkins工作区中的特定文件

时间:2019-03-06 12:18:57

标签: jenkins jenkins-pipeline jenkins-groovy

我有一个Jenkins管道,该管道通过SCM触发了Jenkins groovy脚本,该脚本将创建一个文件(如果该文件不存在)并编写,否则它将更新该文件并做一些工作,该文件需要删除。

下面是创建,写入和更新文件的代码。

import re

...
for line in data:
    res = re.findall(r"CK\-[0-9]+\-[0-9]+", line)
    if len(res) != 0:
        print(res[0])

做完一些事情后,我需要删除此文件, 我尝试如下删除它,但是它不起作用。

node(node_label){
     if (fileExists ( file_path+'/'+file_name ) ){
          def readContent = readFile file_path+'/'+file_name
          writeFile file: file_path+'/'+file_name, text: readContent+'\r\n'+data
     }else{
          writeFile file: file_path+'/'+file_name, text:data
     }
 }

1 个答案:

答案 0 :(得分:0)

我有以下用于手动清理工作区的工具,如您所言,它也可以工作,请检查波纹管。

我假设您可能没有正确获取文件路径

[ERROR] Failed to execute goal org.apache.cxf:cxf-codegen-plugin:3.2.1:wsdl2java (generate-sources) on project model-service: org.apache.cxf.helpers.JavaUtils.isJava9Compatible()Z -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.cxf:cxf-codegen-plugin:3.2.1:wsdl2java (generate-sources) on project model-service: org.apache.cxf.helpers.JavaUtils.isJava9Compatible()Z
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: org.apache.cxf.helpers.JavaUtils.isJava9Compatible()Z
    at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.generate (WSDL2JavaMojo.java:434)
    at org.apache.cxf.maven_plugin.AbstractCodegenMoho.execute (AbstractCodegenMoho.java:279)
    at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.execute (WSDL2JavaMojo.java:511)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: java.lang.NoSuchMethodError: org.apache.cxf.helpers.JavaUtils.isJava9Compatible()Z
    at org.apache.cxf.transport.http.CXFAuthenticator.addAuthenticator (CXFAuthenticator.java:55)
    at org.apache.cxf.transport.http.URLConnectionHTTPConduit.<init> (URLConnectionHTTPConduit.java:89)
    at org.apache.cxf.transport.http.HTTPTransportFactory.getConduit (HTTPTransportFactory.java:236)
    at org.apache.cxf.transport.http.HTTPTransportFactory.getConduit (HTTPTransportFactory.java:215)
    at org.apache.cxf.transport.TransportURIResolver.resolve (TransportURIResolver.java:115)
    at org.apache.cxf.wsdl11.CatalogWSDLLocator.getImportInputSource (CatalogWSDLLocator.java:114)
    at org.apache.cxf.wsdl11.AbstractWrapperWSDLLocator.getImportInputSource (AbstractWrapperWSDLLocator.java:85)
    at com.ibm.wsdl.xml.WSDLReaderImpl.parseSchema (WSDLReaderImpl.java:774)
    at com.ibm.wsdl.xml.WSDLReaderImpl.parseSchema (WSDLReaderImpl.java:864)
    at com.ibm.wsdl.xml.WSDLReaderImpl.parseSchema (WSDLReaderImpl.java:654)
    at com.ibm.wsdl.xml.WSDLReaderImpl.parseTypes (WSDLReaderImpl.java:610)
    at com.ibm.wsdl.xml.WSDLReaderImpl.parseDefinitions (WSDLReaderImpl.java:320)
    at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL (WSDLReaderImpl.java:2352)
    at com.ibm.wsdl.xml.WSDLReaderImpl.parseImport (WSDLReaderImpl.java:470)
    at com.ibm.wsdl.xml.WSDLReaderImpl.parseDefinitions (WSDLReaderImpl.java:312)
    at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL (WSDLReaderImpl.java:2352)
    at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL (WSDLReaderImpl.java:2338)
    at org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition (WSDLManagerImpl.java:254)
    at org.apache.cxf.wsdl11.WSDLManagerImpl.getDefinition (WSDLManagerImpl.java:164)
    at org.apache.cxf.tools.wsdlto.core.WSDLDefinitionBuilder.parseWSDL (WSDLDefinitionBuilder.java:80)
    at org.apache.cxf.tools.wsdlto.core.WSDLDefinitionBuilder.build (WSDLDefinitionBuilder.java:71)
    at org.apache.cxf.tools.wsdlto.frontend.jaxws.wsdl11.JAXWSDefinitionBuilder.build (JAXWSDefinitionBuilder.java:83)
    at org.apache.cxf.tools.wsdlto.frontend.jaxws.wsdl11.JAXWSDefinitionBuilder.build (JAXWSDefinitionBuilder.java:60)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.processWsdl (WSDLToJavaContainer.java:195)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute (WSDLToJavaContainer.java:164)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute (WSDLToJavaContainer.java:412)
    at org.apache.cxf.tools.common.toolspec.ToolRunner.runTool (ToolRunner.java:105)
    at org.apache.cxf.tools.wsdlto.WSDLToJava.run (WSDLToJava.java:113)
    at org.apache.cxf.tools.wsdlto.WSDLToJava.run (WSDLToJava.java:86)
    at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.generate (WSDL2JavaMojo.java:414)
    at org.apache.cxf.maven_plugin.AbstractCodegenMoho.execute (AbstractCodegenMoho.java:279)
    at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.execute (WSDL2JavaMojo.java:511)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)

因此,清理的核心是:

//load jobs
def jobs = Jenkins.instance.getAllItems(Job.class)

//iterate over
for(job in jobs) {

//seems like they dont have workspace
  if(job instanceof hudson.model.ExternalJob){
    continue
  }


     String workspace = null

     //pipelines dont have workspace
     if(job instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob){
       println ("workflow job, not cleaning")   
       continue
     }

     try{
       workspace = job.workspace
     }catch(Exception e){
         //already clean eg.
       println ("no workspace, not cleaning")   
       workspace = null
     }

     if(workspace != null){
       //creation of the workspace and modules folder
       //again not sure, but sometimes was failing due boolean ..
       if(workspace instanceof java.lang.Boolean){
        println "cant cleanup"
        continue
       }

       File folder = new File(workspace) 

       //Check if the Workspace folder really exists
       if(folder!=null && folder.exists()){ 
         //workspace cleanup

         //get files
         File[] files = null
         try{
          files=new File(workspace).listFiles().sort(){
             //println it.name  

             if(!it.isFile()){
               it.deleteDir()
             }else{
               it.delete()
             }
           }
         }catch(Exception e){
            println "cant clean: " + workspace          
         } 

       }else{
        println "workspace is not existing, not cleaning"
       }  
   }
}