Skip to content

Commit 2bef7d5

Browse files
committed
Merge branch 'master' of https://github.com/jenkinsci/docker-workflow-plugin into shell
2 parents 60e3018 + 129a2d7 commit 2bef7d5

File tree

17 files changed

+179
-57
lines changed

17 files changed

+179
-57
lines changed

.github/dependabot.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
version: 2
22
updates:
3-
- package-ecosystem: "maven"
4-
directory: "/"
5-
schedule:
6-
interval: "weekly"
3+
- package-ecosystem: maven
4+
directory: /
5+
schedule:
6+
interval: monthly
7+
- package-ecosystem: github-actions
8+
directory: /
9+
schedule:
10+
interval: monthly

.github/release-drafter.yml

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

.github/workflows/cd.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Note: additional setup is required, see https://www.jenkins.io/redirect/continuous-delivery-of-plugins
2+
3+
name: cd
4+
on:
5+
workflow_dispatch:
6+
check_run:
7+
types:
8+
- completed
9+
10+
jobs:
11+
maven-cd:
12+
uses: jenkins-infra/github-reusable-workflows/.github/workflows/maven-cd.yml@v1
13+
secrets:
14+
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
15+
MAVEN_TOKEN: ${{ secrets.MAVEN_TOKEN }}

.github/workflows/release-drafter.yml

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

.mvn/extensions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<extension>
33
<groupId>io.jenkins.tools.incrementals</groupId>
44
<artifactId>git-changelist-maven-extension</artifactId>
5-
<version>1.0-beta-4</version>
5+
<version>1.4</version>
66
</extension>
77
</extensions>

.mvn/maven.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
-Pconsume-incrementals
22
-Pmight-produce-incrementals
3+
-Dchangelist.format=%d.v%s

pom.xml

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<parent>
55
<groupId>org.jenkins-ci.plugins</groupId>
66
<artifactId>plugin</artifactId>
7-
<version>4.12</version>
7+
<version>4.51</version>
88
<relativePath />
99
</parent>
1010
<artifactId>docker-workflow</artifactId>
11-
<version>${revision}${changelist}</version>
11+
<version>${changelist}</version>
1212
<name>Docker Pipeline</name>
1313
<description>Build and use Docker containers from pipelines</description>
1414
<packaging>hpi</packaging>
@@ -20,20 +20,18 @@
2020
</license>
2121
</licenses>
2222

23-
<url>https://github.com/jenkinsci/docker-workflow-plugin</url>
23+
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
2424
<scm>
25-
<connection>scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git</connection>
26-
<developerConnection>scm:git:git@github.com:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
27-
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
25+
<connection>scm:git:https://github.com/${gitHubRepo}.git</connection>
26+
<developerConnection>scm:git:git@github.com:${gitHubRepo}.git</developerConnection>
27+
<url>https://github.com/${gitHubRepo}</url>
2828
<tag>${scmTag}</tag>
2929
</scm>
3030

3131
<properties>
32-
<revision>1.29</revision>
33-
<changelist>-SNAPSHOT</changelist>
34-
<jenkins.version>2.222.4</jenkins.version>
35-
<java.level>8</java.level>
36-
<pipeline-model-definition-plugin.version>1.8.1</pipeline-model-definition-plugin.version>
32+
<changelist>999999-SNAPSHOT</changelist>
33+
<jenkins.version>2.332.1</jenkins.version>
34+
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
3735
</properties>
3836
<repositories>
3937
<repository>
@@ -51,19 +49,30 @@
5149
<dependencies>
5250
<dependency>
5351
<groupId>io.jenkins.tools.bom</groupId>
54-
<artifactId>bom-2.222.x</artifactId>
55-
<version>887.vae9c8ac09ff7</version>
52+
<artifactId>bom-2.332.x</artifactId>
53+
<version>1643.v1cffef51df73</version>
5654
<scope>import</scope>
5755
<type>pom</type>
5856
</dependency>
59-
57+
<dependency>
58+
<groupId>org.jenkinsci.plugins</groupId>
59+
<artifactId>pipeline-model-api</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.jenkinsci.plugins</groupId>
63+
<artifactId>pipeline-model-extensions</artifactId>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.jenkinsci.plugins</groupId>
67+
<artifactId>pipeline-stage-tags-metadata</artifactId>
68+
</dependency>
6069
</dependencies>
6170
</dependencyManagement>
6271
<dependencies>
6372
<dependency>
6473
<groupId>org.jenkins-ci.plugins</groupId>
6574
<artifactId>docker-commons</artifactId>
66-
<version>1.19</version>
75+
<version>1.21</version>
6776
</dependency>
6877
<dependency>
6978
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -116,7 +125,6 @@
116125
<dependency>
117126
<groupId>org.jenkins-ci.plugins</groupId>
118127
<artifactId>config-file-provider</artifactId>
119-
<version>2.10.1</version>
120128
<scope>test</scope>
121129
</dependency>
122130
<dependency>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import hudson.Extension;
2929
import org.jenkinsci.plugins.workflow.cps.CpsScript;
3030
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;
31+
import org.jenkinsci.plugins.workflow.cps.GroovySourceFileAllowlist;
3132

