how to return some values to the caller of the Jenkins plugin

686 views
Skip to first unread message

long song

unread,
Jun 18, 2021, 8:06:40 PM6/18/21
to Jenkins Developers
Hi,

Is there a way that I can receive a string value from running a Jenkins plugin in the Jenkins pipeline script ?

// the myResult is always null after running the Jenkins plugin "MyPlugin"
pipeline {
   agent any
   stages {
      stage('MyPlugin: MyPlugin Run') {
         steps {
             script {
                def myResult = MyPlugin Parameter1: value1, Parameter2: value2
                println "$myResult"  
             }
         }
      }
   }

Here is the MyPlugin implementation.

public class MyPluginBuilder extends Builder implements SimpleBuildStep {
     @Override
     public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listenerthrows IOException, InterruptedException { ... }
}

The return type of the method perform() is void.

By design, Jenkins doesn't want to return anything back.

To get the running result, I am thinking to store the running result as an artifact and parse it out after running MyPlugin.

Do you have some good practice that I can receive the running result from the Jenkins Plugin ?

Thanks !
Long

Ullrich Hafner

unread,
Jun 20, 2021, 9:24:12 AM6/20/21
to JenkinsCI Developers
It is possible to return values from a step (but not from a SimpleBuildStep) 

Before I go into details we need to make sure that this approach makes senes, your requirements are not clear: 
- What is in the value that you want to return? 
- Why do you want to return a value? 
- What do you want to do with the value?

--
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 jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANDLextj0bbX4zwToHLYCFU4EuYMUQYd0r_6NxWNVszKToDVbw%40mail.gmail.com.

long song

unread,
Jun 20, 2021, 6:53:04 PM6/20/21
to Jenkins Developers

Here is how I use the plugin the pipeline script

        steps {
             script {
                def myResult = MyPlugin Parameter1: value1, Parameter2: value2
                println "$myResult"  
             }
         }

I expect to return a Json string or a map object which can contain running status such as "repo: xxx, version: xxx, total incidents: xxx ..."

Are you talking about the interface hudson.tasks.BuildStep ? It returns a boolean but can't transport much infor. 

boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException;

To get the running result, I am thinking to store the running result as an artifact and parse it out after running MyPlugin.

Do you have some good practice that I can receive the running result from the Jenkins Plugin ?

Thanks!

Ullrich Hafner

unread,
Jun 21, 2021, 4:42:08 AM6/21/21
to JenkinsCI Developers

Am 21.06.2021 um 00:52 schrieb long song <songl...@gmail.com>:


Here is how I use the plugin the pipeline script

        steps {
             script {
                def myResult = MyPlugin Parameter1: value1, Parameter2: value2
                println "$myResult"  
             }
         }

I expect to return a Json string or a map object which can contain running status such as "repo: xxx, version: xxx, total incidents: xxx ..."


Is your real requirement just to print out a structured text? Then use the Jenkins logger in your BuildStep. Then you don’t need a return value. 

Are you talking about the interface hudson.tasks.BuildStep ?

No, this could be done with a  `org.jenkinsci.plugins.workflow.steps.Step`


It returns a boolean but can't transport much infor. 

boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException;

To get the running result, I am thinking to store the running result as an artifact and parse it out after running MyPlugin.



This still looks too complicated. Use the Tell-Don't-Ask principle (https://martinfowler.com/bliki/TellDontAsk.html)

Tell your Jenkins plugin what it should display and do not get values and work with these values. 


Jesse Glick

unread,
Jun 21, 2021, 12:29:39 PM6/21/21
to Jenkins Dev
Beware that returning a value from a `Step` means that your step cannot be used naturally in Declarative Pipeline (only by “cheating” with a `script` block). If values need to be passed to other parts of a build, it may be better to use a block-scoped step and define environment variables (this can be done either in a first-class `Step` or in a `SimpleBuildWrapper`), though that assumes plain `String`s rather than structured objects:

yourPlugin(param1: 'one', param2: 'two') {
  sh 'echo using $VERSION from $REPO'
}

long song

unread,
Jun 22, 2021, 1:34:51 PM6/22/21
to Jenkins Developers
hi Ullrich,

In my actual use case, I don't just print it out, but need to call other APIs with the result value.

Thanks for your suggestion. I will have a try to implement the"org.jenkinsci.plugins.workflow.steps.Step"

best regards,
Long

long song

unread,
Jun 22, 2021, 1:38:02 PM6/22/21
to Jenkins Developers
hi Jesse,

Thank you for the input. 

Defining environment variables should be a good solution for my case.

thanks!
Long


--
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 jenkinsci-de...@googlegroups.com.

Ullrich Hafner

unread,
Jun 22, 2021, 4:39:21 PM6/22/21
to JenkinsCI Developers
Am 22.06.2021 um 19:34 schrieb long song <songl...@gmail.com>:

hi Ullrich,

In my actual use case, I don't just print it out, but need to call other APIs with the result value.

Thanks for your suggestion. I will have a try to implement the"org.jenkinsci.plugins.workflow.steps.Step"


Then this is the wrong approach. Either call the API from your plugin or store the results in an object that is controlled by Jenkins (Action).

Reply all
Reply to author
Forward
0 new messages