How to make jobs inherit folder properties

24 views
Skip to first unread message

Miguelangel Fernandez

unread,
Mar 14, 2018, 8:46:41 AM3/14/18
to Jenkins Developers
Hi,

For a plugin I'm working on I've defined my own folder-level property by extending AbstractFolderProperty<AbstractFolder<?>> and  AbstractDescribableImpl<FolderProperty>, but now I'd like make all jobs inside the folder inherit this property.

What is the best way to go about this? Here's what I have so far.

I need to be able to do this at least for Freestyle and Pipeline jobs. I believe the implementation has to be different for each.
For Freestyle jobs, I've implemented a build wrapper that extends SimpleBuildWrapper and I'm able to use the setUp method to add environment variables to the run. I first do run.getParent() to get the instance of hudson.model.Job and then I the property I've defined from it's parent folder. I got the idea from the way the Jira plugin does it here.

The outstanding part of the problem is Pipeline jobs -i.e. jobs of type org.jenkinsci.plugins.workflow.job.WorkflowJob - because apparently build wrappers don't apply to this type of job. I think the only way to do this is to extend org.jenkinsci.plugins.workflow.cps.GlobalVariable but I've so far been unable to find a way to get a reference to the parent folder from method getValue(CpsScript script)

Can anyone point me in the right direction?

Thank you,

Jesse Glick

unread,
Mar 14, 2018, 10:58:44 AM3/14/18
to Jenkins Dev
On Wed, Mar 14, 2018 at 8:46 AM, Miguelangel Fernandez
<miguela...@gmail.com> wrote:
> For a plugin I'm working on I've defined my own folder-level property by
> extending AbstractFolderProperty<AbstractFolder<?>>

Fine…

> and AbstractDescribableImpl<FolderProperty>

Not sure what this is for though.

> For Freestyle jobs, I've implemented a build wrapper that extends
> SimpleBuildWrapper and I'm able to use the setUp method to add environment
> variables to the run. I first do run.getParent() to get the instance of
> hudson.model.Job and then I the property I've defined from it's parent
> folder.

Generally speaking, this should be a recursive search up to the root
(`Jenkins`).

> The outstanding part of the problem is Pipeline jobs -i.e. jobs of type
> org.jenkinsci.plugins.workflow.job.WorkflowJob - because apparently build
> wrappers don't apply to this type of job.

Sure they do—that is the whole point of using `SimpleBuildWrapper`!
Check *Pipeline Syntax*. If your wrapper defines a `@Symbol`, it will
show up like a step. Otherwise it will appear in an uglier way under
the `wrap`step.

You can also implement the `EnvironmentContributor` extension point if
you wish for environment variable injection to be automatic for all
jobs inside the folder, rather than opt-in using the wrapper. After
all, it would be a bit weird to have the user define a folder property
and then also a build wrapper in each job—why would you not just put
all the configuration into the wrapper to begin with?

Jesse Glick

unread,
Mar 14, 2018, 10:59:42 AM3/14/18
to Jenkins Dev

Miguelangel Fernandez

unread,
Mar 15, 2018, 6:07:21 PM3/15/18
to jenkin...@googlegroups.com
Thanks Jesse, 

Great stuff as always. I see your point. I'll try the EnvironmentContributor. 

And thank you for the link to the docs. I somehow missed this.

On 14 Mar 2018 3:59 pm, "Jesse Glick" <jgl...@cloudbees.com> wrote:
--
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/fyZzwMbaUc4/unsubscribe.
To unsubscribe from this group and all its topics, 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/CANfRfr3iB9ffNocc8LGoVMSP%3D0ykA6y77yEbBU8MTTtbRFdGxw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages