首页 文章

Jenkins Pipeline擦除工作区

提问于
浏览
87

我们正在运行Jenkins 2.x并喜欢新的Pipeline插件 . 但是,如果存储库中有如此多的分支,则磁盘空间会很快填满 .

是否有任何与Pipeline兼容的插件可以在成功的构建中消除工作空间?

10 回答

  • 3

    Cleaning up:由于管道的post部分保证在Pipeline执行结束时运行,我们可以添加一些通知或其他步骤来执行finalization,notification或其他End-of-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 */
            }
        }
    }
    
  • 91

    在高级克隆行为中使用 Git shallow clone

    enter image description here

    而另一种方法是用 git clean -fdx 清理树

  • 1

    使用以下管道脚本:

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

    跟着这些步骤:

    • 导航到要清理工作区的管道作业的最新版本 .

    • 单击LHS菜单中的重播链接 .

    • 在文本框中粘贴上述脚本,然后单击“运行”

  • 0

    使用'WipeWorkspace'扩展似乎也可以 . 它需要更长的形式:

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

    更多细节:https://support.cloudbees.com/hc/en-us/articles/226122247-How-to-Customize-Checkout-for-Pipeline-Multibranch-

    这里有可用的GitSCM扩展:https://github.com/jenkinsci/git-plugin/tree/master/src/main/java/hudson/plugins/git/extensions/impl

  • 5

    就像@gotgenes指出 Jenkins Version. 2.74 一样,下面的作品,不确定从什么时候开始,也许如果有人可以编辑并添加上面的版本

    cleanWs()
    

    我用的是 Jenkins Version 2.16Workspace Cleanup Plugin,我用

    step([$class: 'WsCleanup'])
    

    删除工作区 .

    你可以去看看

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

    然后从Sample步骤中选择“step:General Build Step”,然后从Build步骤中选择“在构建完成时删除工作区”

  • 2

    实际上,deleteDir函数以递归方式删除当前目录及其内容 . 将不会遵循符号链接和联结,但将被删除 .

    要删除工作空间的特定目录,请在dir步骤中包装deleteDir步骤 .

    dir('directoryToDelete') {
        deleteDir()
    }
    
  • 0

    提到的解决方案 deleteDir()cleanWs() (如果使用workspace cleanup plugin)都有效, but the recommendation to use it in an extra build step is usually not the desired solution . 如果构建失败并且管道中止,则永远不会到达此清理阶段,因此未在失败的构建上清除工作区 .

    =>在大多数情况下,你应该把它放在post-built-step condition中,如 always

    pipeline {
        agent any
        stages {
            stage('Example') {
                steps {
                    echo 'Hello World'
                }
            }
        }
        post { 
            always { 
                cleanWs()
            }
        }
    }
    
  • 27

    我使用deleteDir()如下:

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

    但是,我之后不得不经常成功或失败,但你不能订购后期条件 . 当前订单始终,更改,中止,失败,成功然后不稳定 .

    但是,有一个非常有用的后置条件, cleanup 总是最后运行,请参阅https://jenkins.io/doc/book/pipeline/syntax/

    所以最后我的帖子如下:

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

    希望这可能对某些角落案件有所帮助

  • 75

    您可以使用deleteDir()作为管道Jenkinsfile的最后一步(假设您没有更改工作目录) .

  • 44

    我们通过使用git插件的功能确保我们正在使用干净的工作区 . 您可以添加其他行为,例如“结账前清洁” . 我们也将它用于'Prune陈旧的远程跟踪分支' .

相关问题