Reputation: 1197
I want to delete few particular job work spaces present in a slave node. For this I would like to fetch all items present in that particular slave, and then delete the workspace of the item that matches the particular job.
For this, I know
Jenkins.instance.items gives me a list of all items present in that Jenkins instance (in my case it would be the master node).
whereas
Jenkins.items gives me list of all items present in Jenkins.
I need something like,
node.items which will give me list of all items present in that particular node,
where node will be one of Jenkins.instance.nodes
My code currently, looks like
for (node in Jenkins.instance.nodes) {
println("node: " + node.getDisplayName())
if (node.getDisplayName() == 'abc' || node.getDisplayName() == 'def') {
performCleanup(node, node.items)
}
}
def performCleanup(def node, def items) {
for (item in items) {
workspacePath = node.getWorkspaceFor(item)
if (workspacePath == null) {
println(".... could not get workspace path")
continue
}
pathAsString = workspacePath.getRemote()
if (workspacePath.exists()) {
workspacePath.deleteRecursive()
println(".... deleted from location " + pathAsString)
} else {
println(".... nothing to delete at " + pathAsString)
}
}
}
Upvotes: 0
Views: 579
Reputation: 171
I suppose that jenkins nodes do not possess items as such - all the "items", which in your case I believe to be hudson.model.TopLevelItem, exist on master instance, thus nodes do not hold information what they do have. More than that, after looking into https://javadoc.jenkins.io/hudson/model/Node.html#getWorkspaceFor-hudson.model.TopLevelItem- source code, it looks like this method will not help you as well - as if no work space will be found, it will create it, thus instead of deleting you will spam dozens more.
In your case there is another no-brainer available: If you have particular jobs you want to clear from the particular nodes - just iterate through each node root children - https://javadoc.jenkins.io/hudson/model/Node.html#getRootPath-- and check if folder with job name exists - then clean. That is it, all workspaces should be under root, thus just search by directory name.
Upvotes: 1