4

I have Jenkins setup without nodes, all builds are run on the same machine. I want to setup a Groovy script to cleanup old workspaces, so I want to use:

proj.scm.processWorkspaceBeforeDeletion(@Nonnull Job<?,?> project,
                                        @Nonnull FilePath workspace,
                                        @Nonnull Node node)

What value should I put for node when on master?

luka5z
  • 7,525
  • 6
  • 29
  • 52
Gurvan
  • 741
  • 1
  • 9
  • 22
  • Hint: There's the [Workspace Cleanup Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Workspace+Cleanup+Plugin) if it's not about old workspaces but about current. – Gerold Broser May 10 '16 at 13:00
  • In fact I want to keep workspaces available for users to be able to investigate, I only empty workspaces when they are older than a month, meaning no real activity. – Gurvan May 10 '16 at 14:37

1 Answers1

4

Execute below script to clean-up all Jenkins workspaces with Groovy:

import hudson.model.* // For each project

for(item in Jenkins.instance.items)      
{ // check that job is not building
    if(!item.isBuilding())     
    { 
         println("Wiping out workspace of job "+item.name)    
         item.doDoWipeOutWorkspace() 
    } else { 
         println("Skipping job "+item.name+", currently building") 
    } 
 }
luka5z
  • 7,525
  • 6
  • 29
  • 52
  • Isn't it better to use `Jenkins.instance`, since [`Hudson.getInstance()`](http://javadoc.jenkins-ci.org/hudson/model/Hudson.html#getInstance%28%29) is deprecated? – Gerold Broser May 10 '16 at 09:26
  • groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.job.WorkflowJob.doDoWipeOutWorkspace() – Cui Pengfei 崔鹏飞 Jan 02 '18 at 15:28
  • This solution does not work anymore. `isBuilding()` isn't supported. `groovy.lang.MissingMethodException: No signature of method: jenkins.branch.OrganizationFolder.isBuilding() is applicable for argument types: () values: [] Possible solutions: isBuildable(), isBuildable()` – Neil P. Jun 04 '19 at 15:20