diff --git a/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java b/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java index 381581be3..3e72d51a2 100644 --- a/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java +++ b/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java @@ -362,6 +362,7 @@ public void run() { // TODO where can we log provisioning progress ? final DockerAPI api = DockerCloud.this.getDockerApi(); slave = t.provisionNode(api, TaskListener.NULL); + slave.setDockerAPI(api); slave.setCloudId(DockerCloud.this.name); plannedNode.complete(slave); diff --git a/src/main/java/com/nirima/jenkins/plugins/docker/builder/DockerBuilderPublisher.java b/src/main/java/com/nirima/jenkins/plugins/docker/builder/DockerBuilderPublisher.java index 191357b29..f7273bd88 100644 --- a/src/main/java/com/nirima/jenkins/plugins/docker/builder/DockerBuilderPublisher.java +++ b/src/main/java/com/nirima/jenkins/plugins/docker/builder/DockerBuilderPublisher.java @@ -23,15 +23,18 @@ import hudson.Launcher; import hudson.model.AbstractProject; import hudson.model.Item; +import hudson.model.Node; import hudson.model.TaskListener; import hudson.remoting.RemoteInputStream; import hudson.remoting.VirtualChannel; +import hudson.remoting.Channel; import hudson.slaves.Cloud; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.Builder; import hudson.util.FormValidation; import hudson.util.ListBoxModel; import io.jenkins.docker.client.DockerAPI; +import io.jenkins.docker.DockerTransientNode; import jenkins.MasterToSlaveFileCallable; import jenkins.model.Jenkins; import jenkins.tasks.SimpleBuildStep; @@ -204,15 +207,21 @@ public static void verifyTags(String tagsString) { } } - protected DockerCloud getCloud(Launcher launcher) { + protected DockerAPI getDockerAPI(Launcher launcher) { DockerCloud theCloud; + VirtualChannel channel = launcher.getChannel(); if (!Strings.isNullOrEmpty(cloud)) { theCloud = JenkinsUtils.getServer(cloud); } else { - - Optional cloud = JenkinsUtils.getCloudForChannel(launcher.getChannel()); + if(channel instanceof Channel) { + Node node = Jenkins.getInstance().getNode(((Channel)channel).getName() ); + if (node instanceof DockerTransientNode) { + return ((DockerTransientNode) node).getDockerAPI(); + } + } + Optional cloud = JenkinsUtils.getCloudForChannel(channel); if (!cloud.isPresent()) throw new RuntimeException("Could not find the cloud this project was built on"); @@ -226,13 +235,14 @@ protected DockerCloud getCloud(Launcher launcher) { for (DockerCloud dc : JenkinsUtils.getServers()) { if (!dc.isTriton()) { LOGGER.warn("Picked {} cloud instead", dc.getDisplayName()); - return dc; + theCloud = dc; + break; } } } - return theCloud; + return theCloud.getDockerApi(); } class Run implements Serializable { @@ -248,14 +258,14 @@ class Run implements Serializable { final transient hudson.model.Run run; - private Run(hudson.model.Run run, final Launcher launcher, final TaskListener listener, FilePath fpChild, List tagsToUse, DockerCloud dockerCloud) { + private Run(hudson.model.Run run, final Launcher launcher, final TaskListener listener, FilePath fpChild, List tagsToUse, DockerAPI dockerApi) { this.run = run; this.launcher = launcher; this.listener = listener; this.fpChild = fpChild; this.tagsToUse = tagsToUse; - this.dockerApi = dockerCloud.getDockerApi(); + this.dockerApi = dockerApi; } @@ -424,7 +434,7 @@ public void perform(hudson.model.Run run, FilePath workspace, Launcher lau } catch (MacroEvaluationException e) { listener.getLogger().println("Couldn't macro expand docker file directory " + dockerFileDirectory); } - new Run(run, launcher, listener, new FilePath(workspace, expandedDockerFileDirectory), expandedTags, getCloud(launcher)).run(); + new Run(run, launcher, listener, new FilePath(workspace, expandedDockerFileDirectory), expandedTags, getDockerAPI(launcher)).run(); } diff --git a/src/main/java/io/jenkins/docker/pipeline/DockerNodeStepExecution.java b/src/main/java/io/jenkins/docker/pipeline/DockerNodeStepExecution.java index f6dca3680..74b4cdb4f 100644 --- a/src/main/java/io/jenkins/docker/pipeline/DockerNodeStepExecution.java +++ b/src/main/java/io/jenkins/docker/pipeline/DockerNodeStepExecution.java @@ -92,6 +92,7 @@ private DockerTransientNode createNode(TaskListener listener) { Computer computer = null; try { node = t.provisionNode(api, listener); + node.setDockerAPI(api); node.setAcceptingTasks(false); // Prevent this node to be used by tasks from build queue Jenkins.getInstance().addNode(node); diff --git a/src/test/java/io/jenkins/docker/pipeline/DockerNodeStepTest.java b/src/test/java/io/jenkins/docker/pipeline/DockerNodeStepTest.java index 52acaed25..21ded8d11 100644 --- a/src/test/java/io/jenkins/docker/pipeline/DockerNodeStepTest.java +++ b/src/test/java/io/jenkins/docker/pipeline/DockerNodeStepTest.java @@ -259,6 +259,22 @@ public void evaluate() throws Throwable { }); } + @Test + public void dockerBuilderPublisher() throws Exception { + story.addStep(new Statement() { + @Override + public void evaluate() throws Throwable { + WorkflowJob j = story.j.jenkins.createProject(WorkflowJob.class, "dockerBuilderPublisher"); + j.setDefinition(new CpsFlowDefinition("dockerNode(dockerHost: 'unix:///var/run/docker.sock', image: 'jenkins/slave', remoteFs: '/home/jenkins') {\n" + + " writeFile(file: 'Dockerfile', text: 'FROM jenkins/slave')\n" + + " step([$class: 'DockerBuilderPublisher', dockerFileDirectory: ''])\n" + + "}\n", true)); + WorkflowRun r = story.j.buildAndAssertSuccess(j); + story.j.assertLogContains("Successfully built", r); + } + }); + } + public static class PathModifierStep extends Step { private String element;