I've been reaching to a point that I really want to use iteration to save redundant code on stage level when building multi-platforms. Below is showcasing what I'm trying to do:
def map = [ Bob : 42, Alice: 54, Max : 33 ] pipeline { agent none stages { map.each { entry -> stage ($entry.key) { steps{ timestamps{ echo "$entry.value" } } } } } } I think the concept here is pretty straight forward, but the builds are failing by telling me:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 13: Expected a stage @ line 13, column 9. map.each { entry -> ^ WorkflowScript: 12: No stages specified @ line 12, column 5. stages { ^ 2 errors Does this mean jenkins have not yet support user to iterate on stages? If there's anything wrong with what I did, please let me know. Thanks!
-----EDIT------
BTW, @NonCPS is also tried, I don't think this is a each problem in pipeline script...
def map = [ Bob : 42, Alice: 54, Max : 33 ] pipeline { agent none stages { iter_stages map } } @NonCPS def iter_stages(dd) { dd.each { entry -> stage ($entry.key) { steps{ timestamps{ echo "$entry.value" } } } } } 