Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework DockerBuilderPublisher to work under dockerNode() #756

Merged
merged 1 commit into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DockerCloud> 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<DockerCloud> cloud = JenkinsUtils.getCloudForChannel(channel);
if (!cloud.isPresent())
throw new RuntimeException("Could not find the cloud this project was built on");

Expand All @@ -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 {
Expand All @@ -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<String> tagsToUse, DockerCloud dockerCloud) {
private Run(hudson.model.Run<?, ?> run, final Launcher launcher, final TaskListener listener, FilePath fpChild, List<String> 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;

}

Expand Down Expand Up @@ -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();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down