3233
/**
3334
* Something you should <strong>not copy</strong>. Write plain old {@code Step}s and leave it at that.
@@ -53,4 +54,13 @@
5354
return docker;
5455
}
5556

57+
@Extension
58+
public static class DockerDSLAllowlist extends GroovySourceFileAllowlist {
59+
private final String scriptUrl = DockerDSL.class.getResource("Docker.groovy").toString();
60+
61+
@Override
62+
public boolean isAllowed(String groovyResourceUrl) {
63+
return groovyResourceUrl.equals(scriptUrl);
64+
}
65+
}
5666
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public String getToolName() {
107107
this.toolName = Util.fixEmpty(toolName);
108108
}
109109

110-
private static void destroy(String container, Launcher launcher, Node node, EnvVars launcherEnv, String toolName) throws Exception {
110+
private static void destroy(String container, @NonNull Launcher launcher, Node node, EnvVars launcherEnv, String toolName) throws Exception {
111111
new DockerClient(launcher, node, toolName).stop(launcherEnv, container);
112112
}
113113

@@ -350,6 +350,15 @@ private static class Decorator extends LauncherDecorator implements Serializable
350350
@Override public void kill(Map<String,String> modelEnvVars) throws IOException, InterruptedException {
351351
ByteArrayOutputStream baos = new ByteArrayOutputStream();
352352
String executable = getExecutable();
353+
// For this code to be able to successfully kill processes:
354+
// 1: The image must contain `ps`, which notably excludes slim variants of Debian.
355+
// 2. The version of `ps` being used must support `-o command`. Busybox does not (so no Alpine!).
356+
// `-o args` seems to be a more portable equivalent, but even then, see JENKINS-52881.
357+
// 3. The version of `ps` being used must support the `e` output modifier to include environment
358+
// variables in the command's output. Otherwise, the output only includes `jsc=<cookie here>` and
359+
// `JENKINS_SERVER_COOKIE=$jsc`, but the output must include `JENKINS_SERVER_COOKIE=<cookie here>
360+
// for this code to work.
361+
// In practice, this means that this code only works with images that include procps.
353362
if (getInner().launch().cmds(executable, "exec", container, "ps", "-A", "-o", "pid,command", "e").stdout(baos).quiet(true).start().joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, listener) != 0) {
354363
throw new IOException("failed to run ps");
355364
}
@@ -401,7 +410,10 @@ private static class Callback extends BodyExecutionCallback.TailCall {
401410
}
402411

403412
@Override protected void finished(StepContext context) throws Exception {
404-
destroy(container, context.get(Launcher.class), context.get(Node.class), context.get(EnvVars.class), toolName);
413+
Launcher launcher = context.get(Launcher.class);
414+
if (launcher != null) {
415+
destroy(container, launcher, context.get(Node.class), context.get(EnvVars.class), toolName);
416+
}
405417
}
406418

407419
}

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@
3636
import java.io.BufferedReader;
3737
import java.io.ByteArrayOutputStream;
3838
import java.io.IOException;
39+
import java.io.InputStream;
40+
import java.io.InputStreamReader;
3941
import java.io.Reader;
4042
import java.io.StringReader;
4143
import java.nio.charset.Charset;
44+
import java.nio.charset.StandardCharsets;
4245
import java.text.ParseException;
4346
import java.text.SimpleDateFormat;
4447
import java.util.ArrayList;
@@ -196,7 +199,7 @@ public void stop(@NonNull EnvVars launchEnv, @NonNull String containerId) throws
196199
*/
197200
public void rm(@NonNull EnvVars launchEnv, @NonNull String containerId) throws IOException, InterruptedException {
198201
LaunchResult result;
199-
result = launch(launchEnv, false, "rm", "-f", containerId);
202+
result = launch(launchEnv, false, "rm", "-f", "--volumes", containerId);
200203
if (result.getStatus() != 0) {
201204
throw new IOException(String.format("Failed to rm container '%s'.", containerId));
202205
}
@@ -336,6 +339,8 @@ public String whoAmI() throws IOException, InterruptedException {
336339

337340
}
338341

342+
private static final Pattern hostnameMount = Pattern.compile("/containers/([a-z0-9]{64})/hostname");
343+
339344
/**
340345
* Checks if this {@link DockerClient} instance is running inside a container and returns the id of the container
341346
* if so.
@@ -349,10 +354,26 @@ public Optional<String> getContainerIdIfContainerized() throws IOException, Inte
349354
return Optional.absent();
350355
}
351356
FilePath cgroupFile = node.createPath("/proc/self/cgroup");
352-
if (cgroupFile == null || !cgroupFile.exists()) {
353-
return Optional.absent();
357+
if (cgroupFile != null && cgroupFile.exists()) {
358+
Optional<String> containerId = ControlGroup.getContainerId(cgroupFile);
359+
if (containerId.isPresent()) {
360+
return containerId;
361+
}
362+
}
363+
// cgroup v2
364+
FilePath mountInfo = node.createPath("/proc/1/mountinfo");
365+
if (mountInfo != null && mountInfo.exists()) {
366+
try (InputStream is = mountInfo.read(); Reader r = new InputStreamReader(is, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(r)) {
367+
String line;
368+
while ((line = br.readLine()) != null) {
369+
Matcher m = hostnameMount.matcher(line);
370+
if (m.find()) {
371+
return Optional.of(m.group(1));
372+
}
373+
}
374+
}
354375
}
355-
return ControlGroup.getContainerId(cgroupFile);
376+
return Optional.absent();
356377
}
357378

358379
public ContainerRecord getContainerRecord(@NonNull EnvVars launchEnv, String containerId) throws IOException, InterruptedException {

0 commit comments

Comments
 (0)