Skip to content

Commit

Permalink
[FIXED JENKINS-41668] Add "dir" option for Dockerfile.
Browse files Browse the repository at this point in the history
  • Loading branch information
abayer committed Feb 6, 2017
1 parent 46fe307 commit d8d42f1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
Expand Up @@ -38,6 +38,7 @@

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

@DataBoundConstructor
public DockerPipelineFromDockerfile() {
Expand All @@ -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;
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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();
Expand Down
@@ -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.