-
Type:
Bug
-
Resolution: Not A Defect
-
Priority:
Minor
-
Component/s: workflow-cps-plugin
-
Environment:Jenkins 2.332.3
I have a scripted pipeline job with this script:
node(null) { Â Â def c = { id -> Â Â Â Â ws('some_ws') { Â Â Â Â Â Â for (int a = 0; a < 10; ++a) { Â Â Â Â Â Â Â Â //id += (int)(Math.random() * 100000) Â Â Â Â Â Â Â Â def loadStepValidationId = "${id} ${a}" Â Â Â Â Â Â Â Â writeFile file: 'script.groovy', text: 'this.loadStepValidationId = \"' + loadStepValidationId + '\"\nreturn this' Â Â Â Â Â Â Â Â def sc = load 'script.groovy' Â Â Â Â Â Â Â Â sleep time: 100, unit: 'MILLISECONDS' Â Â Â Â Â Â Â Â if (loadStepValidationId != sc?.loadStepValidationId) { Â Â Â Â Â Â Â Â Â Â error "Attempted to load script ${loadStepValidationId}, but got ${sc?.loadStepValidationId}." Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â if (isUnix()) { Â Â Â Â Â Â Â Â Â Â sh('rm script.groovy') Â Â Â Â Â Â Â Â } else { Â Â Â Â Â Â Â Â Â Â bat('del /q script.groovy') Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â } Â Â } Â Â def pstages = [:] Â Â for (int i = 0; i < 20; ++i) { Â Â Â Â pstages[i.toString()] = { c(i) } Â Â } Â Â parallel pstages }
It fails because it turns out that referring 'this' in the script returns who knows what instance.
To fix it, I need to turn the variable 'loadStepValidationId' into a function that returns a constant value.
Can someone shed some light on this? This smells like a bug or at least undefined, undocumented behavior.
I ran variations of this script and it seems that all variables that are assigned to 'this' basically belong to a singleton, is this by design? Is this documented somewhere?