Add these pipeline files as jobs to Jenkins and adjust the clean up tasks in the worker file.
Created
October 29, 2025 08:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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