Plugin APIs to use -- implementing declarative options, controlling build flow

Hey folks. I’m trying to figure out whether it’s possible to write a plugin that would do the following:

  1. provide an option for declarative pipeline, so I could do options { myOption }
  2. when the option is present, either a stage where this option is defined or the whole build would be skipped on a condition obtained from some external service (before selecting agent and checking out)

What are the APIs I should use with “modern” Jenkins? For the first one, I see there is org.jenkinsci.plugins.pipeline.modeldefinition.options.DeclarativeOption and hudson.model.JobPropertyDescriptor, which one is applicable for this usecase? For the second one, should I use SimpleBuildWrapper? I have found some examples in existing plugins but I wanted to double-check so that I don’t accidentally use API that’s not intended for the task or deprecated.

You could look at the “when” implementation here for some tips on how to skip things. I am not sure why you wouldn’t just use a “when” though. You can look at an implementation of a “when” conditional here: pipeline-model-definition-plugin/ at e9fa6fec92821856b42be8419c1b558bbd6f06a6 · jenkinsci/pipeline-model-definition-plugin · GitHub

There are multiple types of abstract classes you can extend for different types of requirements.

I am not sure why you wouldn’t just use a “when” though

I omitted that part; but the original reasoning is so that developers don’t need to include when in every jenkinsfile that also includes myOption. When you need to add both, there’s more space for error: one can accidentally forget to add the when condition, or add the condition but forget the option.

In practice this is a part of a monorepo caching mechanism, and I want to use option block to define which parts of monorepo should be included in a particular build. So the requirements are exactly as outlined in the original post.

As for anyOf implementation: wouldn’t that just allow me to include conditions into the when block, not affecting the options block in any way?

Yes, that is correct, you would need to use it in a when. I think you would need something in the when anyway though because without a when, nothing is checking for bypass, etc. You’d have to determine if your option would be able to add some sort of invisible when or something. The classes near the when code I posted would be your best bet to look at how its implemented.

1 Like

Thank you. I kind of expected that this might not be possible “as is” but was hoping that maybe I was missing something :slight_smile:

Since I would still need the options block, do you happen to know what is the appropriate API to use is currently?

Some options are implemented here: pipeline-model-definition-plugin/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/options/impl at master · jenkinsci/pipeline-model-definition-plugin · GitHub

You may also want to get some more real-time feedback on gitter.

1 Like