Break init scripts up in to re-usable classes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Break init scripts up in to re-usable classes

Edd Grant
Hi folks,

I'm using the init hook script mechanism ($JENKINS_HOME/init.groovy.d) to programmatically configure Jenkins. I'm building up quite a library of code and want to split it out in to re-usable classes, to keep things maintainable and make it easier to write well focused tests etc. I have tried the following with no luck:

1: Trying to split classes out in to separate scripts and using Groovy's 'evaluate' function to load the classes from each script (basically trying to re-implement some sort of importing behaviour). This doesn't work at all due to the way Groovy manipulates scripts.

2: Putting my classes in a jar file and adding this to WEB-INF/lib in the Jenkins war. This approach leads to classloader errors, presumably because the jar classloader that my code belongs to doesn't have access to Jenkins' PluginManager 'uberClassLoader'?  

3: Same as [2] but adding the jar to the JVM's classpath rather than adding it to the jar. Same result as [2].

Has anyone tried doing anything similar to this before? I'm really keen to be able to do things like organise my code in packages and keep it in small maintainable units. Grateful for any suggestions.

Cheers,

Edd

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/dfb93cdd-c2d9-4c05-a7d6-007158e3d821%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Break init scripts up in to re-usable classes

Edd Grant
Gah, spotted a typo. Just to avoid confusion [3] should say "rather than adding it to the war", not the jar.

Cheers,

Edd

On Monday, 12 March 2018 21:57:42 UTC, Edd Grant wrote:
Hi folks,

I'm using the init hook script mechanism ($JENKINS_HOME/init.groovy.d) to programmatically configure Jenkins. I'm building up quite a library of code and want to split it out in to re-usable classes, to keep things maintainable and make it easier to write well focused tests etc. I have tried the following with no luck:

1: Trying to split classes out in to separate scripts and using Groovy's 'evaluate' function to load the classes from each script (basically trying to re-implement some sort of importing behaviour). This doesn't work at all due to the way Groovy manipulates scripts.

2: Putting my classes in a jar file and adding this to WEB-INF/lib in the Jenkins war. This approach leads to classloader errors, presumably because the jar classloader that my code belongs to doesn't have access to Jenkins' PluginManager 'uberClassLoader'?  

3: Same as [2] but adding the jar to the JVM's classpath rather than adding it to the jar. Same result as [2].

Has anyone tried doing anything similar to this before? I'm really keen to be able to do things like organise my code in packages and keep it in small maintainable units. Grateful for any suggestions.

Cheers,

Edd

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/8cfd77ba-9ad2-4553-8925-17950a90462b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Break init scripts up in to re-usable classes

R. Tyler Croy
In reply to this post by Edd Grant
(replies inline)

On Mon, 12 Mar 2018, Edd Grant wrote:

> Hi folks,
>
> I'm using the init hook script mechanism ($JENKINS_HOME/init.groovy.d) to
> programmatically configure Jenkins. I'm building up quite a library of code
> and want to split it out in to re-usable classes, to keep things
> maintainable and make it easier to write well focused tests etc. I have
> tried the following with no luck:
>
> 1: Trying to split classes out in to separate scripts and using Groovy's
> 'evaluate' function to load the classes from each script (basically trying
> to re-implement some sort of importing behaviour). This doesn't work at all
> due to the way Groovy manipulates scripts.
>
> 2: Putting my classes in a jar file and adding this to WEB-INF/lib in the
> Jenkins war. This approach leads to classloader errors, presumably because
> the jar classloader that my code belongs to doesn't have access to Jenkins'
> PluginManager 'uberClassLoader'?  
>
> 3: Same as [2] but adding the jar to the JVM's classpath rather than adding
> it to the jar. Same result as [2].
>
> Has anyone tried doing anything similar to this before? I'm really keen to
> be able to do things like organise my code in packages and keep it in small
> maintainable units. Grateful for any suggestions.
I have nothing to suggest that will help you refactor those Groovy scripts. As
one who has wielded plenty of init.groovy.d scripts in my day, I can tell you
I'm going to refactor mine by just deleting them in favor of:
    https://github.com/jenkinsci/configuration-as-code-plugin#jenkins-configuration-as-code-plugin


I doubt that helps though :)

- R. Tyler Croy

------------------------------------------------------
     Code: <https://github.com/rtyler>
  Chatter: <https://twitter.com/agentdero>
     xmpp: [hidden email]

  % gpg --keyserver keys.gnupg.net --recv-key 1426C7DC3F51E16F
