Return a map/value from a step in a pipeline build

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Return a map/value from a step in a pipeline build

Lakshmi Narasimhan Vaikuntam
Hello Jenkins devs,

Is it possible to get back a return value from a pipeline step? I am looking for a map similar to  checkout step https://issues.jenkins.io/browse/JENKINS-26100.

Currently, I am exporting the values I need to get back from the step to the environment using an EnvironmentContributingAction.

Thanks in advance for your help.

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/44b747fb-9a0f-468d-b4bf-110b46d77342n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Return a map/value from a step in a pipeline build

Victor Martinez
Hey,

You can use the groovy.transform.Field with a global variable in the pipeline, for instance, the below snippet set the value of myStep in the variable myStepData which it's consumed later on in another stage


import groovy.transform.Field
@Field def myStepData = [:]

pipeline {
    ...
    stage('producer') {
        ...
        steps {
            script {
               // myStep returns a map with values
               myStepData = myStep(....)
               ...
            }
         }
     }
     ....
    stage('consumer') {
        ...
        steps {
            script {
               // where <field> is one of the entries
               if (myStepData?.<field>?.equals('acme'))) {
          
               }
            }
         }
     }
}

Cheers
On Thursday, 12 November 2020 at 05:08:37 UTC Lakshmi Narasimhan wrote:
Hello Jenkins devs,

Is it possible to get back a return value from a pipeline step? I am looking for a map similar to  checkout step https://issues.jenkins.io/browse/JENKINS-26100.

Currently, I am exporting the values I need to get back from the step to the environment using an EnvironmentContributingAction.

Thanks in advance for your help.

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/18afc1eb-aa0e-484b-88c9-01bf724c4b2an%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Return a map/value from a step in a pipeline build

Lakshmi Narasimhan Vaikuntam
Hi Victor, 

Thanks for the tip. 

