Jenkins Pipeline Wipe Out Workspace

前端 未结 13 1425
你的背包
你的背包 2020-12-04 08:59

We are running Jenkins 2.x and love the new Pipeline plugin. However, with so many branches in a repository, disk space fills up quickly.

Is there any plugin that

相关标签:
13条回答
  • 2020-12-04 09:30

    You can use deleteDir() as the last step of the pipeline Jenkinsfile (assuming you didn't change the working directory).

    0 讨论(0)
  • 2020-12-04 09:30

    I used deleteDir() as follows:

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

    However, I then had to also run a Success or Failure AFTER always but you cannot order the post conditions. The current order is always, changed, aborted, failure, success and then unstable.

    However, there is a very useful post condition, cleanup which always runs last, see https://jenkins.io/doc/book/pipeline/syntax/

    So in the end my post was as follows :

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

    Hopefully this may be helpful for some corner cases

    0 讨论(0)
  • 2020-12-04 09:35

    We make sure we are working with a clean workspace by using a feature of the git plugin. You can add additional behaviors like 'Clean before checkout'. We use this as well for 'Prune stale remote-tracking branches'.

    0 讨论(0)
  • 2020-12-04 09:35

    In my case, I want to clear out old files at the beginning of the build, but this is problematic since the source code has been checked out.

    My solution is to ask git to clean out any files (from the last build) that it doesn't know about:

        sh "git clean -x -f"
    

    That way I can start the build out clean, and if it fails, the workspace isn't cleaned out and therefore easily debuggable.

    0 讨论(0)
  • 2020-12-04 09:36

    Using the following pipeline script:

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

    Follow these steps:

    1. Navigate to the latest build of the pipeline job you would like to clean the workspace of.
    2. Click the Replay link in the LHS menu.
    3. Paste the above script in the text box and click Run
    0 讨论(0)
  • 2020-12-04 09:38

    If you have used custom workspace in Jenkins then deleteDir() will not delete @tmp folder.

    So to delete @tmp along with workspace use 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 work for default workspace also.

    0 讨论(0)
提交回复
热议问题