Skip to content

Commit

Permalink
[JENKINS-36332] I would like to apply the same command parameter that…
Browse files Browse the repository at this point in the history
…'s used in JENKINS-33063 to the "withRun" method so that I can clean up most of my containers as soon as they're finished.

Given that I also kind of need this I just PR my take on the solution :)
  • Loading branch information
Christian Alonso Chavez Ley committed Aug 22, 2016
1 parent 75f183a commit 770f038
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Expand Up @@ -139,9 +139,9 @@ class Docker implements Serializable {
}
}

public <V> V withRun(String args = '', Closure<V> body) {
public <V> V withRun(String args = '', String command = "", Closure<V> body) {
docker.node {
Container c = run(args)
Container c = run(args, command)
try {
body.call(c)
} finally {
Expand Down
Expand Up @@ -65,7 +65,7 @@
Records a run fingerprint in the build.
</p>
</dd>
<dt><code>Image.withRun[(args)] {…}</code></dt>
<dt><code>Image.withRun([args, command]) {…}</code></dt>
<dd>
<p>
Like <code>run</code> but stops the container as soon as its body exits, so you do not need a <code>try</code>-<code>finally</code> block.
Expand Down
Expand Up @@ -224,6 +224,41 @@ private static void grep(File dir, String text, String prefix, Set<String> match
});
}

@Test public void withRunCommand() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
assumeDocker();
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
p.setDefinition(new CpsFlowDefinition(
" semaphore 'wait'\n" +
" docker.image('maven:3.3.9-jdk-8').withRun(\"--entrypoint mvn\", \"-version\") {c ->\n" +
" sh \"docker logs ${c.id}\"" +
"}", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("wait/1", b);
}
});
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
SemaphoreStep.success("wait/1", null);
WorkflowJob p = story.j.jenkins.getItemByFullName("prj", WorkflowJob.class);
WorkflowRun b = p.getLastBuild();
story.j.assertLogContains("Maven home: /usr/share/maven", story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)));
story.j.assertLogContains("Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre", b);
DockerClient client = new DockerClient(new Launcher.LocalLauncher(StreamTaskListener.NULL), null, null);
String httpdIID = client.inspect(new EnvVars(), "maven:3.3.9-jdk-8", ".Id");
Fingerprint f = DockerFingerprints.of(httpdIID);
assertNotNull(f);
DockerRunFingerprintFacet facet = f.getFacet(DockerRunFingerprintFacet.class);
assertNotNull(facet);
assertEquals(1, facet.records.size());
assertNotNull(facet.records.get(0).getContainerName());
assertEquals(Fingerprint.RangeSet.fromString("1", false), facet.getRangeSet(p));
assertEquals(Collections.singleton("maven"), DockerImageExtractor.getDockerImagesUsedByJobFromAll(p));
}
});
}

@Test public void build() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
Expand Down

0 comments on commit 770f038

Please sign in to comment.