Skip to content
Permalink
Browse files

[FIXED JENKINS-41668] Add "dir" option for Dockerfile.

  • Loading branch information
abayer committed Feb 6, 2017
1 parent 46fe307 commit d8d42f135382a5fe5535005d49467d773daefe82
@@ -38,6 +38,7 @@

public class DockerPipelineFromDockerfile extends AbstractDockerAgent<DockerPipelineFromDockerfile> {
private String filename;
private String dir;

@DataBoundConstructor
public DockerPipelineFromDockerfile() {
@@ -52,6 +53,23 @@ public void setFilename(String filename) {
this.filename = filename;
}

public @Nonnull String getDir() {
return dir;
}

@DataBoundSetter
public void setDir(String dir) {
this.dir = dir;
}

public String getActualDir() {
if (dir != null) {
return dir;
} else {
return ".";
}
}

public String getDockerfileAsString() {
if (filename != null) {
return filename;
@@ -77,7 +77,7 @@ public class DockerPipelineFromDockerfileScript extends AbstractDockerPipelineSc
try {
def hash = Utils.stringToSHA1(script.readFile(describable.getDockerfileAsString()))
def imgName = "${hash}"
return script.getProperty("docker").build(imgName, "-f ${describable.getDockerfileAsString()} .")
return script.getProperty("docker").build(imgName, "-f ${describable.getDockerfileAsString()} ${describable.getActualDir()}")
} catch (FileNotFoundException f) {
script.error("No Dockerfile found at root of repository - failing.")
return null
@@ -160,6 +160,26 @@ public void fromDockerfile() throws Exception {
.go();
}

@Issue("JENKINS-41668")
@Test
public void fromDockerfileInOtherDir() throws Exception {
assumeDocker();
// Bind mounting /var on OS X doesn't work at the moment
onAllowedOS(PossibleOS.LINUX);

sampleRepo.write("subdir/Dockerfile", "FROM ubuntu:14.04\n\nRUN echo 'HI THERE' > /hi-there\n\n");
sampleRepo.git("init");
sampleRepo.git("add", "Dockerfile");
sampleRepo.git("commit", "--message=Dockerfile");

expect("fromDockerfileInOtherDir")
.logContains("[Pipeline] { (foo)",
"The answer is 42",
"-v /tmp:/tmp -p 8000:8000",
"HI THERE")
.go();
}

@Test
public void fromDockerfileNoArgs() throws Exception {
assumeDocker();
@@ -0,0 +1,43 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
agent {
dockerfile {
args "-v /tmp:/tmp -p 8000:8000"
dir "subdir"
}
}
stages {
stage("foo") {
steps {
sh 'cat /hi-there'
sh 'echo "The answer is 42"'
}
}
}
}



0 comments on commit d8d42f1

Please sign in to comment.
You can’t perform that action at this time.