J.K.
J.K.

Reputation: 13

How to delete a build from Jenkins job workspace

I wonder if it is possible to remove only one build (including artifacts) from job workspace.

I tried to "Delete Build" in Build History but all it does is remove build reference from Build History table. I know I can ssh to a server and delete files from the command line but I am looking for a way to do it from Jenkins web interface.

After installing Workspace Cleanup Plugin I am able to wipe out current workspace but I want to keep my other builds in the workspace.

Upvotes: 1

Views: 6209

Answers (2)

Florian
Florian

Reputation: 42872

I also use "General/Advanced/Use custom workspace" (as in @pabloduo's answer) on a Windows machine with something like:

C:\${JOB_NAME}\${BUILD_NUMBER}

Just wanted to add a solution for getting rid of the build job's workspaces.

I use Groovy Events Listener Plugin for this.

Using the plug-in's standard configuration I just use the following Groovy script:

if (event == Event.JOB_DELETED){
  new File(env.WORKSPACE).deleteDir()
}

And now the custom workspace is deleted when the build job is deleted.

Just be aware that this would also delete non-custom workspaces (because the event is triggered for all jobs on your Jenkins server).

Upvotes: 0

pabloduo
pabloduo

Reputation: 147

In your Jenkins instance, to be able to have folder/per build - set flag "Use custom workspace" in your job's settings. Here is a brief help info from the setting description:

For each job on Jenkins, Jenkins allocates a unique "workspace directory." 
This is the directory where the code is checked out and builds happen. 
Normally you should let Jenkins allocate and clean up workspace directories, 
but in several situations this is problematic, and in such case, this option 
lets you specify the workspace location manually.
One such situation is where paths are hard-coded and the code needs to be 
built on a specific location. While there's no doubt that such a build is 
not ideal, this option allows you to get going in such a situation.

...

And your custom directory path would look like this:

workspace\$JOB_NAME\$BUILD_NUMBER ~> workspace\my-job-name\123

where $JOB_NAME will be "my-job-name" and $BUILD_NUMBER is the build number, eq. "123".

There is one nasty problem with this approach and this is why I wouldn't recommend to use it - Jenkins will not be able to reclaim disk space for outdated builds. You would have to handle cleanup of outdated builds manually and it is a lot of hassle.

Alternative approach, that gives you more control, tools and is able to keep disk space usage under control (without your supervision) is to use default workspace settings and archive your build output (files, original source code, libraries and etc.) as a post-build action. Very-very handy and gives you access to a whole bunch of great tools like, Copy Artifact Plugin or ArtifactDeployer Plugin in other jobs.

Hope that info helps you make a decision that fits your needs best.

Upvotes: 4

Related Questions