workflow-cps-global-lib-plugin icon indicating copy to clipboard operation
workflow-cps-global-lib-plugin copied to clipboard

[JENKINS-65612] Support for immediately cleaning `@libs` workspaces

Open basil opened this issue 3 years ago • 6 comments

See JENKINS-65612. SCMRetriever and SCMSourceRetriever create a @libs workspace for retrieving the shared library before copying it into the build directory. While old build directories are eventually discarded based on the global build discarder configuration, these @libs workspaces tend to stick around for a long time and eventually need to be removed. It would be nice if a flag could be set on SCMRetriever and SCMSourceRetriever to clean these @libs workspaces immediately after we are done with them.

basil avatar May 11 '21 18:05 basil

Why not just implement ItemListener.onDeleted so workspace/xxx@libs/ gets deleted when jobs/xxx/ is deleted?

jglick avatar May 13 '21 17:05 jglick

just

I could see an argument for also implementing that by default under the general principle of not leaking resources. But I think this PR has independent value. Let me explain why.

We (unfortunately) use a lot of static agents, and we want to keep builds as isolated as possible. So our shared library has a vars/nodeWithCleanup.groovy with

def call(Map params = [:], Closure closure) {
  def cleanupClosure = {
    try {
      it()
    } finally {
      cleanWs()
    }
  }

  node(params.label) {
    if (params.workspace) {
      ws(params.workspace) {
        cleanupClosure(closure)
      }
    } else {
      cleanupClosure(closure)
    }
  }
}

which we use universally as a generic substitute for node. This allows us to reclaim the workspace immediately when the run ends, since we were going to wipe it on the next run anyway. This avoids wasting space while jobs are at rest, which is useful for jobs that run very infrequently (we have thousands of jobs that run a few times a month to cover various portions of our test matrix).

We've found the above quite useful, so I also think we would find the feature in this PR useful for the same reason (opt-in, of course, rather than by default).

basil avatar May 13 '21 18:05 basil

Why not just implement ItemListener.onDeleted so workspace/xxx@libs/ gets deleted when jobs/xxx/ is deleted?

Done in #115.

basil avatar Sep 13 '21 21:09 basil

#115 merged, closing this pr

car-roll avatar Nov 04 '21 18:11 car-roll

@car-roll read https://github.com/jenkinsci/workflow-cps-global-lib-plugin/pull/110#issuecomment-840737198

jglick avatar Nov 04 '21 19:11 jglick

As of #172 most of the code in this plugin has been moved to another plugin repository so this PR must be closed. If this change is still needed, please

git clone https://github.com/jenkinsci/pipeline-groovy-lib-plugin
cd pipeline-groovy-lib-plugin
git checkout -b JENKINS-65612
git pull https://github.com/basil/workflow-cps-global-lib-plugin JENKINS-65612

resolve any merge conflicts, and file a fresh PR on the new repository. Be sure to paste a link to this old PR to enable bidirectional navigation.

jglick avatar May 23 '22 23:05 jglick