【问题标题】:how to delete a particular file in Jenkins workspace via Jenkins groovy script如何通过 Jenkins groovy 脚本删除 Jenkins 工作区中的特定文件
【发布时间】:2019-03-06 12:18:56
【问题描述】:

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

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

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
     }
 }

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

def Delfile = new File(path+'/'+file_name)
Delfile.delete()

【问题讨论】:

  • 嗨,检查我的答案,我使用的是相同的,但仅适用于正在加载的所有作业,所以它应该可以工作。那么,在您的情况下什么不起作用?你应该能够打印Delfile(由于命名约定,驼峰式,应该是delFile btw ..,它是一个groovy,“基于”java)。所以问题是 pathfile_name 的值是什么,.. 你有什么错误吗?

标签: jenkins jenkins-pipeline jenkins-groovy


【解决方案1】:

我有以下手动工作区清理,所以正如你提到的,它应该也可以工作,请检查以下内容。

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

//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"
       }  
   }
} 

所以,清理的核心是:

  //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          
         } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多