Jenkins pipe displaces workspace

We run Jenkins 2.x and love the new Pipeline plugin. However, with so many branches in the repository, the disk space quickly becomes full.

Is there any Pipeline compatible plugin that I can destroy the workspace in a successful build?

+119
jenkins workspace jenkins-pipeline
May 26 '16 at 18:25
source share
11 answers

You can use deleteDir() as the last step of the Jenkins file in the pipeline (provided that you have not changed the working directory).

+93
May 26 '16 at 18:55
source share

As @gotgenes pointed to the Jenkins version. 2.74 , it works below, I'm not sure from the moment when, maybe, if someone can edit and add the version above

 cleanWs() 



With Jenkins version 2.16 and the Workspace Cleanup Plugin that I have, I use

 step([$class: 'WsCleanup']) 

to delete the workspace.

You can view it by selecting

 JENKINS_URL/job/<any Pipeline project>/pipeline-syntax 

Then select “Step: general assembly step” in the “Step” step, and then select “Delete workspace during assembly” from the assembly step

+111
Aug 22 '16 at 15:10
source share

In fact, the deleteDir function recursively deletes the current directory and its contents. Symbolic links and connections will not be respected, but will be deleted.

To delete a specific workspace directory, move the deleteDir step to the dir step.

 dir('directoryToDelete') { deleteDir() } 
+60
Jul 19 '16 at 2:43 on
source share

The mentioned deleteDir() and cleanWs() solutions (if the workspace cleanup plugin is used) work, but the recommendation to use it at an additional build stage is usually not the desired solution . If the assembly fails and the pipeline breaks, this cleaning step is never reached, and therefore the work area is not cleared when the assembly fails.

=> In most cases, you should probably put it in a post-build state , like always :

 pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } post { always { cleanWs() } } } 
+58
Oct 24 '17 at 15:41
source share

I used deleteDir () as follows:

  post { always { deleteDir() /* clean up our workspace */ } } 

However, I also had to always run Success or Failed AFTER, but you cannot order the terms of publication. The current order is always changed, canceled, failure, success, and then unstable.

However, there is a very useful post cleaning state that always works, finally see https://jenkins.io/doc/book/pipeline/syntax/

So, in the end, my post was as follows:

 post { always { } success{ } failure { } cleanup{ deleteDir() } } 

Hope this can be useful for some corner cases.

+12
Aug 02 '18 at 15:08
source share

Using the following pipelined script:

 pipeline { agent { label "master" } options { skipDefaultCheckout() } stages { stage('CleanWorkspace') { steps { cleanWs() } } } } 

Follow these steps:

  1. Go to the last assembly of the conveyor job from which you want to clear the workspace.
  2. Click on the "Play" link in the LHS menu.
  3. Paste the above script into the text box and click Run
+10
Oct 19 '18 at 4:28
source share

If you used the user workspace in Jenkins, then deleteDir () will not delete the @tmp folder.

To remove @tmp along with the workspace, use the following

 pipeline { agent { node { customWorkspace "/home/jenkins/jenkins_workspace/${JOB_NAME}_${BUILD_NUMBER}" } } post { cleanup { /* clean up our workspace */ deleteDir() /* clean up tmp directory */ dir("${workspace}@tmp") { deleteDir() } /* clean up script directory */ dir("${workspace}@script") { deleteDir() } } } } 

This snippet will also work for the default workspace.

+6
Mar 28 '19 at 7:00
source share

We make sure that we work with a clean workspace using the git plugin function. You can add additional behavior, such as "Clear before validation." We also use this for "Truncated branches of remote tracking."

+2
Aug 25 '17 at 9:07 on
source share

Using the WipeWorkspace extension also works. This requires a longer form:

 checkout([ $class: 'GitSCM', branches: scm.branches, extensions: scm.extensions + [[$class: 'WipeWorkspace']], userRemoteConfigs: scm.userRemoteConfigs ]) 

More details here: https://support.cloudbees.com/hc/en-us/articles/226122247-How-to-Customize-Checkout-for-Pipeline-Multibranch-

Available GitSCM extensions are here: https://github.com/jenkinsci/git-plugin/tree/master/src/main/java/hudson/plugins/git/extensions/impl

+1
Nov 16 '17 at 22:07
source share

Cleaning up . Since the post section of the pipeline is guaranteed to be launched at the end of the pipeline, we can add some notifications or other steps to complete the finalization, notification, or other tasks of ending the pipeline.

 pipeline { agent any stages { stage('No-op') { steps { sh 'ls' } } } post { cleanup { echo 'One way or another, I have finished' deleteDir() /* clean up our workspace */ } } } 
+1
Jan 26 '18 at 17:27
source share

Use Git Shallow Clone in Advanced Clone Behavior
enter image description here

And another approach is to clean the tree with git clean -fdx

-one
Mar 26 '18 at 9:48
source share



All Articles