BKM for passing data between stages


In regards to DSL pipeline what is the best way to pass data between stages?

Currently there are some hack's using the env object to access data globally.  I don't like this since I've seen numerous bugs related to the implicit .toString() when setting env.  There also seems to be a difference between env and environment{} and mutability.

The documents say shared libs /vars/.groovy scripts are global variables... but they look like global methods to me.  I can't define anything outside of the method blocks.  When I think of a global variable, a method isn't it.  I'm looking for a way to persist data across stages.

I haven't tried using /src/.groovy classes much.  I've seen an example of a @Singleton groovy class on SO, but I couldn't get this to work.  I haven't had much luck in general with groovy classes and DSL pipelines.  this becomes the same problem, i want to persist a class object across many stages, but where do I store the reference? Outside the pipeline{} block?  Then pass the object into every DSL method?

I have many different DSL pipelines for many different programs these all import 1 common devops lib and one project specific lib.  Shared libraries are only using the /vars/.groovy currently and the pipelines make heavy use of the script{} blocks to access functions in the /vars/.groovy. Im pretty sure we're not using /vars/.groovy as it was intended but... it works.

Any pointer would be great!.




1 comment

  • 0
    Denys Digtiar

    The Declarative Pipeline is restricted by design. Since you already use `script {}` block heavily, you might be looking at the situation where Scripted Pipeline suites your use case better. The relevant feature request that should give you more context is https://issues.jenkins-ci.org/browse/JENKINS-41335

Please sign in to leave a comment.