Skip to content

Commit 7b13c31

Browse files
committed
Merge branch 'master' of https://github.com/jenkinsci/docker-workflow-plugin into CustomDescribableModel-JENKINS-44892
2 parents 1e6b746 + b174d46 commit 7b13c31

File tree

15 files changed

+123
-200
lines changed

15 files changed

+123
-200
lines changed

pom.xml

Lines changed: 38 additions & 15 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>4.6</version>
7+
<version>4.12</version>
88
<relativePath />
99
</parent>
1010
<artifactId>docker-workflow</artifactId>
@@ -29,12 +29,12 @@
2929
</scm>
3030

3131
<properties>
32-
<revision>1.24</revision>
32+
<revision>1.27</revision>
3333
<changelist>-SNAPSHOT</changelist>
34-
<jenkins.version>2.176.4</jenkins.version>
34+
<jenkins.version>2.190.3</jenkins.version>
3535
<java.level>8</java.level>
36-
<useBeta>true</useBeta>
37-
<pipeline-model-definition-plugin.version>1.5.1</pipeline-model-definition-plugin.version>
36+
<useBeta>true</useBeta> <!-- TODO pending release of https://github.com/jenkinsci/structs-plugin/pull/65 -->
37+
<pipeline-model-definition-plugin.version>1.8.1</pipeline-model-definition-plugin.version>
3838
</properties>
3939
<repositories>
4040
<repository>
@@ -52,11 +52,42 @@
5252
<dependencies>
5353
<dependency>
5454
<groupId>io.jenkins.tools.bom</groupId>
55-
<artifactId>bom-2.176.x</artifactId>
56-
<version>11</version>
55+
<artifactId>bom-2.190.x</artifactId>
56+
<version>14</version>
5757
<scope>import</scope>
5858
<type>pom</type>
5959
</dependency>
60+
<dependency> <!-- TODO until we are on 2.222.x and can use bom 22+ -->
61+
<groupId>org.jenkinsci.plugins</groupId>
62+
<artifactId>pipeline-model-api</artifactId>
63+
<version>${pipeline-model-definition-plugin.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.jenkinsci.plugins</groupId>
67+
<artifactId>pipeline-model-definition</artifactId>
68+
<version>${pipeline-model-definition-plugin.version}</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.jenkinsci.plugins</groupId>
72+
<artifactId>pipeline-model-definition</artifactId>
73+
<classifier>tests</classifier>
74+
<version>${pipeline-model-definition-plugin.version}</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.jenkinsci.plugins</groupId>
78+
<artifactId>pipeline-model-extensions</artifactId>
79+
<version>${pipeline-model-definition-plugin.version}</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.jenkinsci.plugins</groupId>
83+
<artifactId>pipeline-stage-tags-metadata</artifactId>
84+
<version>${pipeline-model-definition-plugin.version}</version>
85+
</dependency>
86+
<dependency> <!-- TODO bom problem -->
87+
<groupId>org.jenkins-ci.plugins</groupId>
88+
<artifactId>jackson2-api</artifactId>
89+
<scope>test</scope>
90+
</dependency>
6091
</dependencies>
6192
</dependencyManagement>
6293
<dependencies>
@@ -81,11 +112,6 @@
81112
<groupId>org.jenkins-ci.plugins.workflow</groupId>
82113
<artifactId>workflow-step-api</artifactId>
83114
</dependency>
84-
<dependency>
85-
<groupId>org.jenkinsci.plugins</groupId>
86-
<artifactId>pipeline-model-extensions</artifactId>
87-
<version>${pipeline-model-definition-plugin.version}</version>
88-
</dependency>
89115
<dependency>
90116
<groupId>org.jenkins-ci.plugins</groupId>
91117
<artifactId>cloudbees-folder</artifactId>
@@ -127,8 +153,6 @@
127153
<dependency>
128154
<groupId>org.jenkinsci.plugins</groupId>
129155
<artifactId>pipeline-model-definition</artifactId>
130-
<version>${pipeline-model-definition-plugin.version}</version>
131-
<scope>test</scope>
132156
</dependency>
133157
<dependency>
134158
<groupId>org.jenkins-ci.plugins</groupId>
@@ -150,7 +174,6 @@
150174
<dependency>
151175
<groupId>org.jenkinsci.plugins</groupId>
152176
<artifactId>pipeline-model-definition</artifactId>
153-
<version>${pipeline-model-definition-plugin.version}</version>
154177
<classifier>tests</classifier>
155178
<scope>test</scope>
156179
</dependency>

src/main/java/org/jenkinsci/plugins/docker/workflow/client/ControlGroup.java

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import java.util.ArrayList;
1212
import java.util.List;
1313

14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
1417
/**
1518
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
1619
*/
@@ -50,42 +53,32 @@ static Optional<String> getContainerId(Reader reader) throws IOException {
5053
}
5154

5255
public String getContainerId() throws IOException {
53-
if (group.contains("/docker/")) {
54-
// 4:cpuset:/system.slice/docker-3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b.scope
55-
// 2:cpu:/docker/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
56-
// 2:cpu:/docker-ce/docker/7cacbc548047c130ae50653548f037285806d49c0c4c1543925cffb8873ed213
57-
// 10:cpu,cpuacct:/docker/a9f3c3932cd81c4a74cc7e0a18c3300255159512f1d000545c42895adaf68932/docker/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
58-
// 3:cpu:/docker/4193df6bcf5fce75f3fc77f303b2ac06fb664adeb269b959b7ae17b3f8dcf329/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
59-
int i = group.lastIndexOf('/');
60-
if (group.length() < i+1+64) throw new IOException("Unexpected cgroup syntax "+group);
61-
return group.substring(i+1, i+1+64);
62-
}
63-
if (group.startsWith("/ecs/")) {
64-
// 7:cpu:/ecs/0410eff2-7e59-4111-823e-1e0d98ef7f30/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
65-
int i = group.lastIndexOf('/');
66-
if (group.length() < i+1+64) throw new IOException("Unexpected cgroup syntax "+group);
67-
return group.substring(i+1, i+1+64);
68-
}
69-
if (group.contains("/docker-")) {
70-
// 8:cpuset:/kubepods.slice/kubepods-pod9c26dfb6_b9c9_11e7_bfb9_02c6c1fc4861.slice/docker-3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b.scope
71-
int i = group.lastIndexOf("/docker-");
72-
if (group.length() < i+8+64) throw new IOException("Unexpected cgroup syntax "+group);
73-
return group.substring(i+8, i+8+64);
74-
}
75-
if (group.startsWith("/kubepods/")) {
76-
// 8:cpuset:/kubepods/besteffort/pod60070ae4-c63a-11e7-92b3-0adc1ac11520/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
77-
int i = group.lastIndexOf('/');
78-
if (group.length() < i+1+64) throw new IOException("Unexpected cgroup syntax "+group);
79-
return group.substring(i+1, i+1+64);
56+
57+
// 4:cpuset:/system.slice/docker-3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b.scope
58+
// 2:cpu:/docker/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
59+
// 2:cpu:/docker-ce/docker/7cacbc548047c130ae50653548f037285806d49c0c4c1543925cffb8873ed213
60+
// 10:cpu,cpuacct:/docker/a9f3c3932cd81c4a74cc7e0a18c3300255159512f1d000545c42895adaf68932/docker/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
61+
// 3:cpu:/docker/4193df6bcf5fce75f3fc77f303b2ac06fb664adeb269b959b7ae17b3f8dcf329/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
62+
// 7:cpu:/ecs/0410eff2-7e59-4111-823e-1e0d98ef7f30/3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b
63+
// 8:cpuset:/kubepods.slice/kubepods-pod9c26dfb6_b9c9_11e7_bfb9_02c6c1fc4861.slice/docker-3dd988081e7149463c043b5d9c57d7309e079c5e9290f91feba1cc45a04d6a5b.scope
64+
// 12:freezer:/actions_job/ddecc467e1fb3295425e663efb6531282c1c936f25a3eeb7bb64e7b0fc61a216
65+
// 11:pids:/kubepods/burstable/pod1fe52ba4-5709-11ea-9ee3-00505682780f/d65c8853fa45d139ce95d5c2b68a6e4aa8da83894d8eb0396cd6edd1c134c97c/user.slice
66+
67+
Matcher matcher = Pattern.compile("([a-z0-9]{64})").matcher(group);
68+
69+
String containerId = null;
70+
71+
while ( matcher.find() ) {
72+
containerId = matcher.group();
8073
}
81-
if (group.startsWith("/actions_job/")) {
82-
// 12:freezer:/actions_job/ddecc467e1fb3295425e663efb6531282c1c936f25a3eeb7bb64e7b0fc61a216
83-
int i = group.lastIndexOf('/');
84-
if (group.length() < i+1+64) throw new IOException("Unexpected cgroup syntax "+group);
85-
return group.substring(i+1, i+1+64);
74+
75+
if ( null == containerId &&
76+
Pattern.compile("^(\\/docker\\/|\\/ecs\\/|\\/docker-|\\/kubepods\\/|\\/actions_job\\/).*").matcher(group).matches() )
77+
{
78+
throw new IOException( "Unexpected cgroup syntax " + group );
8679
}
8780

88-
return null;
81+
return containerId;
8982
}
9083

9184
}

src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ public class DockerClient {
7676
@Restricted(NoExternalUse.class)
7777
public static int CLIENT_TIMEOUT = Integer.getInteger(DockerClient.class.getName() + ".CLIENT_TIMEOUT", 180); // TODO 2.4+ SystemProperties
7878

79+
/**
80+
* Skip removal of container after a container has been stopped.
81+
*/
82+
@SuppressFBWarnings(value="MS_SHOULD_BE_FINAL", justification="mutable for scripts")
83+
@Restricted(NoExternalUse.class)
84+
public static boolean SKIP_RM_ON_STOP = Boolean.getBoolean(DockerClient.class.getName() + ".SKIP_RM_ON_STOP");
85+
7986
// e.g. 2015-04-09T13:40:21.981801679Z
8087
public static final String DOCKER_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
8188

@@ -165,7 +172,8 @@ public List<String> listProcess(@Nonnull EnvVars launchEnv, @Nonnull String cont
165172
* Stop a container.
166173
*
167174
* <p>
168-
* Also removes ({@link #rm(EnvVars, String)}) the container.
175+
* Also removes ({@link #rm(EnvVars, String)}) the container if property
176+
* SKIP_RM_ON_STOP is unset or equals false.
169177
*
170178
* @param launchEnv Docker client launch environment.
171179
* @param containerId The container ID.
@@ -175,7 +183,9 @@ public void stop(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws
175183
if (result.getStatus() != 0) {
176184
throw new IOException(String.format("Failed to kill container '%s'.", containerId));
177185
}
178-
rm(launchEnv, containerId);
186+
if (!SKIP_RM_ON_STOP) {
187+
rm(launchEnv, containerId);
188+
}
179189
}
180190

181191
/**

src/main/java/org/jenkinsci/plugins/docker/workflow/client/WindowsDockerClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hudson.FilePath;
66
import hudson.Launcher;
77
import hudson.model.Node;
8+
import hudson.os.WindowsUtil;
89
import hudson.util.ArgumentListBuilder;
910

1011
import javax.annotation.CheckForNull;
@@ -46,7 +47,7 @@ public String run(@Nonnull EnvVars launchEnv, @Nonnull String image, @CheckForNu
4647
}
4748
for (Map.Entry<String, String> variable : containerEnv.entrySet()) {
4849
argb.add("-e");
49-
argb.addMasked(variable.getKey()+"="+variable.getValue());
50+
argb.addMasked(WindowsUtil.quoteArgument(variable.getKey() + "=" + variable.getValue()));
5051
}
5152
argb.add(image).add(command);
5253

src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/DeclarativeDockerUtils.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@
2626

2727
import hudson.model.Run;
2828
import org.apache.commons.lang.StringUtils;
29-
import org.jenkinsci.plugins.docker.workflow.declarative.DockerPropertiesProvider;
29+
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.Label;
30+
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript;
3031
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
3132
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
33+
import org.jenkinsci.plugins.workflow.cps.CpsScript;
3234
import org.jenkinsci.plugins.workflow.cps.CpsThread;
3335
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
3436

3537
import javax.annotation.Nullable;
3638
import java.io.IOException;
3739
import java.io.Serializable;
40+
import java.util.Collections;
3841

3942
/**
4043
* @see org.jenkinsci.plugins.docker.workflow.declarative.DockerLabelProvider
@@ -82,6 +85,16 @@ public static String getLabel(@Nullable String override) {
8285
return null;
8386
}
8487

88+
@Whitelisted
89+
public static WithScriptScript<?> getLabelScript(AbstractDockerAgent<?> describable, CpsScript script) throws Exception {
90+
String targetLabel = getLabel(describable.getLabel());
91+
Label l = (Label) Label.DescriptorImpl.instanceForName("label", Collections.singletonMap("label", targetLabel));
92+
l.copyFlags(describable);
93+
l.setCustomWorkspace(describable.getCustomWorkspace());
94+
return l.getScript(script);
95+
96+
}
97+
8598
@Whitelisted
8699
public static String getRegistryUrl() {
87100
return getRegistryUrl(null);

src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/ExtensionFilterImpl.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/main/resources/org/jenkinsci/plugins/docker/workflow/DockerDSL/help.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
Hexadecimal ID of a running container.
113113
</p>
114114
</dd>
115-
<dt><code>Container.stop</code></dt>
115+
<dt><code>Container.stop()</code></dt>
116116
<dd>
117117
<p>
118118
Runs <code>docker stop</code> and <code>docker rm</code> to shut down a container and remove its storage.

src/main/resources/org/jenkinsci/plugins/docker/workflow/declarative/AbstractDockerPipelineScript.groovy

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package org.jenkinsci.plugins.docker.workflow.declarative
2727

2828
import hudson.FilePath
29-
import jenkins.model.Jenkins
3029
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
3130
import org.jenkinsci.plugins.workflow.cps.CpsScript
3231

@@ -43,25 +42,16 @@ abstract class AbstractDockerPipelineScript<A extends AbstractDockerAgent<A>> ex
4342
configureRegistry(body).call()
4443
}
4544
} else if (describable.containerPerStageRoot) {
46-
return getLabelScript().run {
45+
return DeclarativeDockerUtils.getLabelScript(describable, script).run {
4746
body.call()
4847
}
4948
} else {
50-
return getLabelScript().run {
49+
return DeclarativeDockerUtils.getLabelScript(describable, script).run {
5150
configureRegistry(body).call()
5251
}
5352
}
5453
}
5554

56-
protected DeclarativeAgentScript getLabelScript() {
57-
String targetLabel = DeclarativeDockerUtils.getLabel(describable.label)
58-
// TODO revert reflection in daad17b90ed0 when we can depend directly on pipeline-model-definition
59-
def l = Jenkins.get().getDescriptorByName('org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.Label').instanceForName("label", [label: targetLabel])
60-
l.copyFlags(describable)
61-
l.customWorkspace = describable.customWorkspace
62-
return (DeclarativeAgentScript) l.getScript(script)
63-
}
64-
6555
protected Closure configureRegistry(Closure body) {
6656
return {
6757
DeclarativeDockerUtils.DockerRegistry registry = DeclarativeDockerUtils.DockerRegistry.build(describable.registryUrl, describable.registryCredentialsId)

src/main/resources/org/jenkinsci/plugins/docker/workflow/declarative/DockerPipelineFromDockerfileScript.groovy

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@
2525

2626
package org.jenkinsci.plugins.docker.workflow.declarative
2727

28-
import jenkins.model.Jenkins
2928
import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames
29+
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
3030
import org.jenkinsci.plugins.workflow.cps.CpsScript
3131
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
3232

3333
class DockerPipelineFromDockerfileScript extends AbstractDockerPipelineScript<DockerPipelineFromDockerfile> {
3434

35-
// TODO revert reflection in daad17b90ed0 when we can depend directly on pipeline-model-definition
36-
static Class Utils = Jenkins.get().pluginManager.getPlugin('pipeline-model-definition').classLoader.loadClass('org.jenkinsci.plugins.pipeline.modeldefinition.Utils')
37-
3835
DockerPipelineFromDockerfileScript(CpsScript s, DockerPipelineFromDockerfile a) {
3936
super(s, a)
4037
}

src/main/resources/org/jenkinsci/plugins/docker/workflow/declarative/DockerPipelineScript.groovy

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@
2525

2626
package org.jenkinsci.plugins.docker.workflow.declarative
2727

28-
import jenkins.model.Jenkins
2928
import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames
29+
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
3030
import org.jenkinsci.plugins.workflow.cps.CpsScript
3131

3232
class DockerPipelineScript extends AbstractDockerPipelineScript<DockerPipeline> {
3333

34-
// TODO revert reflection in daad17b90ed0 when we can depend directly on pipeline-model-definition
35-
static Class Utils = Jenkins.get().pluginManager.getPlugin('pipeline-model-definition').classLoader.loadClass('org.jenkinsci.plugins.pipeline.modeldefinition.Utils')
36-
3734
DockerPipelineScript(CpsScript s, DockerPipeline a) {
3835
super(s, a)
3936
}

0 commit comments

Comments
 (0)