My issue is that the step does not return a map or any value. I am referring to the  "step" metastep. Unlike the "checkout" metastep that returns a map (see  https://issues.jenkins.io/browse/JENKINS-26100), step does not return a value. For instance, this is not possible  or even it is possible, I don't know how I can send back the data from MyStepClass

def vars = step ([$class: "MyStepClass"...] 

But the following is possible because the checkout metastep can get back a map if MySCMClass overrides #buildEnvironment

def vars = checkout ([$class: "MySCMClass"...]   

I am trying to figure out if I can make "MyStepClass" return a map somehow. The workaround is to contribute an action that in turn exports the name-value pairs to the build environment. I can then access it in the script using the env object.

On Thursday, November 12, 2020 at 4:11:32 PM UTC+5:30 [hidden email] wrote:
Hey,

You can use the groovy.transform.Field with a global variable in the pipeline, for instance, the below snippet set the value of myStep in the variable myStepData which it's consumed later on in another stage


import groovy.transform.Field
@Field def myStepData = [:]

pipeline {
    ...
    stage('producer') {
        ...
        steps {
            script {
               // myStep returns a map with values
               myStepData = myStep(....)
               ...
            }
         }
     }
     ....
    stage('consumer') {
        ...
        steps {
            script {
               // where <field> is one of the entries
               if (myStepData?.<field>?.equals('acme'))) {
          
               }
            }
         }
     }
}

Cheers
On Thursday, 12 November 2020 at 05:08:37 UTC Lakshmi Narasimhan wrote:
Hello Jenkins devs,

Is it possible to get back a return value from a pipeline step? I am looking for a map similar to  checkout step https://issues.jenkins.io/browse/JENKINS-26100.

Currently, I am exporting the values I need to get back from the step to the environment using an EnvironmentContributingAction.

Thanks in advance for your help.

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/1ded9bc0-5a11-45a5-b256-41559a008322n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Return a map/value from a step in a pipeline build

Jesse Glick-4
On Thu, Nov 12, 2020 at 6:08 AM Lakshmi Narasimhan
<[hidden email]> wrote:
> My issue is that the step does not return a map or any value. I am referring to the  "step" metastep.

No, you cannot return values if you use `SimpleBuildStep`. You can
extend `Step` directly (Pipeline-specific). But it may be better to
extend `SimpleBuildWrapper` and define environment variables within
its block; this will work in freestyle, Scripted Pipeline, _and_
Declarative Pipeline (without `script` blocks).

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr2H_WD86QDw91f2sme7VQ8GAecK8zeBOrPaxt1eXA5YHQ%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Return a map/value from a step in a pipeline build

Lakshmi Narasimhan Vaikuntam
Hi Jesse, 

Thanks for the suggestion. I went through the docs and implementing Step would certainly work well for my use case.

I am trying to understand  how I can leverage SimpleBuildWrapper to be able to work in different job types. I assume that I need another class to implement the BuildWrapper extension point. In that case, I would end up having something like this
step ([$class: "MyStepClass"...]  // This performs some action and has the return values (name-value pairs)

withMyWrapperClass() { // I need this wrapper to inject those return values from the previous step into the block.
    
    // I can call my step here and have another wrapper block too.
}

I need to send some data from MyStepClass instance to following MyWrapperClass  instance so that it can inject that data into the block's environment. I am tending towards  doing this through an intermediate action created by MyStepClass instance. But if I keep adding actions from my step class to the build, then it is possible that an outer wrapper picks up an action from an inner step.  Same issue will persist if I update just a single action or replace it.

Please let me know if I understood your suggestion properly regarding SimpleBuildWrapper and whether the approach is correct.

On Thursday, November 12, 2020 at 11:49:07 PM UTC+5:30 Jesse Glick wrote:
On Thu, Nov 12, 2020 at 6:08 AM Lakshmi Narasimhan
<[hidden email]> wrote:
> My issue is that the step does not return a map or any value. I am referring to the "step" metastep.

No, you cannot return values if you use `SimpleBuildStep`. You can
extend `Step` directly (Pipeline-specific). But it may be better to
extend `SimpleBuildWrapper` and define environment variables within
its block; this will work in freestyle, Scripted Pipeline, _and_
Declarative Pipeline (without `script` blocks).

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/4874d62e-e6c5-4c40-a00e-e53dbc7cfb50n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Return a map/value from a step in a pipeline build

Jesse Glick-4
On Thu, Dec 10, 2020 at 4:26 AM Lakshmi Narasimhan
<[hidden email]> wrote:
> I am trying to understand  how I can leverage SimpleBuildWrapper to be able to work in different job types.

`SimpleBuildWrapper` is specifically designed (in fact restricted) to
be compatible with traditional job types as well as Pipeline. Please
read

https://www.jenkins.io/doc/developer/plugin-development/pipeline-integration/

> step ([$class: "MyStepClass"...]  // This performs some action and has the return values (name-value pairs)

No, you just define a `@Symbol` and that becomes a Pipeline step name.

> I am tending towards  doing this through an intermediate action

`Run.addAction` affects the build globally, which will lead to
incorrect or confused results if a Pipeline build uses the wrapper
multiple times, or does work after the block ends, or (especially) if
the wrapper is run in parallel. So avoid that.

From a `SimpleBuildWrapper` you can communicate information to nested
steps using simple (textual) environment variables. If you need to
communicate richer substructure contextually, you will need to use
`Step` and check the documentation on `BodyInvoker`.

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr0o1KLdpNpWVarYoycm5v_hKBofXeVyyoYcTiN8JeRQDQ%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Return a map/value from a step in a pipeline build

Matt Sicker
Indeed, if you're modifying run actions inside a pipeline thing like
in credentials-binding, you need to ensure to merge existing actions
into the new one however it makes sense for that usage. If it doesn't
make sense to do so, then attaching actions is likely too coarse for
your use case.

On Thu, Dec 10, 2020 at 10:51 AM Jesse Glick <[hidden email]> wrote:

>
> On Thu, Dec 10, 2020 at 4:26 AM Lakshmi Narasimhan
> <[hidden email]> wrote:
> > I am trying to understand  how I can leverage SimpleBuildWrapper to be able to work in different job types.
>
> `SimpleBuildWrapper` is specifically designed (in fact restricted) to
> be compatible with traditional job types as well as Pipeline. Please
> read
>
> https://www.jenkins.io/doc/developer/plugin-development/pipeline-integration/
>
> > step ([$class: "MyStepClass"...]  // This performs some action and has the return values (name-value pairs)
>
> No, you just define a `@Symbol` and that becomes a Pipeline step name.
>
> > I am tending towards  doing this through an intermediate action
>
> `Run.addAction` affects the build globally, which will lead to
> incorrect or confused results if a Pipeline build uses the wrapper
> multiple times, or does work after the block ends, or (especially) if
> the wrapper is run in parallel. So avoid that.
>
> From a `SimpleBuildWrapper` you can communicate information to nested
> steps using simple (textual) environment variables. If you need to
> communicate richer substructure contextually, you will need to use
> `Step` and check the documentation on `BodyInvoker`.
>
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr0o1KLdpNpWVarYoycm5v_hKBofXeVyyoYcTiN8JeRQDQ%40mail.gmail.com.



--
Matt Sicker
Senior Software Engineer, CloudBees

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CAEot4owXfGvTC27mjSxC7e_SOL%3DgkYnjevzKB%3DUjeg9Afvef-g%40mail.gmail.com.