Jelly/Plugin pipeline script support for optionalBlock

173 views
Skip to first unread message

andrew...@xtra.co.nz

unread,
Jan 17, 2017, 7:55:15 PM1/17/17
to Jenkins Developers

When updating a plugin to support pipeline script, how do I handle an optionalBlock section?


I've been using https://github.com/jenkinsci/pipeline-plugin/blob/master/DEVGUIDE.md#user-content-build-wrappers-1 as a guide to updating my plugins to support pipeline script and that went well until I tried up update a plugin that uses an optionalBlock.


Using the pipeline syntax generator I get an exception that it is expecting a boolean for the optional block parameter and if I make it a boolean it complains about needing to pass a JSonObject!!!



<f:optionalBlock title="Wait Till Build Completed" field="waitTillBuildCompleted" >
    <f:entry title="Show BuildMaster Log on Failure" field="printLogOnFailure" >
      <f:checkbox default="false" />
    </f:entry>
</f:optionalBlock>


public DeployToStageBuilder(String toStage, JSONObject waitTillBuildCompleted) {
    this.toStage = toStage;

    if (waitTillBuildCompleted != null) { 
        this.waitTillBuildCompleted = true; 
        this.printLogOnFailure = "true".equalsIgnoreCase(waitTillBuildCompleted.getString("printLogOnFailure"));
    } else { 
        this.waitTillBuildCompleted = false; 
        this.printLogOnFailure = false;
    }
}

Jesse Glick

unread,
Jan 18, 2017, 11:13:37 AM1/18/17
to Jenkins Dev
On Tue, Jan 17, 2017 at 7:55 PM, <andrew...@xtra.co.nz> wrote:
> When updating a plugin to support pipeline script, how do I handle an
> optionalBlock section?

Do not use `f:optionalBlock`. This is a low-level control. Do not
refer to `JSONObject` either. All this is old-style deprecated form
binding.

Use a high-level control like `f:optionalProperty` for nullable
structs (`ui-samples-plugin` has an example), or simple independent
`f:checkbox`es for `boolean` properties (`doCheck*` form validation
can reject invalid combinations).

Also use `@DataBoundSetter` for any property with a plausible default.

Andrew Sumner

unread,
Feb 6, 2017, 3:47:28 AM2/6/17
to jenkin...@googlegroups.com
Is there any other options for creating foldable sections that will also work with the pipeline syntax generator?
 
optionalBlock: will not add the fields inside the foldable block to the generated syntax
optionalProperty: couldn’t get that to work and I’m not sure that it will indent the child fields like optionalBlock
Simple independent fields: lose visual queue that field is dependant on another field being set


--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/uGVIkV7u78Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-dev+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr0KKVKGvJzvgBBAEGdDNshQ%3DY8wgtrzyXsuPst4y60fag%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.


andrew...@xtra.co.nz

unread,
Feb 16, 2017, 4:30:55 AM2/16/17
to Jenkins Developers

Maybe I'm not looking in the right places or I just don't understand the code but I cannot see how to get an optionalProperty to work.  The only example I can find this the ui-samples-plugin which has an overly complicated example that I cannot follow.

 

1. Do you have any advice on how to search existing plugin code for optionalProperty usage?  Google isn't that helpful...

2. Can you either:
  • point me at a plugin that has a simple implementation that I could use to adapt the following code

or

  • suggest what changes I need to make to convert the following optionalBlock to an optionalProperty?

 


 

Jelly Snippet

<f:optionalBlock title="Set Build Variables in BuildMaster" field="setBuildVariables" >
    <f:entry title="Copy Previous Build's Variables" field="preserveVariables" >


        <f:checkbox default="false" />
    </f:entry>

    <f:entry title="Variables" field="variables" >
        <f:expandableTextbox />
    </f:entry>  
</f:optionalBlock>

 

Class Snippet

@DataBoundSetter
public final void setSetBuildVariables(SetBuildVariables setBuildVariables) {
    if (setBuildVariables != null) {
        this.setBuildVariables = true;
        this.preserveVariables = setBuildVariables.preserveVariables;
        this.variables = setBuildVariables.variables;
    } else {
        this.setBuildVariables = false;
        this.preserveVariables = false;
        this.variables = null;
    }
}
 
public static final class SetBuildVariables {
    private final boolean preserveVariables;
    private final String variables;

    @DataBoundConstructor
    public SetBuildVariables(boolean preserveVariables, String variables) {
        this.preserveVariables = preserveVariables;
        this.variables = variables;
    }
}

 

Thanks in advance

  Andrew

Jesse Glick

unread,
Feb 16, 2017, 12:17:25 PM2/16/17
to Jenkins Dev
On Thu, Feb 16, 2017 at 4:30 AM, <andrew...@xtra.co.nz> wrote:
>The only example I can find this the ui-samples-plugin which has an overly complicated example that I cannot follow.

That is the cleanest example I know of. You can use GitHub search to
look for others but I cannot say whether they will be good examples or
bad examples, unless I wrote them myself.

andrew...@xtra.co.nz

unread,
Feb 17, 2017, 9:22:48 PM2/17/17
to Jenkins Developers
Well how about pointing me at some documentation then since it's pretty obvious by now that the code doesn't make a hell of a lot of sense to me.

I wouldn't have though that my requirements for the UI where that unusual that it would be so hard to get it working with pipeline script.

andrew...@xtra.co.nz

unread,
Feb 17, 2017, 11:53:53 PM2/17/17
to Jenkins Developers

I've finally got it working, one remaining issue:

Is there any way to make optionalProperty backwards compatible with optionalBlock?  When I publish the update to this plugin I don't want people to have to reconfigure there jobs.  If not, is there any way to mark the plugin as not backwards compatible?


For anyone else struggling with this the answer is that an optionalProperty needs to have it's own class and config.jelly.

For me that meant:
1. the original optional block became <f:optionalProperty title="Wait Till Build Completed" field="waitTillBuildCompleted" />
2. a new class:
public class WaitTillCompleted extends AbstractDescribableImpl<WaitTillCompleted> {
 private final boolean printLogOnFailure;
 public boolean isPrintLogOnFailure() {
  return printLogOnFailure;
 }
 @DataBoundConstructor
 public WaitTillCompleted(boolean printLogOnFailure) {
        this.printLogOnFailure = printLogOnFailure;
    }
   
    @Extension
    public static class DescriptorImpl extends Descriptor<WaitTillCompleted> {
        @Override
        public String getDisplayName() {
            return "";
        }
    }
}
3. A new config.jelly for the above class
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
    <f:entry title="Show BuildMaster Log on Failure" field="printLogOnFailure" >
      <f:checkbox default="false" />
    </f:entry>
</j:jelly> 
Cheers
  Andrew

Robert Sandell

unread,
Feb 20, 2017, 6:31:51 AM2/20/17
to jenkin...@googlegroups.com

--
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-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/86581742-e4dd-4e26-943b-62ba8bd4b7dc%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Robert Sandell
Software Engineer
CloudBees Inc.

Daniel Beck

unread,
Feb 20, 2017, 6:43:36 AM2/20/17
to jenkin...@googlegroups.com

> On 20.02.2017, at 12:31, Robert Sandell <rsan...@cloudbees.com> wrote:
>
> https://wiki.jenkins-ci.org/display/JENKINS/Marking+a+new+plugin+version+as+incompatible+with+older+versions

… which should be only used when there's really no way to make work.

https://jenkins.io/project/governance/#compatibility-matters

Reply all
Reply to author
Forward
0 new messages