Skip to content

Commit c7c36eb

Browse files
committed
Using GeneralNonBlockingStepExecution.
1 parent 490870e commit c7c36eb

File tree

6 files changed

+168
-43
lines changed

6 files changed

+168
-43
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.jenkins-ci.plugins</groupId>
66
<artifactId>plugin</artifactId>
7-
<version>3.23</version>
7+
<version>3.29</version>
88
<relativePath />
99
</parent>
1010
<artifactId>docker-workflow</artifactId>
@@ -33,7 +33,7 @@
3333
<changelist>-SNAPSHOT</changelist>
3434
<jenkins.version>2.60.3</jenkins.version>
3535
<java.level>8</java.level>
36-
<workflow-step-api-plugin.version>2.9</workflow-step-api-plugin.version>
36+
<workflow-step-api-plugin.version>2.17-rc491.f7bec8d9dc29</workflow-step-api-plugin.version> <!-- TODO https://github.com/jenkinsci/workflow-step-api-plugin/pull/38 -->
3737
<workflow-support-plugin.version>2.12</workflow-support-plugin.version>
3838
<workflow-cps-plugin.version>2.25</workflow-cps-plugin.version>
3939
</properties>

src/main/java/org/jenkinsci/plugins/docker/workflow/AbstractEndpointStepExecution.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,18 @@
3434
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
3535
import org.jenkinsci.plugins.workflow.steps.StepContext;
3636

37+
/** @deprecated only here for binary compatibility */
38+
@Deprecated
3739
abstract class AbstractEndpointStepExecution extends AbstractStepExecutionImpl {
3840

3941
private static final long serialVersionUID = 1;
4042

41-
protected abstract KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException;
43+
protected KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException {
44+
throw new AssertionError();
45+
}
4246

4347
@Override public final boolean start() throws Exception {
44-
KeyMaterialFactory keyMaterialFactory = newKeyMaterialFactory();
45-
KeyMaterial material = keyMaterialFactory.materialize();
46-
getContext().newBodyInvoker().
47-
withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new Expander(material))).
48-
withCallback(new Callback(material)).
49-
start();
50-
return false;
48+
throw new AssertionError();
5149
}
5250

5351
@Override public final void stop(Throwable cause) throws Exception {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2015, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package org.jenkinsci.plugins.docker.workflow;
25+
26+
import hudson.EnvVars;
27+
import java.io.IOException;
28+
import java.util.logging.Level;
29+
import java.util.logging.Logger;
30+
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterial;
31+
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
32+
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
33+
import org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution;
34+
import org.jenkinsci.plugins.workflow.steps.StepContext;
35+
36+
abstract class AbstractEndpointStepExecution2 extends GeneralNonBlockingStepExecution {
37+
38+
private static final long serialVersionUID = 1;
39+
40+
protected AbstractEndpointStepExecution2(StepContext context) {
41+
super(context);
42+
}
43+
44+
protected abstract KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException;
45+
46+
@Override public final boolean start() throws Exception {
47+
run(this::doStart);
48+
return false;
49+
}
50+
51+
private void doStart() throws Exception {
52+
KeyMaterialFactory keyMaterialFactory = newKeyMaterialFactory();
53+
KeyMaterial material = keyMaterialFactory.materialize();
54+
getContext().newBodyInvoker().
55+
withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new Expander(material))).
56+
withCallback(new Callback(material)).
57+
start();
58+
}
59+
60+
private static class Expander extends EnvironmentExpander {
61+
62+
private static final long serialVersionUID = 1;
63+
private final KeyMaterial material;
64+
65+
Expander(KeyMaterial material) {
66+
this.material = material;
67+
}
68+
69+
@Override public void expand(EnvVars env) throws IOException, InterruptedException {
70+
env.putAll(material.env());
71+
}
72+
73+
}
74+
75+
private class Callback extends TailCall {
76+
77+
private static final long serialVersionUID = 1;
78+
private final KeyMaterial material;
79+
80+
Callback(KeyMaterial material) {
81+
this.material = material;
82+
}
83+
84+
@Override protected void finished(StepContext context) throws Exception {
85+
try {
86+
material.close();
87+
} catch (IOException x) {
88+
Logger.getLogger(AbstractEndpointStepExecution2.class.getName()).log(Level.WARNING, null, x);
89+
}
90+
}
91+
92+
}
93+
94+
}

