jenkinsci / docker-plugin

Jenkins cloud plugin that uses Docker
https://plugins.jenkins.io/docker-plugin/
MIT License
490 stars 319 forks source link

NPE trying to execute system groovy script after upgrade to 1.1.3 #620

Open dhc8 opened 6 years ago

dhc8 commented 6 years ago

docker-plugin version: 1.1.3 (was not present on 1.1.1; downgrading resolves it again) jenkins version: 2.89.4 groovy plugin version: 2.0 docker engin version: 1.9.0

Execute system groovy script step (which runs on master) will fail with the following stacktrace:

java.lang.NullPointerException: Cannot invoke method replaceFirst() on null object
    at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:35)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
    at org.kohsuke.groovy.sandbox.impl.Checker$checkedCall$1.callStatic(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
    at Script1.run(Script1.groovy:7)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.run(GroovySandbox.java:141)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:333)
    at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
    at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
    at hudson.model.Build$BuildExecution.build(Build.java:206)
    at hudson.model.Build$BuildExecution.doRun(Build.java:163)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
    at hudson.model.Run.execute(Run.java:1724)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)

Other potentially useful details: The following errors occur in Manage Old Data (with a type of hudson.model.FreeStyleBuild):

MissingFieldException: No field 'cloudId' found in class 'com.nirima.jenkins.plugins.docker.action.DockerBuildAction',
MissingFieldException: No field 'inspect' found in class 'com.nirima.jenkins.plugins.docker.action.DockerBuildAction'
pjdarton commented 6 years ago

What groovy script are you running in the "Execute system groovy script" step?

(If you can provide sufficient details to allow the bug to be reproduced outside your environment, it'll make it much easier to fix)

ndeloof commented 6 years ago

your script probably assumes some internal behaviour or exposed data. By nature, groovy scripts aren't stable between release as there's no way for plugin author to know which internal stuff may be used by scripts.

dhc8 commented 6 years ago

Script is set to Groovy script file and executes a script present in the workspace containing this code:

import hudson.model.*
import java.net.*

def docker_host = ''
def host = ''

docker_host = build.buildVariableResolver.resolve("DOCKER_HOST").replaceFirst('tcp', 'http')

Looks like it's this last line that's failing

pjdarton commented 6 years ago

Hmm, sounds like resolve("DOCKER_HOST") is returning null. Are you 100% sure that DOCKER_HOST is a variable that should be available?

PS. Personally I avoid chaining multiple thing.thing.method().anotherMethod() calls together because it can then be unclear where (in the long line of multiple calls) the NPE came from. Groovy allows you to "def something=foo.bar" then "def somethingelse=bar.method()" etc so you can easily split a long line into multiple statements without having to have a mass of import statements. We got away with it in this case as there's only one "replaceFirst" method, so we can be fairly sure where it failed. PS again. I think "DOCKER_HOST" should be 'DOCKER_HOST' as you don't need a GString there.

dhc8 commented 6 years ago

"Expose DOCKER_HOST" is checked. It also works perfectly with version 1.1.1 of the docker plugin (both before upgrading to 1.1.3 and after downgrading again). It seems that something between 1.1.1 and 1.1.3 has made the DOCKER_HOST variable no longer available.