------------------------------------------------------

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/20180313021342.kwkigbcbjiws4kzb%40blackberry.coupleofllamas.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc (201 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Break init scripts up in to re-usable classes

Edd Grant
In reply to this post by Edd Grant
Thanks for the response R Tyler Croy, I'm suddenly kicking myself for not finding that plugin when I looked for stuff like this!

I notice the plugin is currently at 0.1 alpha, do (or do other folks here) have any experience using it? It it fairly reliable doing the things it mentions in the docs (LDAP config etc)? I'm torn as we have additional requirements such as loading up trust stores etc and I'd rather not have 2 separate mechanisms to configure different bits of Jenkins.

Cheers,

Edd 

On Monday, 12 March 2018 21:57:42 UTC, Edd Grant wrote:
Hi folks,

I'm using the init hook script mechanism ($JENKINS_HOME/init.groovy.d) to programmatically configure Jenkins. I'm building up quite a library of code and want to split it out in to re-usable classes, to keep things maintainable and make it easier to write well focused tests etc. I have tried the following with no luck:

1: Trying to split classes out in to separate scripts and using Groovy's 'evaluate' function to load the classes from each script (basically trying to re-implement some sort of importing behaviour). This doesn't work at all due to the way Groovy manipulates scripts.

2: Putting my classes in a jar file and adding this to WEB-INF/lib in the Jenkins war. This approach leads to classloader errors, presumably because the jar classloader that my code belongs to doesn't have access to Jenkins' PluginManager 'uberClassLoader'?  

3: Same as [2] but adding the jar to the JVM's classpath rather than adding it to the jar. Same result as [2].

Has anyone tried doing anything similar to this before? I'm really keen to be able to do things like organise my code in packages and keep it in small maintainable units. Grateful for any suggestions.

Cheers,

Edd

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/69f48e62-b013-4721-9413-1a43ef53ab2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Break init scripts up in to re-usable classes

Ewelina Wilkosz
you're right, it is only alpha so far and I wouldn't say it's production ready, but should be pretty soon - I hope to have next, more reliable release in April
but I'm part of the team working on the plugin, not using it yet :) so maybe someone here is actually happy enough with current version 

as for LDAP I know there are some issues: https://github.com/jenkinsci/configuration-as-code-plugin/issues/138 and I was not able to solve it, yet
keep an eye on the plugin though, contributors are very active and we really believe it will be THE configuration as code solution for Jenkins

On Tuesday, March 13, 2018 at 10:00:52 AM UTC+1, Edd Grant wrote:
Thanks for the response R Tyler Croy, I'm suddenly kicking myself for not finding that plugin when I looked for stuff like this!

I notice the plugin is currently at 0.1 alpha, do (or do other folks here) have any experience using it? It it fairly reliable doing the things it mentions in the docs (LDAP config etc)? I'm torn as we have additional requirements such as loading up trust stores etc and I'd rather not have 2 separate mechanisms to configure different bits of Jenkins.

Cheers,

Edd 

On Monday, 12 March 2018 21:57:42 UTC, Edd Grant wrote:
Hi folks,

I'm using the init hook script mechanism ($JENKINS_HOME/init.groovy.d) to programmatically configure Jenkins. I'm building up quite a library of code and want to split it out in to re-usable classes, to keep things maintainable and make it easier to write well focused tests etc. I have tried the following with no luck:

1: Trying to split classes out in to separate scripts and using Groovy's 'evaluate' function to load the classes from each script (basically trying to re-implement some sort of importing behaviour). This doesn't work at all due to the way Groovy manipulates scripts.

2: Putting my classes in a jar file and adding this to WEB-INF/lib in the Jenkins war. This approach leads to classloader errors, presumably because the jar classloader that my code belongs to doesn't have access to Jenkins' PluginManager 'uberClassLoader'?  

3: Same as [2] but adding the jar to the JVM's classpath rather than adding it to the jar. Same result as [2].

Has anyone tried doing anything similar to this before? I'm really keen to be able to do things like organise my code in packages and keep it in small maintainable units. Grateful for any suggestions.

Cheers,

Edd

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ed5d103d-96eb-4585-928d-04e66c6bbaf3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Break init scripts up in to re-usable classes

Cesar Canassa
In reply to this post by Edd Grant
Did you find any solution for this? I am currently struggling with the same issue :(

I am currently using something similar to this:

https://github.com/packt-jenkins-course/volume1/blob/b519ddac905b8184e39d86be871837a186c6982f/Section1_ConfiguringJenkins/4_secrets/groovy_init/init.groovy

It works, but I wish I could just import the classes instead of doing the evaluate trick

On Monday, March 12, 2018 at 11:24:42 PM UTC+1, Edd Grant wrote:
Gah, spotted a typo. Just to avoid confusion [3] should say "rather than adding it to the war", not the jar.

Cheers,

Edd

On Monday, 12 March 2018 21:57:42 UTC, Edd Grant wrote:
Hi folks,

I'm using the init hook script mechanism ($JENKINS_HOME/init.groovy.d) to programmatically configure Jenkins. I'm building up quite a library of code and want to split it out in to re-usable classes, to keep things maintainable and make it easier to write well focused tests etc. I have tried the following with no luck:

1: Trying to split classes out in to separate scripts and using Groovy's 'evaluate' function to load the classes from each script (basically trying to re-implement some sort of importing behaviour). This doesn't work at all due to the way Groovy manipulates scripts.

2: Putting my classes in a jar file and adding this to WEB-INF/lib in the Jenkins war. This approach leads to classloader errors, presumably because the jar classloader that my code belongs to doesn't have access to Jenkins' PluginManager 'uberClassLoader'?  

3: Same as [2] but adding the jar to the JVM's classpath rather than adding it to the jar. Same result as [2].

Has anyone tried doing anything similar to this before? I'm really keen to be able to do things like organise my code in packages and keep it in small maintainable units. Grateful for any suggestions.

Cheers,

Edd

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/1d17e0d4-e6b2-4df3-b8d9-9af31de8c4f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Break init scripts up in to re-usable classes

Ewelina Wilkosz
Hi Cesar,

not yet, but I guess at some point a PR with fix for ldap plugin will happen
will let you know when it happens (or you can have a look at https://github.com/jenkinsci/configuration-as-code-plugin/blob/master/PLUGINS.md and prepare a PR on your own :) )

On Wednesday, May 30, 2018 at 11:40:47 PM UTC+2, Cesar Canassa wrote:
Did you find any solution for this? I am currently struggling with the same issue :(

I am currently using something similar to this:

<a href="https://github.com/packt-jenkins-course/volume1/blob/b519ddac905b8184e39d86be871837a186c6982f/Section1_ConfiguringJenkins/4_secrets/groovy_init/init.groovy" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fpackt-jenkins-course%2Fvolume1%2Fblob%2Fb519ddac905b8184e39d86be871837a186c6982f%2FSection1_ConfiguringJenkins%2F4_secrets%2Fgroovy_init%2Finit.groovy\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFS6ffO3rUnJAkSu82Rc_foOLWqpQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fpackt-jenkins-course%2Fvolume1%2Fblob%2Fb519ddac905b8184e39d86be871837a186c6982f%2FSection1_ConfiguringJenkins%2F4_secrets%2Fgroovy_init%2Finit.groovy\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFS6ffO3rUnJAkSu82Rc_foOLWqpQ&#39;;return true;">https://github.com/packt-jenkins-course/volume1/blob/b519ddac905b8184e39d86be871837a186c6982f/Section1_ConfiguringJenkins/4_secrets/groovy_init/init.groovy

It works, but I wish I could just import the classes instead of doing the evaluate trick

On Monday, March 12, 2018 at 11:24:42 PM UTC+1, Edd Grant wrote:
Gah, spotted a typo. Just to avoid confusion [3] should say "rather than adding it to the war", not the jar.

Cheers,

Edd

On Monday, 12 March 2018 21:57:42 UTC, Edd Grant wrote:
Hi folks,

I'm using the init hook script mechanism ($JENKINS_HOME/init.groovy.d) to programmatically configure Jenkins. I'm building up quite a library of code and want to split it out in to re-usable classes, to keep things maintainable and make it easier to write well focused tests etc. I have tried the following with no luck:

1: Trying to split classes out in to separate scripts and using Groovy's 'evaluate' function to load the classes from each script (basically trying to re-implement some sort of importing behaviour). This doesn't work at all due to the way Groovy manipulates scripts.

2: Putting my classes in a jar file and adding this to WEB-INF/lib in the Jenkins war. This approach leads to classloader errors, presumably because the jar classloader that my code belongs to doesn't have access to Jenkins' PluginManager 'uberClassLoader'?  

3: Same as [2] but adding the jar to the JVM's classpath rather than adding it to the jar. Same result as [2].

Has anyone tried doing anything similar to this before? I'm really keen to be able to do things like organise my code in packages and keep it in small maintainable units. Grateful for any suggestions.

Cheers,

Edd

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fbf1d44d-a7d1-4b4b-afb6-ef4c8fffffcf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.