Skip to content

Instantly share code, notes, and snippets.

@floriangosse
Created October 29, 2025 08:50
Show Gist options
  • Select an option

  • Save floriangosse/3ffe84a1b88629ec7be8d6377855ade1 to your computer and use it in GitHub Desktop.

Select an option

Save floriangosse/3ffe84a1b88629ec7be8d6377855ade1 to your computer and use it in GitHub Desktop.
Jenkins "Crime Scene Cleaner" which run clean up tasks on every available node

Add these pipeline files as jobs to Jenkins and adjust the clean up tasks in the worker file.

pipeline {
// Run on the controller to avoid provisioning agents
agent any // use 'master' on older Jenkins
triggers {
cron('0 */2 * * *')
}
stages {
stage('Schedule cleaners') {
steps {
script {
// Collect node names (Strings) of online, not temporarily-offline agents
def nodeNames = []
({ ->
def j = jenkins.model.Jenkins.get()
try {
j.nodes.each { n ->
def c = n.toComputer()
if (c?.online && !c?.temporarilyOffline) {
nodeNames << n.nodeName
}
}
} finally {
// Drop references to Jenkins/Node objects before any checkpoint
j = null
}
})()
// Trigger the worker job once per node
nodeNames.each { nodeName ->
// Modern parameter syntax:
build job: './crime-scene-cleaner-worker',
wait: false,
parameters: [string(name: 'nodeName', value: nodeName)]
}
}
}
}
}
}
pipeline {
parameters {
string(
name: 'nodeName',
description: 'jenkins node to run on'
)
}
agent { node "${params.nodeName}" }
stages {
stage('Work') {
steps {
echo "Hello from ${env.NODE_NAME}"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment