Praqma / JenkinsAsCodeReference

This repository is intended for the reference Jenkins configuration as code as well as JobDSL library
BSD 3-Clause "New" or "Revised" License
142 stars 110 forks source link

Groovy hook tries to compile init scripts #226

Open alexsedova opened 6 years ago

alexsedova commented 6 years ago

Jenkins as a code setup has good numbers of groovy scripts for many different plugins configurations. However, we don't want users to install all plugins the setup is supporting. One of the option to prevent compilation during Jenkins startup is to use a def dynamic variable and whole classpath. It seems some scripts don't meet these requirements and fail during startup with a compilation error. For example, artifactory.groovy.

WARNING: Failed to run script file:/var/jenkins_home/init.groovy.d/artifactory.groovy
jmaster_1       | org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
jmaster_1       | /var/jenkins_home/init.groovy.d/artifactory.groovy: 22: unable to resolve class CredentialsConfig 
jmaster_1       |  @ line 22, column 23.
jmaster_1       |        CredentialsConfig deployerCredentials = new CredentialsConfig(getPasswordCredentials(serverConfig.deployerCredentialsId),
jmaster_1       |                          ^
jmaster_1       | 
jmaster_1       | /var/jenkins_home/init.groovy.d/artifactory.groovy: 22: unable to resolve class CredentialsConfig 
jmaster_1       |  @ line 22, column 45.
jmaster_1       |    sConfig deployerCredentials = new Creden
jmaster_1       |                                  ^
jmaster_1       | 
jmaster_1       | /var/jenkins_home/init.groovy.d/artifactory.groovy: 25: unable to resolve class CredentialsConfig 
jmaster_1       |  @ line 25, column 23.
jmaster_1       |        CredentialsConfig resolverCredentials = new CredentialsConfig(getPasswordCredentials(serverConfig.deployerCredentialsId),
jmaster_1       |                          ^
jmaster_1       | 
jmaster_1       | /var/jenkins_home/init.groovy.d/artifactory.groovy: 25: unable to resolve class CredentialsConfig 
jmaster_1       |  @ line 25, column 45.
jmaster_1       |    sConfig resolverCredentials = new Creden
jmaster_1       |                                  ^
jmaster_1       | 
jmaster_1       | /var/jenkins_home/init.groovy.d/artifactory.groovy: 28: unable to resolve class ArtifactoryServer 
jmaster_1       |  @ line 28, column 10.
jmaster_1       |        List<ArtifactoryServer> servers =  desc.getArtifactoryServers()
jmaster_1       |             ^
jmaster_1       | 
jmaster_1       | /var/jenkins_home/init.groovy.d/artifactory.groovy: 29: unable to resolve class ArtifactoryServer 
jmaster_1       |  @ line 29, column 23.
jmaster_1       |        ArtifactoryServer server = new ArtifactoryServer(serverConfig.serverName,
jmaster_1       |                          ^
jmaster_1       | 
jmaster_1       | /var/jenkins_home/init.groovy.d/artifactory.groovy: 29: unable to resolve class ArtifactoryServer 
jmaster_1       |  @ line 29, column 32.
jmaster_1       |        ArtifactoryServer server = new ArtifactoryServer(serverConfig.serverName,
jmaster_1       |                                   ^
jmaster_1       | 
jmaster_1       | 7 errors
jmaster_1       | 
jmaster_1       |  at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
jmaster_1       |  at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:946)
jmaster_1       |  at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
jmaster_1       |  at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542)
jmaster_1       |  at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
jmaster_1       |  at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
jmaster_1       |  at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
jmaster_1       |  at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
jmaster_1       |  at groovy.lang.GroovyShell.evaluate(GroovyShell.java:584)
jmaster_1       |  at jenkins.util.groovy.GroovyHookScript.execute(GroovyHookScript.java:136)
jmaster_1       |  at jenkins.util.groovy.GroovyHookScript.execute(GroovyHookScript.java:127)
jmaster_1       |  at jenkins.util.groovy.GroovyHookScript.run(GroovyHookScript.java:110)
jmaster_1       |  at hudson.init.impl.GroovyInitScript.init(GroovyInitScript.java:41)
jmaster_1       |  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
jmaster_1       |  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
jmaster_1       |  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
jmaster_1       |  at java.lang.reflect.Method.invoke(Method.java:498)
jmaster_1       |  at hudson.init.TaskMethodFinder.invoke(TaskMethodFinder.java:104)
jmaster_1       |  at hudson.init.TaskMethodFinder$TaskImpl.run(TaskMethodFinder.java:175)
jmaster_1       |  at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:282)
jmaster_1       |  at jenkins.model.Jenkins$7.runTask(Jenkins.java:1089)
jmaster_1       |  at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:210)
jmaster_1       |  at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:117)
jmaster_1       |  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
jmaster_1       |  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
jmaster_1       |  at java.lang.Thread.run(Thread.java:745)
alexsedova commented 6 years ago

Found two scripts bitbucket. groovy and artifactory.groovy. The problem is it tries to download classes by name not by classpath. To avoid that it needs to create a special object using Class.forName(String: "whole-package-class-path") and call this class constructor using getDeclaredConstructor() default method.