src/main/java/org/jenkinsci/plugins/docker/workflow/RegistryEndpointStep.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package org.jenkinsci.plugins.docker.workflow;
2525

26-
import com.google.inject.Inject;
26+
import com.google.common.collect.ImmutableSet;
2727
import hudson.EnvVars;
2828
import hudson.Extension;
2929
import hudson.FilePath;
@@ -36,21 +36,22 @@
3636
import java.util.Collections;
3737
import java.util.HashMap;
3838
import java.util.Map;
39+
import java.util.Set;
3940
import java.util.TreeMap;
4041
import javax.annotation.CheckForNull;
4142
import javax.annotation.Nonnull;
4243
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
4344
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
4445
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
4546
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
46-
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
47-
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
4847
import org.jenkinsci.plugins.workflow.steps.Step;
49-
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
48+
import org.jenkinsci.plugins.workflow.steps.StepContext;
49+
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
50+
import org.jenkinsci.plugins.workflow.steps.StepExecution;
5051
import org.kohsuke.stapler.DataBoundConstructor;
5152
import org.kohsuke.stapler.DataBoundSetter;
5253

53-
public class RegistryEndpointStep extends AbstractStepImpl {
54+
public class RegistryEndpointStep extends Step {
5455

5556
private final @Nonnull DockerRegistryEndpoint registry;
5657
private @CheckForNull String toolName;
@@ -72,29 +73,39 @@ public String getToolName() {
7273
this.toolName = Util.fixEmpty(toolName);
7374
}
7475

75-
public static class Execution extends AbstractEndpointStepExecution {
76+
@Override public StepExecution start(StepContext context) throws Exception {
77+
return new Execution2(this, context);
78+
}
79+
80+
private static final class Execution2 extends AbstractEndpointStepExecution2 {
7681

7782
private static final long serialVersionUID = 1;
7883

79-
@Inject(optional=true) private transient RegistryEndpointStep step;
80-
@StepContextParameter private transient Job<?,?> job;
81-
@StepContextParameter private transient FilePath workspace;
82-
@StepContextParameter private transient Launcher launcher;
83-
@StepContextParameter private transient TaskListener listener;
84-
@StepContextParameter private transient Node node;
85-
@StepContextParameter private transient EnvVars envVars;
84+
private transient RegistryEndpointStep step;
85+
86+
Execution2(RegistryEndpointStep step, StepContext context) {
87+
super(context);
88+
this.step = step;
89+
}
8690

8791
@Override protected KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException {
88-
return step.registry.newKeyMaterialFactory(job, workspace, launcher, envVars, listener, DockerTool.getExecutable(step.toolName, node, listener, envVars));
92+
TaskListener listener = getContext().get(TaskListener.class);
93+
EnvVars envVars = getContext().get(EnvVars.class);
94+
String executable = DockerTool.getExecutable(step.toolName, getContext().get(Node.class), listener, envVars);
95+
return step.registry.newKeyMaterialFactory(getContext().get(Job.class), getContext().get(FilePath.class), getContext().get(Launcher.class), envVars, listener, executable);
8996
}
9097

9198
}
9299

93-
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl {
100+
/** @deprecated only here for binary compatibility */
101+
@Deprecated
102+
public static class Execution extends AbstractEndpointStepExecution {
94103

95-
public DescriptorImpl() {
96-
super(Execution.class);
97-
}
104+
private static final long serialVersionUID = 1;
105+
106+
}
107+
108+
@Extension public static class DescriptorImpl extends StepDescriptor {
98109

99110
@Override public String getFunctionName() {
100111
return "withDockerRegistry";
@@ -135,6 +146,11 @@ public DescriptorImpl() {
135146
return super.newInstance(arguments);
136147
}
137148

149+
@SuppressWarnings("unchecked")
150+
@Override public Set<? extends Class<?>> getRequiredContext() {
151+
return ImmutableSet.of(TaskListener.class, EnvVars.class, Node.class, Job.class, FilePath.class, Launcher.class);
152+
}
153+
138154
}
139155

140156
}

src/main/java/org/jenkinsci/plugins/docker/workflow/ServerEndpointStep.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@
2323
*/
2424
package org.jenkinsci.plugins.docker.workflow;
2525

26-
import com.google.inject.Inject;
26+
import com.google.common.collect.ImmutableSet;
2727
import hudson.Extension;
2828
import hudson.FilePath;
2929
import hudson.model.Job;
3030
import java.io.IOException;
31+
import java.util.Set;
3132
import javax.annotation.Nonnull;
3233
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
3334
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
34-
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
35-
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
36-
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
35+
import org.jenkinsci.plugins.workflow.steps.Step;
36+
import org.jenkinsci.plugins.workflow.steps.StepContext;
37+
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
38+
import org.jenkinsci.plugins.workflow.steps.StepExecution;
3739
import org.kohsuke.stapler.DataBoundConstructor;
3840

39-
public class ServerEndpointStep extends AbstractStepImpl {
41+
public class ServerEndpointStep extends Step {
4042

4143
private final @Nonnull DockerServerEndpoint server;
4244

@@ -49,25 +51,36 @@ public DockerServerEndpoint getServer() {
4951
return server;
5052
}
5153

52-
public static class Execution extends AbstractEndpointStepExecution {
54+
@Override public StepExecution start(StepContext context) throws Exception {
55+
return new Execution2(this, context);
56+
}
57+
58+
private static final class Execution2 extends AbstractEndpointStepExecution2 {
5359

5460
private static final long serialVersionUID = 1;
5561

56-
@Inject(optional=true) private transient ServerEndpointStep step;
57-
@StepContextParameter private transient Job<?,?> job;
58-
@StepContextParameter private transient FilePath workspace;
62+
private transient final ServerEndpointStep step;
63+
64+
Execution2(ServerEndpointStep step, StepContext context) {
65+
super(context);
66+
this.step = step;
67+
}
5968

6069
@Override protected KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException {
61-
return step.server.newKeyMaterialFactory(job, workspace.getChannel());
70+
return step.server.newKeyMaterialFactory(getContext().get(Job.class), getContext().get(FilePath.class).getChannel());
6271
}
6372

6473
}
6574

66-
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl {
75+
/** @deprecated only here for binary compatibility */
76+
@Deprecated
77+
public static class Execution extends AbstractEndpointStepExecution {
6778

68-
public DescriptorImpl() {
69-
super(Execution.class);
70-
}
79+
private static final long serialVersionUID = 1;
80+
81+
}
82+
83+
@Extension public static class DescriptorImpl extends StepDescriptor {
7184

7285
@Override public String getFunctionName() {
7386
return "withDockerServer";
@@ -85,8 +98,11 @@ public DescriptorImpl() {
8598
return true;
8699
}
87100

88-
// TODO allow DockerServerEndpoint fields to be inlined, as in RegistryEndpointStep, so Docker.groovy can say simply: script.withDockerServer(uri: uri, credentialsId: credentialsId) {…}
101+
@Override public Set<? extends Class<?>> getRequiredContext() {
102+
return ImmutableSet.of(Job.class, FilePath.class);
103+
}
89104

105+
// TODO allow DockerServerEndpoint fields to be inlined, as in RegistryEndpointStep, so Docker.groovy can say simply: script.withDockerServer(uri: uri, credentialsId: credentialsId) {…}
90106
}
91107

92108
}

src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private static void destroy(String container, Launcher launcher, Node node, EnvV
108108
new DockerClient(launcher, node, toolName).stop(launcherEnv, container);
109109
}
110110

111+
// TODO switch to GeneralNonBlockingStepExecution
111112
public static class Execution extends AbstractStepExecutionImpl {
112113

113114
private static final long serialVersionUID = 1;

0 commit comments

Comments
 (0)