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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 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.