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

**URL:** <https://community.jenkins.io/t/plugin-apis-to-use-implementing-declarative-options-controlling-build-flow/5130>\
**Category:** Ask a question\
**Created:** [December 28, 2022, 11:05am UTC](https://community.jenkins.io/t/plugin-apis-to-use-implementing-declarative-options-controlling-build-flow/5130 "2022-12-28T11:05:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vlad](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@vlad](https://community.jenkins.io/u/vlad)\
**Post date:** [December 28, 2022, 11:05am UTC](https://community.jenkins.io/t/plugin-apis-to-use-implementing-declarative-options-controlling-build-flow/5130/1 "2022-12-28T11:05:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [December 28, 2022, 4:02pm UTC](https://community.jenkins.io/t/plugin-apis-to-use-implementing-declarative-options-controlling-build-flow/5130/2 "2022-12-28T16:02:13Z")

</div>

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/AnyOfConditional.java at e9fa6fec92821856b42be8419c1b558bbd6f06a6 · jenkinsci/pipeline-model-definition-plugin · GitHub](https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/e9fa6fec92821856b42be8419c1b558bbd6f06a6/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AnyOfConditional.java)

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

---

<div class="post-metadata">

**Author:** ![vlad](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@vlad](https://community.jenkins.io/u/vlad)\
**Post date:** [December 28, 2022, 4:11pm UTC](https://community.jenkins.io/t/plugin-apis-to-use-implementing-declarative-options-controlling-build-flow/5130/3 "2022-12-28T16:11:45Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [December 28, 2022, 4:47pm UTC](https://community.jenkins.io/t/plugin-apis-to-use-implementing-declarative-options-controlling-build-flow/5130/4 "2022-12-28T16:47:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vlad](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@vlad](https://community.jenkins.io/u/vlad)\
**Post date:** [December 28, 2022, 4:56pm UTC](https://community.jenkins.io/t/plugin-apis-to-use-implementing-declarative-options-controlling-build-flow/5130/5 "2022-12-28T16:56:27Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [December 28, 2022, 5:07pm UTC](https://community.jenkins.io/t/plugin-apis-to-use-implementing-declarative-options-controlling-build-flow/5130/6 "2022-12-28T17:07:15Z")

</div>

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](https://github.com/jenkinsci/pipeline-model-definition-plugin/tree/master/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/options/impl)

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